Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 2 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
Commits on Feb 22, 2012
Morgan Christiansson Add bank account validation service ef537c5
Morgan Christiansson Handle errors 17acbfc
View
1  Gemfile
@@ -10,4 +10,5 @@ group :development do
gem "bundler"
gem "jeweler", "~> 1.6.2"
gem "rcov", ">= 0"
+ gem "webmock", :require => false
end
View
5 Gemfile.lock
@@ -3,6 +3,7 @@ GEM
specs:
activesupport (3.1.1)
multi_json (~> 1.0)
+ addressable (2.2.7)
crack (0.1.8)
diff-lcs (1.1.3)
git (1.2.5)
@@ -24,6 +25,9 @@ GEM
rspec-expectations (2.8.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.8.0)
+ webmock (1.8.0)
+ addressable (>= 2.2.7)
+ crack (>= 0.1.7)
PLATFORMS
ruby
@@ -36,3 +40,4 @@ DEPENDENCIES
jeweler (~> 1.6.2)
rcov
rspec
+ webmock
View
92 lib/postcode_anywhere/bank_account_validation.rb
@@ -0,0 +1,92 @@
+require 'json'
+#require 'active_support/inflector/methods'
+require 'active_support/core_ext/string/inflections'
+
+module PostcodeAnywhere
+ class BankAccountValidation
+ include HTTParty
+ base_uri 'https://services.postcodeanywhere.co.uk/BankAccountValidation/Interactive/Validate/v2.00/json3.ws'
+ format :html
+ def initialize(key)
+ self.class.default_params :Key => key
+ end
+ 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[0]
+ end
+ end
+
+ class BankAccountResult
+ ATTRIBUTES = [:is_correct,
+ :is_direct_debit_capable,
+ :status_information,
+ :corrected_sort_code,
+ :corrected_account_number,
+ :iban,
+ :bank,
+ :bank_bic,
+ :branch,
+ :branch_bic,
+ :contact_address_line1,
+ :contact_address_line2,
+ :contact_post_town,
+ :contact_postcode,
+ :contact_phone,
+ :contact_fax,
+ :faster_payments_supported,
+ :chaps_supported]
+ attr_accessor *ATTRIBUTES
+
+ def initialize attributes
+ attributes.each do |k, v|
+ send "#{k.underscore}=", v
+ end
+ end
+
+ def correct?
+ is_correct
+ end
+ def direct_debit_capable?
+ is_direct_debit_capable
+ end
+ def faster_payments_supported?
+ faster_payments_supported
+ end
+ 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
3  lib/ruby-postcodeanywhere.rb
@@ -2,6 +2,7 @@
require 'active_support/core_ext/module/attribute_accessors'
module PostcodeAnywhere
+ autoload :BankAccountValidation, 'postcode_anywhere/bank_account_validation'
# Account codes to access the PostcodeAnywhere Service
mattr_accessor :account_code
@@ -137,5 +138,5 @@ class AddressListItem
attr_accessor :id, :street_address, :place
end
-
end
+
View
94 spec/lib/postcode_anywhere/bank_account_validation_spec.rb
@@ -0,0 +1,94 @@
+require 'spec_helper'
+
+WebMock.disable_net_connect!
+
+describe PostcodeAnywhere::BankAccountValidation do
+ subject { PostcodeAnywhere::BankAccountValidation.new 'key' }
+ let :json_response do
+ <<-JSON
+{
+ "Items": [
+ {
+ "IsCorrect": false,
+ "IsDirectDebitCapable": false,
+ "StatusInformation": "UnknownSortCode",
+ "CorrectedSortCode": "",
+ "CorrectedAccountNumber": "",
+ "IBAN": "",
+ "Bank": "",
+ "BankBIC": "",
+ "Branch": "",
+ "BranchBIC": "",
+ "ContactAddressLine1": "",
+ "ContactAddressLine2": "",
+ "ContactPostTown": "",
+ "ContactPostcode": "",
+ "ContactPhone": "",
+ "ContactFax": "",
+ "FasterPaymentsSupported": false,
+ "CHAPSSupported": false
+ }
+ ]
+}
+ 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
+ ret = make_bav_request :response => json_response
+
+ ret.should_not be_correct
+ ret.should_not be_direct_debit_capable
+ ret.status_information.should == 'UnknownSortCode'
+ ret.corrected_sort_code.should == ''
+ ret.corrected_account_number.should == ''
+ ret.iban.should == ''
+ ret.bank.should == ''
+ ret.bank_bic.should == ''
+ ret.branch.should == ''
+ ret.branch_bic.should == ''
+ ret.contact_address_line1.should == ''
+ ret.contact_address_line2.should == ''
+ ret.contact_post_town.should == ''
+ ret.contact_postcode.should == ''
+ ret.contact_phone.should == ''
+ ret.contact_fax.should == ''
+ ret.faster_payments_supported.should == false
+ ret.should_not be_faster_payments_supported
+ 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
+
View
1  spec/spec_helper.rb
@@ -1,5 +1,6 @@
require 'rubygems'
require 'bundler/setup'
+require 'webmock/rspec'
require 'ruby-postcodeanywhere'

No commit comments for this range

Something went wrong with that request. Please try again.