module Birdwatcher::Util

Constants

HUMAN_PREFIXES

Public Class Methods

ago_in_words_pair(secs) click to toggle source
# File lib/birdwatcher/util.rb, line 15
def self.ago_in_words_pair(secs)
  [[60, :seconds], [60, :minutes], [24, :hours], [100_000, :days]].map{ |count, name|
    if secs > 0
      secs, n = secs.divmod(count)
      "#{n.to_i} #{name}"
    end
  }.compact.reverse[0..1]
end
ago_in_words_singularize(pair) click to toggle source
# File lib/birdwatcher/util.rb, line 24
def self.ago_in_words_singularize(pair)
  if pair.size == 1
    pair.map! {|part| part[0, 2].to_i == 1 ? part.chomp("s") : part }
  else
    pair.map! {|part| part[0, 2].to_i == 1 ? part.chomp("s") : part[0, 2].to_i == 0 ? nil : part }
  end
  pair.compact
end
escape_html(string) click to toggle source
# File lib/birdwatcher/util.rb, line 48
def self.escape_html(string)
  CGI.escapeHTML(string.to_s)
end
excerpt(text, max_length, omission = "...") click to toggle source
# File lib/birdwatcher/util.rb, line 60
def self.excerpt(text, max_length, omission = "...")
  text = text.gsub(/\s/, " ").split(" ").map(&:strip).join(" ")
  return text if text.length < max_length
  text[0..max_length] + omission
end
number_to_human_size(number) click to toggle source
# File lib/birdwatcher/util.rb, line 66
def self.number_to_human_size(number)
  s = number.to_f
  i = HUMAN_PREFIXES.length - 1
  while s > 512 && i > 0
    i -= 1
    s /= 1024
  end
  ((s > 9 || s.modulo(1) < 0.1 ? "%d" : "%.1f") % s) + HUMAN_PREFIXES[i]
end
parse_time(time) click to toggle source
# File lib/birdwatcher/util.rb, line 33
def self.parse_time(time)
  ::Chronic.parse(time)
end
pluralize(count, singular, plural) click to toggle source
# File lib/birdwatcher/util.rb, line 56
def self.pluralize(count, singular, plural)
  count == 1 ? "1 #{singular}" : "#{count} #{plural}"
end
strip_control_characters(string) click to toggle source
# File lib/birdwatcher/util.rb, line 41
def self.strip_control_characters(string)
  string = string.to_s.uncolorize
  string.split("").delete_if do |char|
    char.ascii_only? and (char.ord < 32 or char.ord == 127)
  end.join("")
end
strip_html(string) click to toggle source
# File lib/birdwatcher/util.rb, line 37
def self.strip_html(string)
  string.to_s.gsub(/<\/?[^>]*>/, "")
end
suppress_output() { || ... } click to toggle source
# File lib/birdwatcher/util.rb, line 76
def self.suppress_output(&block)
  original_stdout = $stdout
  $stdout = fake = StringIO.new
  begin
    yield
  ensure
    $stdout = original_stdout
  end
  fake.string
end
suppress_warnings(&block) click to toggle source
# File lib/birdwatcher/util.rb, line 87
def self.suppress_warnings(&block)
  warn_level = $VERBOSE
  $VERBOSE = nil
  result = block.call
  $VERBOSE = warn_level
  result
end
time_ago_in_words(time) click to toggle source
# File lib/birdwatcher/util.rb, line 5
def self.time_ago_in_words(time)
  return "a very very long time ago" if time.year < 1800
  secs = Time.now - time
  return "just now" if secs > -1 && secs < 1
  return "" if secs <= -1
  pair = ago_in_words_pair(secs)
  ary = ago_in_words_singularize(pair)
  ary.size == 0 ? "" : ary.join(" and ") << " ago"
end
unescape_html(string) click to toggle source
# File lib/birdwatcher/util.rb, line 52
def self.unescape_html(string)
  CGI.unescapeHTML(string.to_s)
end