class Notification::Send

Public Class Methods

new(options = {}) click to toggle source
# File lib/gaddygaddy-client/notification.rb, line 29
def initialize(options = {})
  I18n.load_path += Dir[File.join(File.dirname(__FILE__), '..','..','locales', '*.yml').to_s]
  @speech_enabled = options[:speech_enabled]
end

Public Instance Methods

event() click to toggle source
# File lib/gaddygaddy-client/notification.rb, line 43
def event
  @event
end
event=(event) click to toggle source
# File lib/gaddygaddy-client/notification.rb, line 39
def event= event
  @event = JSON.parse(event)
end
language() click to toggle source

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

# File lib/gaddygaddy-client/notification.rb, line 35
def language
  'en'
end
message_as_text() click to toggle source
# File lib/gaddygaddy-client/notification.rb, line 47
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) click to toggle source

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

# File lib/gaddygaddy-client/notification.rb, line 64
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)
  begin
    with_retries(:limit => 3, :sleep=> 10) {Notification::Sensu.notify(event, message_as_text)}
  rescue Exception => e
    logger.error "Could not connect with gaddlet to send message. Error is #{e.message}"
  end
  Notification::ESpeakNotification.notify(event, message_as_text) if should_speak && @speech_enabled
end