Class: ActionSmser::DeliveryMethods::Nexmo

Inherits:
SimpleHttp
  • Object
show all
Defined in:
lib/action_smser/delivery_methods/nexmo.rb

Overview

Very simple implementation of http request to gateway. Options used are server, use_ssl, username, password Also optional code (=unicode) is possible. See docs.nexmo.com/index.php/messaging-sms-api/send-message overwrite deliver_path(sms, options) with your own if you have different type of path

Class Method Summary collapse

Methods inherited from SimpleHttp

deliver_http_request

Class Method Details

.deliver(sms) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/action_smser/delivery_methods/nexmo.rb', line 12

def self.deliver(sms)
  options = sms.delivery_options[:nexmo] || {}
  options = options.dup

  options[:server] = 'rest.nexmo.com'
  options[:use_ssl] = true
  options[:status_report_req] ||= sms.delivery_options[:save_delivery_reports]

  sms.delivery_info = []

  sms.to_numbers_array.each do |to|
    deliver_path = self.deliver_path(sms, to, options)
    response = self.deliver_http_request(sms, options, deliver_path)

    ActionSmser::Logger.info "Nexmo delivery http ||| #{deliver_path} ||| #{response.inspect}"
    ActionSmser::Logger.info response.body if !response.blank?

    sms.delivery_info.push(response)

    result = JSON.parse(response.body)["messages"].first

    # Results include sms_id or error code in each line
    if sms.delivery_options[:save_delivery_reports]
      dr = ActionSmser::DeliveryReport.build_from_sms(sms, to, result["message-id"])
      if result["status"].to_i > 0
        dr.status = "SENT_ERROR_#{result["status"]}"
        dr.log += "nexmo_error: #{result["error-text"]}"
      end
      dr.save
      sms.delivery_reports.push(dr)
    end
  end

  sms.delivery_options[:save_delivery_reports] ? sms.delivery_reports : sms.delivery_info
end

.deliver_path(sms, to, options) ⇒ Object



48
49
50
51
52
# File 'lib/action_smser/delivery_methods/nexmo.rb', line 48

def self.deliver_path(sms, to, options)
  path = "/sms/json?username=#{options[:username]}&password=#{options[:password]}&ttl=#{sms.ttl_to_i*1000}&status-report-req=#{options[:status_report_req]}&from=#{sms.from_encoded}&to=#{to}&text=#{sms.body_escaped}"
  path += "&type=#{options[:type]}" if options[:type]
  path
end

.process_delivery_report(request) ⇒ Object

Callback message status handling This has to return array of hashes. In hash msg_id is the key and other params are updated to db



56
57
58
59
60
61
62
63
# File 'lib/action_smser/delivery_methods/nexmo.rb', line 56

def self.process_delivery_report(request)
  params = request.params
  processable_array = []
  if msg_id = params["messageId"]
    processable_array << {'msg_id' => params["messageId"], 'status' => params['status']}
  end
  return processable_array
end