Module: EventHub::Helper
- Included in:
- Configuration, Heartbeat, Message, Processor
- Defined in:
- lib/eventhub/helper.rb
Instance Method Summary collapse
-
#class_to_array(class_name) ⇒ Object
converts a class like EventHub::PlateStore::MyClassName to an array [‘event_hub’,‘plate_store’,‘my_class_name’].
- #duration(difference) ⇒ Object
-
#format_string(message, max_characters = 80) ⇒ Object
replaces CR, LF, CRLF with “;” and cut’s string to requied length by adding “…” if string would be longer.
- #now_stamp(now = nil) ⇒ Object
Instance Method Details
#class_to_array(class_name) ⇒ Object
converts a class like EventHub::PlateStore::MyClassName to an array [‘event_hub’,‘plate_store’,‘my_class_name’]
6 7 8 |
# File 'lib/eventhub/helper.rb', line 6 def class_to_array(class_name) class_name.to_s.split("::").map{ |m| m.gsub(/[A-Z]/) { |c| "_#{c}"}.gsub(/^_/,"").downcase } end |
#duration(difference) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/eventhub/helper.rb', line 23 def duration(difference) negative = difference < 0 difference = difference.abs rest, secs = difference.divmod( 60 ) # self is the time difference t2 - t1 rest, mins = rest.divmod( 60 ) days, hours = rest.divmod( 24 ) secs = secs.truncate milliseconds = ((difference - difference.truncate)*1000).round result = [] result << "#{days} days" if days > 1 result << "#{days} day" if days == 1 result << "#{hours} hours" if hours > 1 result << "#{hours} hour" if hours == 1 result << "#{mins} minutes" if mins > 1 result << "#{mins} minute" if mins == 1 result << "#{secs} seconds" if secs > 1 result << "#{secs} second" if secs == 1 result << "#{milliseconds} milliseconds" if milliseconds > 1 result << "#{milliseconds} millisecond" if milliseconds == 1 return (negative ? "-" : "") + result.join(' ') end |
#format_string(message, max_characters = 80) ⇒ Object
replaces CR, LF, CRLF with “;” and cut’s string to requied length by adding “…” if string would be longer
11 12 13 14 15 16 |
# File 'lib/eventhub/helper.rb', line 11 def format_string(,max_characters=80) max_characters = 5 if max_characters < 5 m = .gsub(/\r\n|\n|\r/m,";") return (m[0..max_characters-4] + "...") if m.size > max_characters return m end |
#now_stamp(now = nil) ⇒ Object
18 19 20 21 |
# File 'lib/eventhub/helper.rb', line 18 def now_stamp(now=nil) now ||= Time.now now.utc.strftime("%Y-%m-%dT%H:%M:%S.#{now.usec}Z") end |