Class: String

Inherits:
Object show all
Defined in:
lib/wordnik_ruby_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_httpObject

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

#domainObject

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_wwwObject

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(options={})
   max = options[:max] || 40
   delimiter = options[: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(options={})
  base_url = "http://www.google.com/s2/favicons?domain="
  options[:fallback] ||= base_url
  self.valid_url? ? [base_url, self.domain].join : options[: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

#permalinkifyObject

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_wwwObject

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_whitespaceObject

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_tagsObject

Removes HTML tags from a string



236
237
238
# File 'lib/wordnik_ruby_helpers.rb', line 236

def strip_tags
  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(options={})
  end_string = options[:end_string] || "..."
  max_words = options[: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 = options[: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