Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 6 files changed
  • 0 comments
  • 1 contributor
Feb 22, 2012
Morgan Christiansson Add bank account validation service ef537c5
Morgan Christiansson Handle errors 17acbfc
1  Gemfile
@@ -10,4 +10,5 @@ group :development do
10 10 gem "bundler"
11 11 gem "jeweler", "~> 1.6.2"
12 12 gem "rcov", ">= 0"
  13 + gem "webmock", :require => false
13 14 end
5 Gemfile.lock
@@ -3,6 +3,7 @@ GEM
3 3 specs:
4 4 activesupport (3.1.1)
5 5 multi_json (~> 1.0)
  6 + addressable (2.2.7)
6 7 crack (0.1.8)
7 8 diff-lcs (1.1.3)
8 9 git (1.2.5)
@@ -24,6 +25,9 @@ GEM
24 25 rspec-expectations (2.8.0)
25 26 diff-lcs (~> 1.1.2)
26 27 rspec-mocks (2.8.0)
  28 + webmock (1.8.0)
  29 + addressable (>= 2.2.7)
  30 + crack (>= 0.1.7)
27 31
28 32 PLATFORMS
29 33 ruby
@@ -36,3 +40,4 @@ DEPENDENCIES
36 40 jeweler (~> 1.6.2)
37 41 rcov
38 42 rspec
  43 + webmock
92 lib/postcode_anywhere/bank_account_validation.rb
... ... @@ -0,0 +1,92 @@
  1 +require 'json'
  2 +#require 'active_support/inflector/methods'
  3 +require 'active_support/core_ext/string/inflections'
  4 +
  5 +module PostcodeAnywhere
  6 + class BankAccountValidation
  7 + include HTTParty
  8 + base_uri 'https://services.postcodeanywhere.co.uk/BankAccountValidation/Interactive/Validate/v2.00/json3.ws'
  9 + format :html
  10 + def initialize(key)
  11 + self.class.default_params :Key => key
  12 + end
  13 + def validate(sort_code, account_number)
  14 + http_response = self.class.get('', :query => {:SortCode => sort_code, :AccountNumber => account_number})
  15 +
  16 + r = unwrap_response(http_response)
  17 +
  18 + if r["Error"]
  19 + raise BankAccountException, r
  20 + else
  21 + BankAccountResult.new(r)
  22 + end
  23 + end
  24 +
  25 + protected
  26 + def unwrap_response(http_response)
  27 + r = JSON.parse(http_response)
  28 + raise unless r.length == 1
  29 + r = r["Items"]
  30 + raise unless r.length == 1
  31 +
  32 + r[0]
  33 + end
  34 + end
  35 +
  36 + class BankAccountResult
  37 + ATTRIBUTES = [:is_correct,
  38 + :is_direct_debit_capable,
  39 + :status_information,
  40 + :corrected_sort_code,
  41 + :corrected_account_number,
  42 + :iban,
  43 + :bank,
  44 + :bank_bic,
  45 + :branch,
  46 + :branch_bic,
  47 + :contact_address_line1,
  48 + :contact_address_line2,
  49 + :contact_post_town,
  50 + :contact_postcode,
  51 + :contact_phone,
  52 + :contact_fax,
  53 + :faster_payments_supported,
  54 + :chaps_supported]
  55 + attr_accessor *ATTRIBUTES
  56 +
  57 + def initialize attributes
  58 + attributes.each do |k, v|
  59 + send "#{k.underscore}=", v
  60 + end
  61 + end
  62 +
  63 + def correct?
  64 + is_correct
  65 + end
  66 + def direct_debit_capable?
  67 + is_direct_debit_capable
  68 + end
  69 + def faster_payments_supported?
  70 + faster_payments_supported
  71 + end
  72 + def chaps_supported?
  73 + chaps_supported
  74 + end
  75 + end
  76 +
  77 + class BankAccountException < StandardError
  78 + ATTRIBUTES = [:error,
  79 + :description,
  80 + :cause,
  81 + :resolution]
  82 + attr_accessor *ATTRIBUTES
  83 +
  84 + def initialize(attributes)
  85 + self.error = attributes.delete("Error").to_i
  86 + attributes.each do |k,v|
  87 + send "#{k.underscore}=", v
  88 + end
  89 + end
  90 + end
  91 +end
  92 +
3  lib/ruby-postcodeanywhere.rb
@@ -2,6 +2,7 @@
2 2 require 'active_support/core_ext/module/attribute_accessors'
3 3
4 4 module PostcodeAnywhere
  5 + autoload :BankAccountValidation, 'postcode_anywhere/bank_account_validation'
5 6
6 7 # Account codes to access the PostcodeAnywhere Service
7 8 mattr_accessor :account_code
@@ -137,5 +138,5 @@ class AddressListItem
137 138 attr_accessor :id, :street_address, :place
138 139
139 140 end
140   -
141 141 end
  142 +
94 spec/lib/postcode_anywhere/bank_account_validation_spec.rb
... ... @@ -0,0 +1,94 @@
  1 +require 'spec_helper'
  2 +
  3 +WebMock.disable_net_connect!
  4 +
  5 +describe PostcodeAnywhere::BankAccountValidation do
  6 + subject { PostcodeAnywhere::BankAccountValidation.new 'key' }
  7 + let :json_response do
  8 + <<-JSON
  9 +{
  10 + "Items": [
  11 + {
  12 + "IsCorrect": false,
  13 + "IsDirectDebitCapable": false,
  14 + "StatusInformation": "UnknownSortCode",
  15 + "CorrectedSortCode": "",
  16 + "CorrectedAccountNumber": "",
  17 + "IBAN": "",
  18 + "Bank": "",
  19 + "BankBIC": "",
  20 + "Branch": "",
  21 + "BranchBIC": "",
  22 + "ContactAddressLine1": "",
  23 + "ContactAddressLine2": "",
  24 + "ContactPostTown": "",
  25 + "ContactPostcode": "",
  26 + "ContactPhone": "",
  27 + "ContactFax": "",
  28 + "FasterPaymentsSupported": false,
  29 + "CHAPSSupported": false
  30 + }
  31 + ]
  32 +}
  33 + JSON
  34 + end
  35 +
  36 + let :json_error_response do
  37 + <<-JSON
  38 +{
  39 + "Items": [
  40 + {
  41 + "Error": "1002",
  42 + "Description": "SortCode Invalid",
  43 + "Cause": "SortCode Invalid",
  44 + "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."
  45 + }
  46 + ]
  47 +}
  48 + JSON
  49 + end
  50 +
  51 + it "sends a valid request when validating a bank account" do
  52 + ret = make_bav_request :response => json_response
  53 +
  54 + ret.should_not be_correct
  55 + ret.should_not be_direct_debit_capable
  56 + ret.status_information.should == 'UnknownSortCode'
  57 + ret.corrected_sort_code.should == ''
  58 + ret.corrected_account_number.should == ''
  59 + ret.iban.should == ''
  60 + ret.bank.should == ''
  61 + ret.bank_bic.should == ''
  62 + ret.branch.should == ''
  63 + ret.branch_bic.should == ''
  64 + ret.contact_address_line1.should == ''
  65 + ret.contact_address_line2.should == ''
  66 + ret.contact_post_town.should == ''
  67 + ret.contact_postcode.should == ''
  68 + ret.contact_phone.should == ''
  69 + ret.contact_fax.should == ''
  70 + ret.faster_payments_supported.should == false
  71 + ret.should_not be_faster_payments_supported
  72 + ret.chaps_supported.should == false
  73 + ret.should_not be_chaps_supported
  74 + end
  75 +
  76 + it "handles errors" do
  77 + begin
  78 + make_bav_request :response => json_error_response
  79 + fail 'should raise exception'
  80 + rescue PostcodeAnywhere::BankAccountException => e
  81 + e.error.should == 1002
  82 + e.description.should == "SortCode Invalid"
  83 + e.cause.should == "SortCode Invalid"
  84 + 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."
  85 + end
  86 + end
  87 + def make_bav_request(options)
  88 + stub_request(:get,
  89 + 'https://services.postcodeanywhere.co.uk/BankAccountValidation/Interactive/Validate/v2.00/json3.ws?AccountNumber=account_number&Key=key&SortCode=sort_code'
  90 + ).to_return(:body => options[:response])
  91 + ret = subject.validate('sort_code', 'account_number')
  92 + end
  93 +end
  94 +
1  spec/spec_helper.rb
... ... @@ -1,5 +1,6 @@
1 1 require 'rubygems'
2 2 require 'bundler/setup'
  3 +require 'webmock/rspec'
3 4
4 5 require 'ruby-postcodeanywhere'
5 6

No commit comments for this range

Something went wrong with that request. Please try again.