Class: SmsPromote::Gateway
- Inherits:
-
Object
- Object
- SmsPromote::Gateway
- Defined in:
- lib/smspromote/gateway.rb
Constant Summary collapse
Class Method Summary collapse
-
.encode_params(params = {}) ⇒ Object
returns the encoded params str based on the passed hash.
-
.read_api_key_from_file ⇒ Object
returns the api key as string.
Instance Method Summary collapse
-
#credits ⇒ Object
returns the credits left for the gateway.
-
#debug? ⇒ Boolean
returns true if the message sending is a dummy operation.
-
#initialize(api_key, options = {}) ⇒ Gateway
constructor
- 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.
-
#originator? ⇒ Boolean
returns true if the messages should contain an orginator.
-
#parse_response(body) ⇒ Object
returns the response message hash based on the body data.
-
#route ⇒ Object
returns either basic or gold.
-
#secure? ⇒ Boolean
returns true if the service should use https.
-
#send_message(message) ⇒ Object
send message using the gateway defaults.
-
#service_url ⇒ Object
returns the service url based on the security options.
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, = {}) @api_key = api_key @options = { :secure => false }.merge() 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_file ⇒ Object
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
#credits ⇒ Object
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
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
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 |
#route ⇒ Object
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
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 () = { :key => @api_key, :to => .recipient, :message => .body, :route => route, :concat => .multipart? ? 1 : 0, :debug => debug? ? 1 : 0, :message_id => 1, :cost => 1, :count => 1 } # use oroginator if gold route was used if route == :gold [:from] = @options[:originator] end response = session do |http| request = Net::HTTP::Post.new("/") request.body = Gateway.encode_params() http.request(request) end data = parse_response(response.body) .after_send(data[:code], data[:message_id], data[:cost], data[:count]) end |