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.



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

#domainObject

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

#domain_without_wwwObject

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

#downcase?Boolean

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

#ellipsize(options = {}) ⇒ Object

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(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

#gnix(string) ⇒ Object

Removes all instances of string



214
215
216
# File 'lib/wordnik_ruby_helpers.rb', line 214

def gnix(string)
  self.gsub(string, "")
end

#nix(string) ⇒ Object

Removes first instance of string



209
210
211
# File 'lib/wordnik_ruby_helpers.rb', line 209

def nix(string)
  self.sub(string, "")
end

#permalinkifyObject

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

#pollute(delimiter = "^--^--^") ⇒ Object

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

#remove_http_and_wwwObject

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

#remove_whitespaceObject

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

#sanitize(delimiter = "^--^--^") ⇒ Object

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

#strip_tagsObject

Removes HTML tags from a string



204
205
206
# File 'lib/wordnik_ruby_helpers.rb', line 204

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



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(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



282
283
284
# File 'lib/wordnik_ruby_helpers.rb', line 282

def upcase?
  self.upcase == self
end

#valid_email?Boolean

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

#valid_url?Boolean

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