Class: Notification::Send

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

Instance Method Summary collapse

Methods included from Retriable

#extract_options, set_test_mode, #with_retries

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.



29
30
31
32
# 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

Instance Method Details

#eventObject



43
44
45
# File 'lib/gaddygaddy-client/notification.rb', line 43

def event
  @event
end

#event=(event) ⇒ Object



39
40
41
# File 'lib/gaddygaddy-client/notification.rb', line 39

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



35
36
37
# File 'lib/gaddygaddy-client/notification.rb', line 35

def language
  'en'
end

#message_as_textObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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