require 'net/http' require 'net/https' module ActionSmser::DeliveryMethods # Very simple implementation of http request to gateway. Options used are # server, use_ssl, username, password # overwrite deliver_path(sms, options) with your own if you have different type of path class Nexmo < SimpleHttp def self.deliver(sms) logger.info "Delivering sms by https" 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] logger.info("Nexmo delivery with #{sms.to_numbers_array.count} recipients") to_original = sms.to sms.delivery_info = [] delivery_reports = [] sms.to_numbers_array.each do |to| deliver_path = self.deliver_path(sms, to, options) response = self.deliver_http_request(sms, options, deliver_path) logger.info "Nexmo delivery http ||| #{deliver_path} ||| #{response.inspect}" 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 delivery_reports << dr end end sms.to = to_original sms.delivery_options[:save_delivery_reports] ? delivery_reports : sms.delivery_info end def self.deliver_path(sms, to, options) "/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}" end # 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 def self.process_delivery_report(params) processable_array = [] if msg_id = params["messageId"] processable_array << {'msg_id' => params["messageId"], 'status' => params['status']} end return processable_array end end end