Class: Notification::Send

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/gaddygaddy-client/notification.rb

Instance Method Summary collapse

Methods included from Logging

create_logger, #logger, logger, set_log_conf, #set_log_file, #set_log_level

Constructor Details

#initialize(options = {}) ⇒ Send

Returns a new instance of Send.



27
28
29
30
# File 'lib/gaddygaddy-client/notification.rb', line 27

def initialize(options = {})
  I18n.load_path += Dir[File.join(File.dirname(__FILE__), '..','..','locales', '*.yml').to_s]
  @speech_enabled = options[:speech_enabled]
end

Instance Method Details

#eventObject



41
42
43
# File 'lib/gaddygaddy-client/notification.rb', line 41

def event
  @event
end

#event=(event) ⇒ Object



37
38
39
# File 'lib/gaddygaddy-client/notification.rb', line 37

def event= event
  @event = JSON.parse(event)
end

#languageObject

Language is english for now, we need to add a language parameter to the user info or config for gaddygaddy



33
34
35
# File 'lib/gaddygaddy-client/notification.rb', line 33

def language
  'en'
end

#message_as_textObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gaddygaddy-client/notification.rb', line 45

def message_as_text
  logger.debug "Will create message from event #{event['message_key']}"
  translated_text = I18n.t event['message_key'],  {locale: language}
  count_values = translated_text.split("%").count - 1
  if event['message_values']||count_values > 0
    if count_values == event['message_values'].length
      translated_text % event['message_values']
    else
      raise "Mismatch between the text for #{event['message_key']} - #{translated_text} have #{count_values} %s and the size of event values array is #{event['message_values'].length}"
    end
  else
    translated_text
  end
end

#notify(device_id) ⇒ Object

Will do notifications to different sources like log file, web site and even speak



62
63
64
65
66
67
68
69
70
71
# File 'lib/gaddygaddy-client/notification.rb', line 62

def notify(device_id)
  event['event_time'] = Time.now unless event['event_time']
  event['device_id'] = device_id
  # Need to test before we put stuff in log file
  should_speak = ! Notification::FileNotification.text_in_log_file(message_as_text)
  Notification::Wall.notify(event, message_as_text)
  Notification::FileNotification.notify(event, message_as_text)
  Notification::Sensu.notify(event, message_as_text)
  Notification::ESpeakNotification.notify(event, message_as_text) if should_speak && @speech_enabled
end