Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use only string keys/values, which makes it easy to use Net::HTTP.pos…

…t_form.
  • Loading branch information...
commit a2c2f44ebd29ec3b47a12de8817214faeb021af3 1 parent f1ffff1
@alloy alloy authored
Showing with 66 additions and 32 deletions.
  1. +16 −15 lib/mollie/sms.rb
  2. +50 −17 spec/sms_spec.rb
View
31 lib/mollie/sms.rb
@@ -1,13 +1,16 @@
require "digest/md5"
require "uri"
+require "net/http"
module Mollie
class SMS
+ GATEWAY_URI = URI.parse("http://www.mollie.nl/xml/sms")
+
GATEWAYS = {
- :basic => 2,
- :business => 4,
- :business_plus => 1,
- :landline => 8
+ :basic => '2',
+ :business => '4',
+ :business_plus => '1',
+ :landline => '8'
}
class << self
@@ -19,29 +22,27 @@ def password=(password)
def request_params
{
- :username => @username,
- :md5_password => @password,
- :gateway => @gateway,
- :charset => @charset,
- :type => :normal
+ 'username' => @username,
+ 'md5_password' => @password,
+ 'gateway' => @gateway,
+ 'charset' => @charset,
+ 'type' => @type
}
end
end
self.charset = 'UTF-8'
- self.type = :normal
+ self.type = 'normal'
self.gateway = GATEWAYS[:basic]
attr_accessor :telephone_number, :body
def request_params
- self.class.request_params.merge(:recipients => @telephone_number, :message => @body)
+ self.class.request_params.merge('recipients' => @telephone_number, 'message' => @body)
end
- def post_body
- request_params.map do |key, value|
- "#{URI.escape(key.to_s)}=#{URI.escape(value.to_s)}"
- end.join('&')
+ def deliver
+ Net::HTTP.post_form(GATEWAY_URI, request_params)
end
end
end
View
67 spec/sms_spec.rb
@@ -5,6 +5,10 @@
Mollie::SMS.password = 'secret'
describe "Mollie::SMS" do
+ it "holds the gateway uri" do
+ Mollie::SMS::GATEWAY_URI.should == URI.parse("http://www.mollie.nl/xml/sms")
+ end
+
it "holds the service username" do
Mollie::SMS.username.should == 'AstroRadio'
end
@@ -18,14 +22,14 @@
end
it "returns the default message type" do
- Mollie::SMS.type.should == :normal
+ Mollie::SMS.type.should == 'normal'
end
it "holds a list of available gateways" do
- Mollie::SMS::GATEWAYS[:basic].should == 2
- Mollie::SMS::GATEWAYS[:business].should == 4
- Mollie::SMS::GATEWAYS[:business_plus].should == 1
- Mollie::SMS::GATEWAYS[:landline].should == 8
+ Mollie::SMS::GATEWAYS[:basic].should == '2'
+ Mollie::SMS::GATEWAYS[:business].should == '4'
+ Mollie::SMS::GATEWAYS[:business_plus].should == '1'
+ Mollie::SMS::GATEWAYS[:landline].should == '8'
end
it "returns the default gateway to use" do
@@ -34,11 +38,11 @@
it "returns a hash of params for a request" do
Mollie::SMS.request_params.should == {
- :username => 'AstroRadio',
- :md5_password => Mollie::SMS.password,
- :gateway => 2,
- :charset => 'UTF-8',
- :type => :normal
+ 'username' => 'AstroRadio',
+ 'md5_password' => Mollie::SMS.password,
+ 'gateway' => '2',
+ 'charset' => 'UTF-8',
+ 'type' => 'normal'
}
end
end
@@ -58,18 +62,47 @@
@sms.body.should == "The stars tell me you will have chicken noodle soup for breakfast."
end
- it "returns the request params" do
+ it "returns the request params with all string keys and values" do
params = Mollie::SMS.request_params.merge(
- :recipients => '+31612345678',
- :message => "The stars tell me you will have chicken noodle soup for breakfast."
+ 'recipients' => '+31612345678',
+ 'message' => "The stars tell me you will have chicken noodle soup for breakfast."
)
@sms.request_params.should == params
end
+end
+
+module Net
+ class HTTP
+ def self.reset!
+ @posted = {}
+ end
- it "returns a string version of the request params" do
- def @sms.request_params
- [[:key, :value], ["another key", "another value"]]
+ def self.post_form(url, params)
+ @posted = { 'url' => url, 'params' => params }
end
- @sms.post_body.should == "key=value&another%20key=another%20value"
+
+ def self.posted
+ @posted ||= {}
+ end
+ end
+end
+
+describe "When sending a Mollie::SMS message" do
+ before do
+ @sms = Mollie::SMS.new
+ @sms.telephone_number = '+31612345678'
+ @sms.body = "The stars tell me you will have chicken noodle soup for breakfast."
+ end
+
+ after do
+ Net::HTTP.reset!
+ end
+
+ it "posts the post body to the gateway" do
+ @sms.deliver
+ Net::HTTP.posted.should == {
+ 'url' => Mollie::SMS::GATEWAY_URI,
+ 'params' => @sms.request_params
+ }
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.