Skip to content
Browse files

Handle errors

  • Loading branch information...
1 parent ef537c5 commit 17acbfc834f717ad41b3c372940e3f29694554b2 Morgan Christiansson committed Feb 22, 2012
View
40 lib/postcode_anywhere/bank_account_validation.rb
@@ -13,18 +13,23 @@ def initialize(key)
def validate(sort_code, account_number)
http_response = self.class.get('', :query => {:SortCode => sort_code, :AccountNumber => account_number})
+ r = unwrap_response(http_response)
+
+ if r["Error"]
+ raise BankAccountException, r
+ else
+ BankAccountResult.new(r)
+ end
+ end
+
+ protected
+ def unwrap_response(http_response)
r = JSON.parse(http_response)
raise unless r.length == 1
r = r["Items"]
raise unless r.length == 1
- r = r[0]
-
- r2 = BankAccountResult.new
- r.each do |k, v|
- r2.send "#{k.underscore}=", v
- end
- r2
+ r[0]
end
end
@@ -49,6 +54,12 @@ class BankAccountResult
:chaps_supported]
attr_accessor *ATTRIBUTES
+ def initialize attributes
+ attributes.each do |k, v|
+ send "#{k.underscore}=", v
+ end
+ end
+
def correct?
is_correct
end
@@ -62,5 +73,20 @@ def chaps_supported?
chaps_supported
end
end
+
+ class BankAccountException < StandardError
+ ATTRIBUTES = [:error,
+ :description,
+ :cause,
+ :resolution]
+ attr_accessor *ATTRIBUTES
+
+ def initialize(attributes)
+ self.error = attributes.delete("Error").to_i
+ attributes.each do |k,v|
+ send "#{k.underscore}=", v
+ end
+ end
+ end
end
View
38 spec/lib/postcode_anywhere/bank_account_validation_spec.rb
@@ -33,11 +33,23 @@
JSON
end
+ let :json_error_response do
+ <<-JSON
+{
+ "Items": [
+ {
+ "Error": "1002",
+ "Description": "SortCode Invalid",
+ "Cause": "SortCode Invalid",
+ "Resolution": "The SortCode parameter should be 6 digits in the form 00-00-00 or 000000. It should be prefixed with leading 0s if necessary."
+ }
+ ]
+}
+ JSON
+ end
+
it "sends a valid request when validating a bank account" do
- stub_request(:get,
- 'https://services.postcodeanywhere.co.uk/BankAccountValidation/Interactive/Validate/v2.00/json3.ws?AccountNumber=account_number&Key=key&SortCode=sort_code'
- ).to_return(:body => json_response)
- ret = subject.validate('sort_code', 'account_number')
+ ret = make_bav_request :response => json_response
ret.should_not be_correct
ret.should_not be_direct_debit_capable
@@ -60,5 +72,23 @@
ret.chaps_supported.should == false
ret.should_not be_chaps_supported
end
+
+ it "handles errors" do
+ begin
+ make_bav_request :response => json_error_response
+ fail 'should raise exception'
+ rescue PostcodeAnywhere::BankAccountException => e
+ e.error.should == 1002
+ e.description.should == "SortCode Invalid"
+ e.cause.should == "SortCode Invalid"
+ e.resolution.should == "The SortCode parameter should be 6 digits in the form 00-00-00 or 000000. It should be prefixed with leading 0s if necessary."
+ end
+ end
+ def make_bav_request(options)
+ stub_request(:get,
+ 'https://services.postcodeanywhere.co.uk/BankAccountValidation/Interactive/Validate/v2.00/json3.ws?AccountNumber=account_number&Key=key&SortCode=sort_code'
+ ).to_return(:body => options[:response])
+ ret = subject.validate('sort_code', 'account_number')
+ end
end

0 comments on commit 17acbfc

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