Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 65 lines (50 sloc) 2.289 kb
cb3c527 @holli updated some comments etc
authored
1 require 'net/http'
2 require 'net/https'
3
f5330d5 @holli delivery methods added for sms objects
authored
4 module ActionSmser::DeliveryMethods
5
6 # Very simple implementation of http request to gateway. Options used are
7 # server, use_ssl, username, password
8 # overwrite deliver_path(sms, options) with your own if you have different type of path
797c568 @holli added nexmo deliverymethod
authored
9 # When save_delivery_reports=true it expects collection msg_ids in each line in http response
f5330d5 @holli delivery methods added for sms objects
authored
10 class SimpleHttp
ff397aa @syntaxTerr0r Refactor logger.info call
syntaxTerr0r authored
11
bff9986 @holli simple http update
authored
12 def self.deliver(sms, options = nil)
13 options = options ? options : sms.delivery_options[:simple_http]
627ab0f @holli simple_http tests
authored
14 deliver_path = self.deliver_path(sms, options)
57134bb @holli small bug fix in simple_http
authored
15 response = self.deliver_http_request(sms, options, deliver_path)
627ab0f @holli simple_http tests
authored
16
ff397aa @syntaxTerr0r Refactor logger.info call
syntaxTerr0r authored
17 ActionSmser::Logger.info "SimpleHttp delivery ||| #{deliver_path} ||| #{response.inspect}"
18 ActionSmser::Logger.info response.body if !response.blank?
627ab0f @holli simple_http tests
authored
19 sms.delivery_info = response
20
10f3b06 @holli using classes instead of lambdas
authored
21 # Results include sms_id or error code in each line
22
627ab0f @holli simple_http tests
authored
23 results = response.body.split("\n")
10f3b06 @holli using classes instead of lambdas
authored
24 if sms.delivery_options[:save_delivery_reports]
25 sms.to_numbers_array.each_with_index do |to, i|
a69b273 @holli delivery_report fies to delayed_job
authored
26 sms.delivery_reports.push(ActionSmser::DeliveryReport.create_from_sms(sms, to, results[i].to_s.strip))
627ab0f @holli simple_http tests
authored
27 end
a69b273 @holli delivery_report fies to delayed_job
authored
28 return sms.delivery_reports
10f3b06 @holli using classes instead of lambdas
authored
29 else
30 return results
627ab0f @holli simple_http tests
authored
31 end
32
f5330d5 @holli delivery methods added for sms objects
authored
33 end
34
797c568 @holli added nexmo deliverymethod
authored
35 # This is also used by other delivery methods (e.g. nexmo)
57134bb @holli small bug fix in simple_http
authored
36 def self.deliver_http_request(sms, options, path)
f5330d5 @holli delivery methods added for sms objects
authored
37 # http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
38 # http://notetoself.vrensk.com/2008/09/verified-https-in-ruby/
39
b7efd30 @holli small bug fix in simple_http
authored
40 response = nil
ff397aa @syntaxTerr0r Refactor logger.info call
syntaxTerr0r authored
41
f5330d5 @holli delivery methods added for sms objects
authored
42 server_port = options[:use_ssl] ? 443 : 80
43 http = Net::HTTP.new(options[:server], server_port)
44 if options[:use_ssl]
45 http.use_ssl = true
46 end
47
48 unless Rails.env.test?
49 http.start do |http|
57134bb @holli small bug fix in simple_http
authored
50 response = http.request(Net::HTTP::Get.new(path)) unless Rails.env.test? #Never request by accident in test environment.
f5330d5 @holli delivery methods added for sms objects
authored
51 end
52 else
081e0ab @holli changed rest of logger to ActionSmser::Logger.info , as in #7
authored
53 ActionSmser::Logger.warn "DeliveryMethods does never make real http requests in test environment!"
f5330d5 @holli delivery methods added for sms objects
authored
54 end
3d61cd5 @holli delivery report fixes
authored
55
627ab0f @holli simple_http tests
authored
56 response
f5330d5 @holli delivery methods added for sms objects
authored
57 end
58
59 def self.deliver_path(sms, options)
60 "/api/sendsms/plain?user=#{options[:username]}&password=#{options[:password]}&sender=#{sms.from_encoded}&SMSText=#{sms.body_encoded_escaped}&GSM=#{sms.to_encoded}"
61 end
62
63 end
64 end
Something went wrong with that request. Please try again.