Skip to content
Browse files

Return whether or not sending the message succeeded.

  • Loading branch information...
1 parent 7cb21f3 commit f3b2cf0f78c97961d81ec42b1f355824972514f2 @alloy alloy committed Jul 30, 2010
Showing with 58 additions and 6 deletions.
  1. +17 −3 lib/mollie/sms.rb
  2. +40 −3 spec/sms_spec.rb
  3. +1 −0 spec/spec_helper.rb
View
20 lib/mollie/sms.rb
@@ -2,6 +2,12 @@
require "uri"
require "net/http"
+begin
+ require "rubygems"
+rescue LoadError
+end
+require "active_support"
+
module Mollie
class SMS
GATEWAY_URI = URI.parse("http://www.mollie.nl/xml/sms")
@@ -46,10 +52,18 @@ def deliver
end
class Response
- attr_reader :response
+ attr_reader :http_response
+
+ def initialize(http_response)
+ @http_response = http_response
+ end
+
+ def params
+ @params ||= Hash.from_xml(@http_response.read_body)['response']['item']
+ end
- def initialize(response)
- @response = response
+ def success?
+ @http_response.is_a?(Net::HTTPSuccess) && params['success'] == 'true'
end
end
end
View
43 spec/sms_spec.rb
@@ -77,7 +77,8 @@ class << self
attr_accessor :stubbed_response
def reset!
- @posted = {}
+ @posted = nil
+ @stubbed_response = nil
end
def post_form(url, params)
@@ -115,9 +116,45 @@ class ResponseStub
end
it "returns a Mollie::SMS::Response object, with the Net::HTTP response" do
- Net::HTTP.stubbed_response = ResponseStub.new
+ Net::HTTP.stubbed_response = Net::HTTPOK.new('1.1', '200', 'OK')
response = @sms.deliver
response.should.be.instance_of Mollie::SMS::Response
- response.response.should == Net::HTTP.stubbed_response
+ response.http_response.should == Net::HTTP.stubbed_response
end
end
+
+SUCCESS_BODY = %{
+<?xml version="1.0" ?>
+<response>
+ <item type="sms">
+ <recipients>1</recipients>
+ <success>true</success>
+ <resultcode>10</resultcode>
+ <resultmessage>Message successfully sent.</resultmessage>
+ </item>
+</response>}
+
+describe "A Mollie::SMS::Response instance" do
+ before do
+ @http_response = Net::HTTPOK.new('1.1', '200', 'OK')
+ @http_response.stubs(:read_body).returns(SUCCESS_BODY)
+ @http_response.add_field('Content-type', 'application/xml')
+ @response = Mollie::SMS::Response.new(@http_response)
+ end
+
+ it "returns the Net::HTTP response object" do
+ @response.http_response.should == @http_response
+ end
+
+ it "returns the response body as a hash" do
+ @response.params.should == Hash.from_xml(SUCCESS_BODY)['response']['item']
+ end
+
+ it "returns whether or not it was a success" do
+ @response.should.be.success
+
+ @response.stubs(:params).returns('resultcode' => '20', 'success' => 'false')
+ @response.should.not.be.success
+ end
+
+end
View
1 spec/spec_helper.rb
@@ -1,5 +1,6 @@
require "rubygems"
require "bacon"
+require "mocha"
$:.unshift File.expand_path("../../lib", __FILE__)

0 comments on commit f3b2cf0

Please sign in to comment.
Something went wrong with that request. Please try again.