Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/core_ext/core_ext.rb
Instance Method Summary collapse
-
#add_http_prefix ⇒ Object
Prefixes the given url with ‘http://’.
-
#capitalize_words ⇒ Object
General methods.
-
#expand_address_abbreviations ⇒ Object
Address methods.
- #formatted_phone ⇒ Object
- #formatted_zip ⇒ Object
-
#has_http? ⇒ Boolean
Returns true if a given string begins with http:// or https://.
-
#has_trailing_slash? ⇒ Boolean
Returns true if a given string has a trailing slash.
-
#is_page? ⇒ Boolean
Returns true if a given string refers to an HTML page.
-
#to_12_hour_time ⇒ Object
Time methods.
-
#to_host ⇒ Object
Returns the host from a given URL string; returns nil if the string is not a valid URL.
-
#to_uri ⇒ Object
Returns a URI for the given string; nil if the string is invalid.
-
#valid_http_url? ⇒ Boolean
Returns true if the given string is a valid URL.
Instance Method Details
#add_http_prefix ⇒ Object
Prefixes the given url with ‘http://’.
106 107 108 109 110 111 |
# File 'lib/core_ext/core_ext.rb', line 106 def add_http_prefix return if self.blank? _uri = self.to_uri return self if _uri.nil? || _uri.is_a?(URI::FTP) || _uri.is_a?(URI::HTTP) || _uri.is_a?(URI::HTTPS) || _uri.is_a?(URI::LDAP) || _uri.is_a?(URI::MailTo) "http://#{self}" end |
#capitalize_words ⇒ Object
General methods
33 34 35 |
# File 'lib/core_ext/core_ext.rb', line 33 def capitalize_words self.downcase.gsub(/\b([a-z])/) { $1.capitalize }.gsub( "'S", "'s" ) end |
#expand_address_abbreviations ⇒ Object
Address methods
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/core_ext/core_ext.rb', line 39 def _address = self.strip.capitalize_words # NOTE: DO NOT rearrange the replace sequences; order matters! # streets _address.gsub!( /\b(ave|av)\.?\b/i, 'Avenue ' ) _address.gsub!( /\b(blvd|blv|bld|bl)\.?\b/i, 'Boulevard ' ) _address.gsub!( /\bcr\.?\b/i, 'Circle ' ) _address.gsub!( /\bctr\.?\b/i, 'Center ' ) _address.gsub!( /\b(crt|ct)\.?\b/i, 'Court ' ) _address.gsub!( /\bdr\.?\b/i, 'Drive ' ) _address.gsub!( /\b(expressw|expw|expy)\.?\b/i, 'Expressway ' ) _address.gsub!( /\bfrwy\.?\b/i, 'Freeway ' ) _address.gsub!( /\bhwy\.?\b/i, 'Highway ' ) _address.gsub!( /\bln\.?\b/i, 'Lane ' ) _address.gsub!( /\b(prkwy|pkwy|pkw|pky)\.?\b/i, 'Parkway ' ) _address.gsub!( /\bpk\.?\b/i, 'Pike ' ) _address.gsub!( /\bplz\.?\b/i, 'Plaza ' ) _address.gsub!( /\bpl\.?\b/i, 'Place ' ) _address.gsub!( /\brd\.?\b/i, 'Road ' ) _address.gsub!( /\b(rte|rt)\.?\b/i, 'Route ' ) _address.gsub!( /\bste\.?\b/i, 'Suite ' ) _address.gsub!( /\bst\.?\b/i, 'Street ' ) _address.gsub!( /\btrpk\.?\b/i, 'Turnpike ' ) _address.gsub!( /\btr\.?\b/i, 'Trail ' ) # directions _address.gsub!( /\bN\.?e\.?\b/i, 'Northeast ' ) _address.gsub!( /\bS\.?e\.?\b/i, 'Southeast ' ) _address.gsub!( /\bS\.?w\.?\b/i, 'Southwest ' ) _address.gsub!( /\bN\.?w\.?\b/i, 'Northwest ' ) _address.gsub!( /\bN\.?\b/, 'North ' ) _address.gsub!( /\bE\.?\b/, 'East ' ) _address.gsub!( /\bS\.?\b/, 'South ' ) _address.gsub!( /\bW\.?\b/, 'West ' ) _address.gsub!( '.', '' ) _address.gsub!( / +/, ' ' ) _address.strip end |
#formatted_phone ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/core_ext/core_ext.rb', line 80 def formatted_phone if self # remove non-digit characters _self = self.gsub(/[\(\) -]+/, '') # format as phone if 10 digits are left return number_to_phone(_self, :area_code => true ) if !! (_self =~ /[0-9]{10}/) end self end |
#formatted_zip ⇒ Object
91 92 93 94 95 |
# File 'lib/core_ext/core_ext.rb', line 91 def formatted_zip return if self.blank? self.gsub!( /[\(\) -]+/, '' ) self.size == 9 ? "#{self[0 .. 4]}-#{self[5 .. -1]}" : self end |
#has_http? ⇒ Boolean
Returns true if a given string begins with http:// or https://.
114 115 116 |
# File 'lib/core_ext/core_ext.rb', line 114 def has_http? !! (self =~ /^http[s]?:\/\/.+/) end |
#has_trailing_slash? ⇒ Boolean
Returns true if a given string has a trailing slash.
119 120 121 |
# File 'lib/core_ext/core_ext.rb', line 119 def has_trailing_slash? !! (self =~ /\/$/) end |
#is_page? ⇒ Boolean
Returns true if a given string refers to an HTML page.
124 125 126 |
# File 'lib/core_ext/core_ext.rb', line 124 def is_page? !! (self =~ /\.htm[l]?$/) end |
#to_12_hour_time ⇒ Object
Time methods
99 100 101 |
# File 'lib/core_ext/core_ext.rb', line 99 def to_12_hour_time (self == '0' || self.blank?) ? nil : Time.parse( "#{self[0..-3]}:#{self[-2..-1]}" ).to_s( :time ).gsub(/^0/, '') end |
#to_host ⇒ Object
Returns the host from a given URL string; returns nil if the string is not a valid URL.
129 130 131 132 |
# File 'lib/core_ext/core_ext.rb', line 129 def to_host _uri = self.to_uri _uri ? _uri.host : nil end |
#to_uri ⇒ Object
Returns a URI for the given string; nil if the string is invalid.
135 136 137 138 139 140 141 142 143 |
# File 'lib/core_ext/core_ext.rb', line 135 def to_uri begin _uri = URI.parse self rescue URI::InvalidURIError _uri = nil end _uri end |
#valid_http_url? ⇒ Boolean
Returns true if the given string is a valid URL.
146 147 148 |
# File 'lib/core_ext/core_ext.rb', line 146 def valid_http_url? self.scan(/:\/\//).size == 1 && self.to_uri.is_a?(URI::HTTP) end |