Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/automation_object/helpers/string.rb

Overview

String class method additions

Instance Method Summary collapse

Instance Method Details

#join_url(url) ⇒ Object



15
16
17
18
# File 'lib/automation_object/helpers/string.rb', line 15

def join_url(url)
  full_url = chomp('/') + url.reverse.chomp('/').reverse
  full_url
end

#pascalize ⇒ Object

Convert self to pascal case



21
22
23
24
# File 'lib/automation_object/helpers/string.rb', line 21

def pascalize
  return self if self !~ /_/ && self =~ /[A-Z]+.*/
  split('_').map(&:capitalize).join
end

#to_underscore ⇒ String

Convert string to underscore

Returns:

  • (String) —

    underscored string



35
36
37
# File 'lib/automation_object/helpers/string.rb', line 35

def to_underscore
  dup.tap(&:to_underscore!)
end

#to_underscore! ⇒ String

Convert string to underscore

Returns:

  • (String) —

    underscored self string



28
29
30
31
# File 'lib/automation_object/helpers/string.rb', line 28

def to_underscore!
  gsub!(/(.)([A-Z])/, '\1_\2')
  downcase!
end

#valid_url? ⇒ Boolean

Test whether a string is a valid url or not

Returns:

  • (Boolean)


7
8
9
10
11
12
13
# File 'lib/automation_object/helpers/string.rb', line 7

def valid_url?
  uri = URI.parse(self)
  return true if uri.is_a?(URI::HTTP)
  return !(self =~ /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix).nil?
rescue URI::InvalidURIError
  return false
end