Module: Flapjack::Utility
- Included in:
- CLI::Migrate, Data::Alert, Data::Rule, Gateways::AwsSns, Gateways::Email, Gateways::JSONAPI, Gateways::Jabber::Interpreter, Gateways::Jabber::Notifier, Gateways::Oobetet::Bot, Gateways::PagerDuty::Notifier, Gateways::Slack, Gateways::SmsAspsms, Gateways::SmsMessagenet, Gateways::SmsNexmo, Gateways::SmsTwilio, Gateways::Web, Notifier, Processor
- Defined in:
- lib/flapjack/utility.rb
Instance Method Summary collapse
-
#hashify(*args, &block) ⇒ Object
The passed block will be provided each value from the args and must return array pairs [key, value] representing members of the hash this method returns.
- #load_template(template_config, message_type, message_filetype, default_template_path) ⇒ Object
-
#local_timezone ⇒ Object
returns a string showing the local timezone we’re running in eg “CST (UTC+09:30)”.
-
#relative_time_ago(from_time, to_time) ⇒ Object
Returns relative time in words referencing the given date relative_time_ago(Time.now, Time.now - 1) => ‘about a minute ago’.
- #remove_utc_offset(time) ⇒ Object
- #stringify(obj) ⇒ Object
- #symbolize(obj) ⇒ Object
- #time_period_in_words(period) ⇒ Object
-
#truncate(str, length, options = {}) ⇒ Object
copied from ActiveSupport.
Instance Method Details
#hashify(*args, &block) ⇒ Object
The passed block will be provided each value from the args and must return array pairs [key, value] representing members of the hash this method returns. Keys should be unique – if they’re not, the earlier pair for that key will be overwritten.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/flapjack/utility.rb', line 66 def hashify(*args, &block) key_value_pairs = args.map {|a| yield(a) } # if using Ruby 1.9, # Hash[ key_value_pairs ] # is all that's needed, but for Ruby 1.8 compatability, these must # be flattened and the resulting array unpacked. flatten(1) only # flattens the arrays constructed in the block, it won't mess up # any values (or keys) that are themselves arrays/hashes. Hash[ *( key_value_pairs.flatten(1) )] end |
#load_template(template_config, message_type, message_filetype, default_template_path) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/flapjack/utility.rb', line 90 def load_template(template_config, , , default_template_path) template_file = if template_config.nil? || !template_config.has_key?("#{}.#{}") # we ship UTF-8 templates with the gem template_path = File.join(default_template_path, "#{}.#{}.erb") File.open(template_path, "r:UTF-8", &:read) else # respects Encoding.default_external (UTF-8 or ENV['LANG']) template_path = template_config["#{}.#{}"] File.read(template_path) end [ERB.new(template_file, nil, '-'), template_file] end |
#local_timezone ⇒ Object
returns a string showing the local timezone we’re running in eg “CST (UTC+09:30)”
38 39 40 41 42 43 44 |
# File 'lib/flapjack/utility.rb', line 38 def local_timezone tzname = Time.new.zone q, r = Time.new.utc_offset.divmod(3600) sign = (q < 0) ? '-' : '+' tzoffset = sign + "%02d" % q.abs.to_s + ':' + r.to_f.div(60).to_s "#{tzname} (UTC#{tzoffset})" end |
#relative_time_ago(from_time, to_time) ⇒ Object
Returns relative time in words referencing the given date relative_time_ago(Time.now, Time.now - 1) => ‘about a minute ago’
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/flapjack/utility.rb', line 19 def relative_time_ago(from_time, to_time) distance_in_minutes = (((from_time.to_time - to_time.to_time).abs)/60).round case distance_in_minutes when 0..1 then 'about a minute' when 2..44 then "#{distance_in_minutes} minutes" when 45..89 then 'about 1 hour' when 90..1439 then "about #{(distance_in_minutes.to_f / 60.0).round} hours" when 1440..2439 then '1 day' when 2440..2879 then 'about 2 days' when 2880..43199 then "#{(distance_in_minutes / 1440).round} days" when 43200..86399 then 'about 1 month' when 86400..525599 then "#{(distance_in_minutes / 43200).round} months" when 525600..1051199 then 'about 1 year' else "over #{(distance_in_minutes / 525600).round} years" end end |
#remove_utc_offset(time) ⇒ Object
46 47 48 |
# File 'lib/flapjack/utility.rb', line 46 def remove_utc_offset(time) Time.utc(time.year, time.month, time.day, time.hour, time.min, time.sec) end |
#stringify(obj) ⇒ Object
50 51 52 53 54 |
# File 'lib/flapjack/utility.rb', line 50 def stringify(obj) return obj.inject({}){|memo,(k,v)| memo[k.to_s] = stringify(v); memo} if obj.is_a?(Hash) return obj.inject([]){|memo,v | memo << stringify(v); memo} if obj.is_a?(Array) obj end |
#symbolize(obj) ⇒ Object
56 57 58 59 60 |
# File 'lib/flapjack/utility.rb', line 56 def symbolize(obj) return obj.inject({}){|memo,(k,v)| memo[k.to_sym] = symbolize(v); memo} if obj.is_a? Hash return obj.inject([]){|memo,v | memo << symbolize(v); memo} if obj.is_a? Array obj end |
#time_period_in_words(period) ⇒ Object
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/flapjack/utility.rb', line 6 def time_period_in_words(period) return "0 seconds" unless period.is_a?(Integer) && period > 0 period_mm, period_ss = period.divmod(60) period_hh, period_mm = period_mm.divmod(60) period_dd, period_hh = period_hh.divmod(24) ["#{period_dd} day#{plural_s(period_dd)}", "#{period_hh} hour#{plural_s(period_hh)}", "#{period_mm} minute#{plural_s(period_mm)}", "#{period_ss} second#{plural_s(period_ss)}"].reject {|s| s =~ /^0 /}.join(', ') end |
#truncate(str, length, options = {}) ⇒ Object
copied from ActiveSupport
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/flapjack/utility.rb', line 79 def truncate(str, length, = {}) text = str.dup [:omission] ||= "..." length_with_room_for_omission = length - [:omission].length stop = [:separator] ? (text.rindex([:separator], length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission (text.length > length ? text[0...stop] + [:omission] : text).to_s end |