Module: EventHub::Helper

Included in:
Configuration, Message, Processor
Defined in:
lib/eventhub/helper.rb

Instance Method Summary collapse

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/eventhub/helper.rb', line 32

def duration(difference)
      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 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(message,max_characters=80) 
 max_characters = 5 if max_characters < 5
 m = message.gsub(/\r\n|\n|\r/m,";")
 return (m[0..max_characters-4] + "...") if m.size > max_characters
 return m
end

#get_hostObject



18
19
20
# File 'lib/eventhub/helper.rb', line 18

def get_host
  Socket.gethostname
end

#get_ip_adressesObject



22
23
24
25
# File 'lib/eventhub/helper.rb', line 22

def get_ip_adresses
  list = Socket.ip_address_list.map { |i| i.ip_address unless i.ipv4_loopback? || i.ipv6_loopback? }.compact
  return list.size == 0 ? ["no ip address found (loopback excluded)"] : list
end

#now_stamp(now = nil) ⇒ Object



27
28
29
30
# File 'lib/eventhub/helper.rb', line 27

def now_stamp(now=nil)
  now ||= Time.now
   now.utc.strftime("%Y-%m-%dT%H:%M:%S.#{now.usec}Z")
end