Class: SmsPromote::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/smspromote/gateway.rb

Constant Summary collapse

DOMAIN =
"gateway.smspromote.de".freeze
SECURE =
"https://#{DOMAIN}"
INSECURE =
"http://#{DOMAIN}"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, options = {}) ⇒ Gateway

create a new gateway using the passed api_key and options

api_key
String

yourt api key (e.g. “KAJSHkjhlskfl32jh24”)

options

a hash of options

:secure (true/false) # use ssl or not
:originator (string) # sender address
:debug (true/false)  # use the dummy service


24
25
26
27
28
29
# File 'lib/smspromote/gateway.rb', line 24

def initialize(api_key, options = {})
  @api_key = api_key
  @options = {
    :secure => false
  }.merge(options)
end

Class Method Details

.encode_params(params = {}) ⇒ Object

returns the encoded params str based on the passed hash



105
106
107
108
109
110
111
# File 'lib/smspromote/gateway.rb', line 105

def self.encode_params(params = {})
  data = []
  params.each do |key, value|
    data << "#{key}=#{URI.escape(value.to_s)}"
  end
  data.join("&")
end

.read_api_key_from_fileObject

returns the api key as string. The api key will be read from the “.smspromote.key” file from the home directroy of the current user



14
15
16
# File 'lib/smspromote/gateway.rb', line 14

def self.read_api_key_from_file
  File.read("#{ENV['HOME']}/.smspromote.key").chomp
end

Instance Method Details

#creditsObject

returns the credits left for the gateway



62
63
64
65
66
# File 'lib/smspromote/gateway.rb', line 62

def credits
  session do |http|
    http.get("/credits/?key=#{@api_key}").body.to_f
  end
end

#debug?Boolean

returns true if the message sending is a dummy operation

Returns:

  • (Boolean)


80
81
82
# File 'lib/smspromote/gateway.rb', line 80

def debug?
  !!@options[:debug]
end

#originator?Boolean

returns true if the messages should contain an orginator

Returns:

  • (Boolean)


90
91
92
# File 'lib/smspromote/gateway.rb', line 90

def originator?
  !!@options[:originator]
end

#parse_response(body) ⇒ Object

returns the response message hash based on the body data



69
70
71
72
73
74
75
76
77
# File 'lib/smspromote/gateway.rb', line 69

def parse_response(body)
  lines = body.split(/\n/)
  {
    :code => lines[0].to_i,
    :message_id => lines[1],
    :cost => (lines[2] || "0").to_f,
    :count => (lines[3] || "0").to_i
  }
end

#routeObject

returns either basic or gold



95
96
97
# File 'lib/smspromote/gateway.rb', line 95

def route
  originator? ? :gold : :basic
end

#secure?Boolean

returns true if the service should use https

Returns:

  • (Boolean)


85
86
87
# File 'lib/smspromote/gateway.rb', line 85

def secure?
  !!@options[:secure]
end

#send_message(message) ⇒ Object

send message using the gateway defaults



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
# File 'lib/smspromote/gateway.rb', line 32

def send_message(message)
  options = {
    :key => @api_key, 
    :to => message.recipient,
    :message => message.body, 
    :route => route,
    :concat => message.multipart? ? 1 : 0,
    :debug => debug? ? 1 : 0,
    :message_id => 1,
    :cost => 1,
    :count => 1
  }
  
  # use oroginator if gold route was used
  if route == :gold
    options[:from] = @options[:originator]
  end
  
  response = session do |http|
    request = Net::HTTP::Post.new("/")
    request.body = Gateway.encode_params(options)
    http.request(request)
  end
  
  data = parse_response(response.body)
  message.after_send(data[:code], data[:message_id], data[:cost], data[:count])
  message
end

#service_urlObject

returns the service url based on the security options



100
101
102
# File 'lib/smspromote/gateway.rb', line 100

def service_url
  URI.parse(secure? ? SECURE : INSECURE)
end