Class: ActionSmser::DeliveryMethods::SimpleHttp

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

Overview

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 When save_delivery_reports=true it expects collection msg_ids in each line in http response

Direct Known Subclasses

Nexmo, Smstrade

Class Method Summary collapse

Class Method Details

.deliver(sms, options = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/action_smser/delivery_methods/simple_http.rb', line 12

def self.deliver(sms, options = nil)
  options = options ? options : sms.delivery_options[:simple_http]
  deliver_path = self.deliver_path(sms, options)
  response = self.deliver_http_request(sms, options, deliver_path)

  ActionSmser::Logger.info "SimpleHttp delivery ||| #{deliver_path} ||| #{response.inspect}"
  ActionSmser::Logger.info response.body if !response.blank?
  sms.delivery_info = response

  # Results include sms_id or error code in each line

  results = response.body.split("\n")
  if sms.delivery_options[:save_delivery_reports]
    sms.to_numbers_array.each_with_index do |to, i|
      sms.delivery_reports.push(ActionSmser::DeliveryReport.create_from_sms(sms, to, results[i].to_s.strip))
    end
    return sms.delivery_reports
  else
    return results
  end

end

.deliver_http_request(sms, options, path) ⇒ Object

This is also used by other delivery methods (e.g. nexmo)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/action_smser/delivery_methods/simple_http.rb', line 36

def self.deliver_http_request(sms, options, path)
  # http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
  # http://notetoself.vrensk.com/2008/09/verified-https-in-ruby/

  response = nil

  server_port = options[:use_ssl] ? 443 : 80
  http = Net::HTTP.new(options[:server], server_port)
  if options[:use_ssl]
    http.use_ssl = true
  end

  unless Rails.env.test?
    http.start do |http|
      response = http.request(Net::HTTP::Get.new(path)) unless Rails.env.test? #Never request by accident in test environment.
    end
  else
    ActionSmser::Logger.warn "DeliveryMethods does never make real http requests in test environment!"
  end

  response
end

.deliver_path(sms, options) ⇒ Object



59
60
61
# File 'lib/action_smser/delivery_methods/simple_http.rb', line 59

def self.deliver_path(sms, options)
  "/api/sendsms/plain?user=#{options[:username]}&password=#{options[:password]}&sender=#{sms.from_encoded}&SMSText=#{sms.body_encoded_escaped}&GSM=#{sms.to_encoded}"
end