Class: ActionSmser::DeliveryMethods::Smstrade

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

Overview

Constant Summary collapse

ERROR_CODES =
{
  "10" => "RECEIVER_INVALID",
  "20" => "SENDER_INVALID",
  "30" => "BODY_INVALID",
  "31" => "MESSAGE_TYPE_INVALID",
  "40" => "ROUTE_INVALID",
  "50" => "API_KEY_INVALID",
  "60" => "INSUFFICIENT_FUNDS",
  "70" => "ROUTE_NOT_SUPPORTED",
  "71" => "FEATURE_NOT_POSSIBLE",
  "80" => "HANDOVER_FAILED",
  "100" => "OK"
}

Class Method Summary collapse

Methods inherited from SimpleHttp

deliver_http_request

Class Method Details

.deliver(sms) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/action_smser/delivery_methods/smstrade.rb', line 22

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

  options[:server] = 'gateway.smstrade.de'
  options[:use_ssl] = false
  options[:status_report_req] ||= sms.delivery_options[:save_delivery_reports]

  sms.delivery_info = []

  # SmsTrade might confuse US numbers with German numbers if not prepended with 00 or +.
  formatted_numbers = sms.to_numbers_array.map { |to| ["00", to].join }
  formatted_numbers.each do |to|
    deliver_path = self.deliver_path(sms, to, options)
    response = self.deliver_http_request(sms, options, deliver_path)

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

    sms.delivery_info.push(response)

    result = response.body.split("\n")
    result = {"response-code" => result[0], "message-id" => result[1], "cost" => result[2], "message-count" => result[3]}

    if ERROR_CODES[result["response-code"]].nil?
      status = "UNKNOWN_ERROR"
    else
      status = ERROR_CODES[result["response-code"]]
    end

    # 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 status != "OK"
        dr.status = status
        dr.add_log "smstrade_error_code: #{result["response-code"]}"
      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



67
68
69
70
71
72
73
74
75
# File 'lib/action_smser/delivery_methods/smstrade.rb', line 67

def self.deliver_path(sms, to, options)
  "/?key=#{options[:key]}"+
  "&from=#{sms.from_escaped}"+
  "&to=#{to}"+
  "&message=#{sms.body_escaped}"+
  "&route=#{options[:route]}"+
  "&debug=#{options[:debug] ? 1 : 0}"+
  "&cost=1&message_id=1&count=1&charset=utf-8"
end