Permalink
Browse files

Make SMS instances comparable and add #deliver! which raises a Delive…

…ryFailure when it fails.
  • Loading branch information...
1 parent a901b65 commit 432fd0df030fb625de342a8fbfb26083c3790f4f @alloy alloy committed Aug 3, 2010
Showing with 53 additions and 2 deletions.
  1. +27 −1 lib/mollie/sms.rb
  2. +26 −1 spec/sms_spec.rb
View
@@ -23,6 +23,18 @@ class StandardError < ::StandardError; end
class ValidationError < StandardError; end
class MissingRequiredParam < StandardError; end
+ class DeliveryFailure < StandardError
+ attr_reader :sms, :response
+
+ def initialize(sms, response)
+ @sms, @response = sms, response
+ end
+
+ def message
+ "(#{@response.message}) #{@sms.to_s}"
+ end
+ end
+
REQUIRED_PARAMS = %w{ username md5_password originator gateway charset type recipients message }
class << self
@@ -72,8 +84,16 @@ def body=(body)
@params['message'] = body
end
+ def ==(other)
+ other.is_a?(SMS) && other.params == params
+ end
+
+ def to_s
+ %{from: <#{params['originator']}> to: <#{telephone_number}> body: "#{body}"}
+ end
+
def inspect
- "#<#{self.class.name} from: <#{@params['originator']}> to: <#{telephone_number}> body: \"#{body}\" >"
+ %{#<#{self.class.name} #{to_s}>}
end
def deliver
@@ -89,6 +109,12 @@ def deliver
end
end
+ def deliver!
+ response = deliver
+ raise DeliveryFailure.new(self, response) unless response.success?
+ response
+ end
+
def validate_params!
params.slice(*REQUIRED_PARAMS).each do |key, value|
raise MissingRequiredParam, "The required parameter `#{key}' is missing." if value.blank?
View
@@ -89,6 +89,16 @@
)
@sms.params.should == params
end
+
+ it "checks equality by comparing the params" do
+ @sms.should == Mollie::SMS.new('+31612345678', "The stars tell me you will have chicken noodle soup for breakfast.")
+ @sms.should.not == Mollie::SMS.new('+31612345678', "Different message")
+ @sms.should.not == Mollie::SMS.new('+31612345678', "The stars tell me you will have chicken noodle soup for breakfast.", 'originator' => 'Some being')
+ end
+
+ it "returns a string representation of itself" do
+ @sms.to_s.should == 'from: <Astro INC> to: <+31612345678> body: "The stars tell me you will have chicken noodle soup for breakfast."'
+ end
end
describe "When sending a Mollie::SMS message" do
@@ -118,10 +128,25 @@
it "returns a Mollie::SMS::Response object, with the Net::HTTP response" do
Net::HTTP.stubbed_response = Net::HTTPOK.new('1.1', '200', 'OK')
Net::HTTP.stubbed_response.stubs(:read_body).returns(SUCCESS_BODY)
- response = @sms.deliver
+ response = @sms.deliver!
response.should.be.instance_of Mollie::SMS::Response
response.http_response.should == Net::HTTP.stubbed_response
end
+
+ it "raises a Mollie::SMS::DeliveryFailure exception when sending, through the #deliver! method, fails" do
+ Net::HTTP.stubbed_response = Net::HTTPOK.new('1.1', '200', 'OK')
+ Net::HTTP.stubbed_response.stubs(:read_body).returns(FAILURE_BODY)
+
+ exception = nil
+ begin
+ @sms.deliver!
+ rescue Mollie::SMS::DeliveryFailure => exception
+ end
+
+ exception.sms.should == @sms
+ exception.response.http_response.should == Net::HTTP.stubbed_response
+ exception.message.should == "(No username given.) #{@sms.to_s}"
+ end
end
describe "A Mollie::SMS::Response instance, for a succeeded request" do

0 comments on commit 432fd0d

Please sign in to comment.