Class: String
Instance Method Summary (collapse)
-
- (Object) add_http
Prepends 'http://' to the beginning of non-empty strings that don't already have it.
-
- (Object) domain
Extracts domain name from a URL.
-
- (Object) domain_without_www
Extracts domain name (sans 'www.') from a URL string.
-
- (Boolean) downcase?
Returns true if all letters in the string are lowercase.
-
- (Object) ellipsize(options = {})
Removes the middle from long strings, replacing with a placeholder.
-
- (Object) gnix(string)
Removes all instances of string.
-
- (Object) nix(string)
Removes first instance of string.
-
- (Object) permalinkify
Generates a permalink-style string, with odd characters removed, etc.
-
- (Object) pollute(delimiter = "^--^--^")
Pollute the space between every letter in a string, so it will be exempt from any impending string searches.
-
- (Object) remove_http_and_www
Removes presentationally superflous http and/or www text from the beginning of the string.
-
- (Object) remove_whitespace
Removes tab characters and instances of more than one space.
-
- (Object) sanitize(delimiter = "^--^--^")
Meant to be paired with the pollute method, this removes 'pollution' from the string.
-
- (Object) strip_tags
Removes HTML tags from a string.
-
- (Object) truncate_preserving_words(options = {})
Shortens a string, preserving the last word.
-
- (Boolean) upcase?
Returns true if all letters in the string are capitalized.
-
- (Boolean) valid_email?
Returns true or false depending on whether a string appears to be an email address.
-
- (Boolean) valid_url?
Returns true or false depending on whether a string appears to be a URL.
Instance Method Details
- (Object) add_http
Prepends 'http://' to the beginning of non-empty strings that don't already have it.
219 220 221 222 223 |
# File 'lib/wordnik_ruby_helpers.rb', line 219 def add_http return "" if self.blank? return "http://#{self}" unless self.starts_with?("http") self end |
- (Object) domain
Extracts domain name from a URL
253 254 255 256 |
# File 'lib/wordnik_ruby_helpers.rb', line 253 def domain url = self.dup url=~(/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : url end |
- (Object) domain_without_www
Extracts domain name (sans 'www.') from a URL string
259 260 261 |
# File 'lib/wordnik_ruby_helpers.rb', line 259 def domain_without_www self.domain.remove_http_and_www end |
- (Boolean) downcase?
Returns true if all letters in the string are lowercase
287 288 289 |
# File 'lib/wordnik_ruby_helpers.rb', line 287 def downcase? self.downcase == self end |
- (Object) ellipsize(options = {})
Removes the middle from long strings, replacing with a placeholder
185 186 187 188 189 190 191 |
# File 'lib/wordnik_ruby_helpers.rb', line 185 def ellipsize(={}) max = [:max] || 40 delimiter = [:delimiter] || "..." return self if self.size <= max offset = max/2 self[0,offset] + delimiter + self[-offset,offset] end |
- (Object) gnix(string)
Removes all instances of string
214 215 216 |
# File 'lib/wordnik_ruby_helpers.rb', line 214 def gnix(string) self.gsub(string, "") end |
- (Object) nix(string)
Removes first instance of string
209 210 211 |
# File 'lib/wordnik_ruby_helpers.rb', line 209 def nix(string) self.sub(string, "") end |
- (Object) permalinkify
Generates a permalink-style string, with odd characters removed, etc.
194 195 196 197 198 199 200 201 |
# File 'lib/wordnik_ruby_helpers.rb', line 194 def permalinkify result = self.to_s result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics). result.gsub!(/[^\w_ \-]+/i, '') # Remove unwanted chars. result.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row. result.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator. result.downcase end |
- (Object) pollute(delimiter = "^--^--^")
Pollute the space between every letter in a string, so it will be exempt from any impending string searches.
175 176 177 |
# File 'lib/wordnik_ruby_helpers.rb', line 175 def pollute(delimiter = "^--^--^") self.split('').map{|letter| "#{letter}#{delimiter}" }.join end |
- (Object) remove_http_and_www
Removes presentationally superflous http and/or www text from the beginning of the string
226 227 228 229 230 |
# File 'lib/wordnik_ruby_helpers.rb', line 226 def remove_http_and_www return "" if self.blank? return self.split(".").remove_first_element.join(".") if self.starts_with?("www.") self.gsub("http://www.", "").gsub("http://", "").gsub("https://www.", "").gsub("https://", "") end |
- (Object) remove_whitespace
Removes tab characters and instances of more than one space
277 278 279 |
# File 'lib/wordnik_ruby_helpers.rb', line 277 def remove_whitespace self.gnix("\t").split(" ").remove_blanks.join(" ") end |
- (Object) sanitize(delimiter = "^--^--^")
Meant to be paired with the pollute method, this removes 'pollution' from the string
180 181 182 |
# File 'lib/wordnik_ruby_helpers.rb', line 180 def sanitize(delimiter = "^--^--^") self.gsub(delimiter, "") end |
- (Object) strip_tags
Removes HTML tags from a string
204 205 206 |
# File 'lib/wordnik_ruby_helpers.rb', line 204 def self.gsub(/<\/?[^>]*>/, "") end |
- (Object) truncate_preserving_words(options = {})
Shortens a string, preserving the last word. Truncation can be limited by words or characters
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/wordnik_ruby_helpers.rb', line 233 def truncate_preserving_words(={}) end_string = [:end_string] || "..." max_words = [:max_words] || nil if max_words words = self.split() return self if words.size < max_words words = words[0..(max_words-1)] words << end_string words.join(" ") else max_chars = [:max_chars] || 60 return self if self.size < max_chars out = self[0..(max_chars-1)].split(" ") out.pop out << end_string out.join(" ") end end |
- (Boolean) upcase?
Returns true if all letters in the string are capitalized
282 283 284 |
# File 'lib/wordnik_ruby_helpers.rb', line 282 def upcase? self.upcase == self end |
- (Boolean) valid_email?
Returns true or false depending on whether a string appears to be an email address
272 273 274 |
# File 'lib/wordnik_ruby_helpers.rb', line 272 def valid_email? !self.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.(?:[A-Z]{2}|com|org|net|biz|info|name|aero|biz|info|jobs|museum|name)$/i).nil? end |
- (Boolean) valid_url?
Returns true or false depending on whether a string appears to be a URL
264 265 266 267 268 269 |
# File 'lib/wordnik_ruby_helpers.rb', line 264 def valid_url? URI.parse(self) true rescue false end |