Module: Tweetwine::Util

Defined in:
lib/tweetwine/util.rb

Class Method Summary collapse

Class Method Details

.humanize_time_diff(from, to) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/tweetwine/util.rb', line 5

def self.humanize_time_diff(from, to)
  from = Time.parse(from.to_s) unless from.is_a? Time
  to = Time.parse(to.to_s) unless to.is_a? Time

  difference = (to - from).to_i.abs

  value, unit = case difference
  when 0..59 then [difference, "sec"]
  when 60..3599 then [(difference/60.0).round, "min"]
  when 3600..86399 then [(difference/3600.0).round, "hour"]
  else [(difference/86400.0).round, "day"]
  end

  [value, pluralize_unit(value, unit)]
end

.parse_int_gt(value, default, min, name_for_error) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tweetwine/util.rb', line 30

def self.parse_int_gt(value, default, min, name_for_error)
  if value
    value = value.to_i
    if value >= min
      value
    else
      raise ArgumentError, "Invalid #{name_for_error} -- must be greater than or equal to #{min}"
    end
  else
    default
  end
end

.symbolize_hash_keys(hash) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/tweetwine/util.rb', line 21

def self.symbolize_hash_keys(hash)
  hash.inject({}) do |result, pair|
    value = pair.last
    value = symbolize_hash_keys(value) if value.is_a? Hash
    result[pair.first.to_sym] = value
    result
  end
end