Class: String
Instance Method Summary collapse
-
#add_http ⇒ Object
Prepends ‘http://’ to the beginning of non-empty strings that don’t already have it.
-
#domain ⇒ Object
Extracts domain name from a URL.
-
#domain_without_www ⇒ Object
Extracts domain name (sans ‘www.’) from a URL string.
-
#downcase? ⇒ Boolean
Returns true if all letters in the string are lowercase.
-
#ellipsize(options = {}) ⇒ Object
Removes the middle from long strings, replacing with a placeholder.
-
#favicon_url_for_domain(options = {}) ⇒ Object
Given a URL string, return a URL to a google-hosted PNG favicon for the URL’s domain.
-
#gnix(string) ⇒ Object
Removes all instances of string.
-
#nix(string) ⇒ Object
Removes first instance of string.
-
#permalinkify ⇒ Object
Generates a permalink-style string, with odd characters removed, etc.
- #permalinkify! ⇒ Object
-
#pollute(delimiter = "^--^--^") ⇒ Object
Pollute the space between every letter in a string, so it will be exempt from any impending string searches.
-
#remove_http_and_www ⇒ Object
Removes presentationally superflous http and/or www text from the beginning of the string.
-
#remove_whitespace ⇒ Object
Removes tab characters and instances of more than one space.
-
#sanitize(delimiter = "^--^--^") ⇒ Object
Meant to be paired with the pollute method, this removes ‘pollution’ from the string.
-
#strip_tags ⇒ Object
Removes HTML tags from a string.
-
#truncate_preserving_words(options = {}) ⇒ Object
Shortens a string, preserving the last word.
-
#upcase? ⇒ Boolean
Returns true if all letters in the string are capitalized.
-
#valid_email? ⇒ Boolean
Returns true or false depending on whether a string appears to be an email address.
-
#valid_url? ⇒ Boolean
Returns true or false depending on whether a string appears to be a URL.
Instance Method Details
#add_http ⇒ Object
Prepends ‘http://’ to the beginning of non-empty strings that don’t already have it.
251 252 253 254 255 |
# File 'lib/wordnik_ruby_helpers.rb', line 251 def add_http return "" if self.blank? return "http://#{self}" unless self.starts_with?("http") self end |
#domain ⇒ Object
Extracts domain name from a URL
285 286 287 288 |
# File 'lib/wordnik_ruby_helpers.rb', line 285 def domain url = self.dup url =~ (/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : url end |
#domain_without_www ⇒ Object
Extracts domain name (sans ‘www.’) from a URL string
291 292 293 |
# File 'lib/wordnik_ruby_helpers.rb', line 291 def domain_without_www self.domain.remove_http_and_www end |
#downcase? ⇒ Boolean
Returns true if all letters in the string are lowercase
326 327 328 |
# File 'lib/wordnik_ruby_helpers.rb', line 326 def downcase? self.downcase == self end |
#ellipsize(options = {}) ⇒ Object
Removes the middle from long strings, replacing with a placeholder
213 214 215 216 217 218 219 |
# File 'lib/wordnik_ruby_helpers.rb', line 213 def ellipsize(={}) max = [:max] || 40 delimiter = [:delimiter] || "..." return self if self.size <= max offset = max/2 self[0,offset] + delimiter + self[-offset,offset] end |
#favicon_url_for_domain(options = {}) ⇒ Object
Given a URL string, return a URL to a google-hosted PNG favicon for the URL’s domain
304 305 306 307 308 |
# File 'lib/wordnik_ruby_helpers.rb', line 304 def favicon_url_for_domain(={}) base_url = "http://www.google.com/s2/favicons?domain=" [:fallback] ||= base_url self.valid_url? ? [base_url, self.domain].join : [:fallback] end |
#gnix(string) ⇒ Object
Removes all instances of string
246 247 248 |
# File 'lib/wordnik_ruby_helpers.rb', line 246 def gnix(string) self.gsub(string, "") end |
#nix(string) ⇒ Object
Removes first instance of string
241 242 243 |
# File 'lib/wordnik_ruby_helpers.rb', line 241 def nix(string) self.sub(string, "") end |
#permalinkify ⇒ Object
Generates a permalink-style string, with odd characters removed, etc.
222 223 224 225 226 227 228 229 |
# File 'lib/wordnik_ruby_helpers.rb', line 222 def permalinkify result = self.dup 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 |
#permalinkify! ⇒ Object
231 232 233 |
# File 'lib/wordnik_ruby_helpers.rb', line 231 def permalinkify! self.replace(self.permalinkify) end |
#pollute(delimiter = "^--^--^") ⇒ Object
Pollute the space between every letter in a string, so it will be exempt from any impending string searches.
203 204 205 |
# File 'lib/wordnik_ruby_helpers.rb', line 203 def pollute(delimiter = "^--^--^") self.split('').map{|letter| "#{letter}#{delimiter}" }.join end |
#remove_http_and_www ⇒ Object
Removes presentationally superflous http and/or www text from the beginning of the string
258 259 260 261 262 |
# File 'lib/wordnik_ruby_helpers.rb', line 258 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 |
#remove_whitespace ⇒ Object
Removes tab characters and instances of more than one space
316 317 318 |
# File 'lib/wordnik_ruby_helpers.rb', line 316 def remove_whitespace self.gnix("\t").split(" ").remove_blanks.join(" ") end |
#sanitize(delimiter = "^--^--^") ⇒ Object
Meant to be paired with the pollute method, this removes ‘pollution’ from the string
208 209 210 |
# File 'lib/wordnik_ruby_helpers.rb', line 208 def sanitize(delimiter = "^--^--^") self.gsub(delimiter, "") end |
#strip_tags ⇒ Object
Removes HTML tags from a string
236 237 238 |
# File 'lib/wordnik_ruby_helpers.rb', line 236 def self.gsub(/<\/?[^>]*>/, "") end |
#truncate_preserving_words(options = {}) ⇒ Object
Shortens a string, preserving the last word. Truncation can be limited by words or characters
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/wordnik_ruby_helpers.rb', line 265 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 |
#upcase? ⇒ Boolean
Returns true if all letters in the string are capitalized
321 322 323 |
# File 'lib/wordnik_ruby_helpers.rb', line 321 def upcase? self.upcase == self end |
#valid_email? ⇒ Boolean
Returns true or false depending on whether a string appears to be an email address
311 312 313 |
# File 'lib/wordnik_ruby_helpers.rb', line 311 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 |
#valid_url? ⇒ Boolean
Returns true or false depending on whether a string appears to be a URL
296 297 298 299 300 301 |
# File 'lib/wordnik_ruby_helpers.rb', line 296 def valid_url? URI.parse(self) true rescue false end |