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

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
34
35
36
# File 'lib/action_smser/delivery_methods/simple_http.rb', line 12

def self.deliver(sms, options = nil)
  logger.info "Delivering sms by https"

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

  logger.info "SimpleHttp delivery ||| #{deliver_path} ||| #{response.inspect}"
  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]
    delivery_reports = []
    sms.to_numbers_array.each_with_index do |to, i|
      delivery_reports << ActionSmser::DeliveryReport.create_from_sms(sms, to, results[i].to_s.strip)
    end
    return 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)



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

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
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  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
    logger.warn "DeliveryMethods does never make real http requests in test environment!"
  end

  response
end

.deliver_path(sms, options) ⇒ Object



63
64
65
# File 'lib/action_smser/delivery_methods/simple_http.rb', line 63

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

.loggerObject



67
68
69
# File 'lib/action_smser/delivery_methods/simple_http.rb', line 67

def self.logger
  ActionSmser::Logger
end