lib/flapjack/gateways/sms_messagenet.rb in flapjack-0.7.27 vs lib/flapjack/gateways/sms_messagenet.rb in flapjack-0.7.28
- old
+ new
@@ -1,60 +1,62 @@
#!/usr/bin/env ruby
require 'em-synchrony'
require 'em-synchrony/em-http'
+require 'active_support/inflector'
module Flapjack
module Gateways
class SmsMessagenet
- MESSAGENET_URL = 'https://www.messagenet.com.au/dotnet/Lodge.asmx/LodgeSMSMessage'
+ MESSAGENET_DEFAULT_URL = 'https://www.messagenet.com.au/dotnet/Lodge.asmx/LodgeSMSMessage'
class << self
def start
@sent = 0
end
def perform(notification)
@logger.debug "Woo, got a notification to send out: #{notification.inspect}"
- notification_type = notification['notification_type']
- contact_first_name = notification['contact_first_name']
- contact_last_name = notification['contact_last_name']
- state = notification['state']
- summary = notification['summary']
- time = notification['time']
- entity, check = notification['event_id'].split(':', 2)
+ endpoint = @config["endpoint"] || MESSAGENET_DEFAULT_URL
+ username = @config["username"]
+ password = @config["password"]
- headline_map = {'problem' => 'PROBLEM: ',
- 'recovery' => 'RECOVERY: ',
- 'acknowledgement' => 'ACK: ',
- 'test' => 'TEST NOTIFICATION: ',
- 'unknown' => '',
- '' => '',
- }
+ @notification_type = notification['notification_type']
+ @rollup = notification['rollup']
+ @rollup_alerts = notification['rollup_alerts']
+ @state = notification['state']
+ @summary = notification['summary']
+ @time = notification['time']
+ @entity_name, @check = notification['event_id'].split(':', 2)
+ address = notification['address']
+ notification_id = notification['id']
- headline = headline_map[notification_type] || ''
+ message_type = case
+ when @rollup
+ 'rollup'
+ else
+ 'alert'
+ end
- message = "#{headline}'#{check}' on #{entity}"
- message += " is #{state.upcase}" unless ['acknowledgement', 'test'].include?(notification_type)
- message += " at #{Time.at(time).strftime('%-d %b %H:%M')}, #{summary}"
+ sms_template = ERB.new(File.read(File.dirname(__FILE__) +
+ "/sms_messagenet/#{message_type}.text.erb"), nil, '-')
+ bnd = binding
+ message = sms_template.result(bnd).chomp
+
# TODO log error and skip instead of raising errors
if @config.nil? || (@config.respond_to?(:empty?) && @config.empty?)
@logger.error "Messagenet config is missing"
return
end
errors = []
- username = @config["username"]
- password = @config["password"]
- address = notification['address']
safe_message = truncate(message, 159)
- notification_id = notification['id']
[[username, "Messagenet username is missing"],
[password, "Messagenet password is missing"],
[address, "SMS address is missing"],
[notification_id, "Notification id is missing"]].each do |val_err|
@@ -71,10 +73,10 @@
query = {'Username' => username,
'Pwd' => password,
'PhoneNumber' => address,
'PhoneMessage' => safe_message}
- http = EM::HttpRequest.new(MESSAGENET_URL).get(:query => query)
+ http = EM::HttpRequest.new(endpoint).get(:query => query)
@logger.debug "server response: #{http.response}"
status = (http.nil? || http.response_header.nil?) ? nil : http.response_header.status
if (status >= 200) && (status <= 206)