Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add methods to help with determining the state of a transaction

  • Loading branch information...
commit d6a9b60f31eea4d0cfbe2b4101e9f35d45b6e43a 1 parent dd6b7c7
@tomeric tomeric authored
Showing with 117 additions and 0 deletions.
  1. +31 −0 lib/buckaroo-ideal/status.rb
  2. +86 −0 spec/buckaroo-ideal/status_spec.rb
View
31 lib/buckaroo-ideal/status.rb
@@ -5,6 +5,25 @@ module Ideal
class Status
class UnknownStatusCode < StandardError; end
+ STATES = {
+ completed: %w(071 121 151 171 190 242 243 244 245 246 247 254 255 301
+ 401 461 462 463 464 551 601 701 801),
+ pending: %w(000 001 070 090 091 100 105 120 126 135 136 150 156 157
+ 170 176 177 253 300 400 460 500 550 600 700 710 790 791
+ 792 793 800 811 814 815 831 834),
+ failed: %w(072 073 074 075 076 101 102 103 104 106 122 123 124 125
+ 137 138 139 152 153 155 158 159 172 173 175 178 179 201
+ 203 204 205 206 207 251 252 260 261 262 302 303 304 305
+ 306 309 402 409 410 411 414 421 422 425 466 468 490 491
+ 492 501 552 553 554 555 556 560 581 590 602 605 609 610
+ 612 690 702 703 704 705 706 707 708 711 712 720 721 802
+ 803 804 810 812 813 816 820 821 822 823 824 830 833 835
+ 836 890 891 900 901 910 931 932 933 934 935 940 941 942
+ 943 944 945 946 947 948 949 950 951 952 953 954 955 956
+ 960 961 962 963 964 971 972 973 974 975 976 977 978 980
+ 981 982 983 990 991 992 993 999)
+ }
+
CSV_FILE = File.expand_path('../../../files/statuscodes.csv', __FILE__)
attr_reader :code, :message
@@ -28,6 +47,18 @@ def initialize(code)
end
end
+ def state
+ STATES.each do |state, codes|
+ return state if codes.include? @code
+ end
+
+ :unknown
+ end
+
+ def completed?; state == :completed; end
+ def pending?; state == :pending; end
+ def failed?; state == :failed; end
+
def ==(other)
other.respond_to?(:code) && other.code == code
end
View
86 spec/buckaroo-ideal/status_spec.rb
@@ -1,6 +1,8 @@
require 'spec_helper'
describe Buckaroo::Ideal::Status do
+ let(:status) { Buckaroo::Ideal::Status.new('000') }
+
describe '.status_codes' do
let(:csv_file) { File.expand_path('../../fixtures/statuscodes.csv', __FILE__) }
@@ -43,4 +45,88 @@
Buckaroo::Ideal::Status.new('000').should == Buckaroo::Ideal::Status.new('000')
end
end
+
+ describe '#state' do
+ %w(071 121 151 171 190 242 243 244 245 246 247 254 255 301 401 461 462 463
+ 464 551 601 701 801).each do |code|
+ message = Buckaroo::Ideal::Status.status_codes[code]
+
+ it "returns :completed for code #{code} (#{message})" do
+ status = Buckaroo::Ideal::Status.new(code)
+ status.state.should == :completed
+ end
+ end
+
+ %w(000 001 070 090 091 100 105 120 126 135 136 150 156 157 170 176 177 253
+ 300 400 460 500 550 600 700 710 790 791 792 793 800 811 814 815 831 834).each do |code|
+ message = Buckaroo::Ideal::Status.status_codes[code]
+
+ it "returns :pending for code #{code} (#{message})" do
+ status = Buckaroo::Ideal::Status.new(code)
+ status.state.should == :pending
+ end
+ end
+
+ %w(264 541 345 372 390 392).each do |code|
+ message = Buckaroo::Ideal::Status.status_codes[code]
+
+ it "returns :unknown for code #{code} (#{message})" do
+ status = Buckaroo::Ideal::Status.new(code)
+ status.state.should == :unknown
+ end
+ end
+
+ %w(072 073 074 075 076 101 102 103 104 106 122 123 124 125 137 138 139 152
+ 153 155 158 159 172 173 175 178 179 201 203 204 205 206 207 251 252 260
+ 261 262 302 303 304 305 306 309 402 409 410 411 414 421 422 425 466 468
+ 490 491 492 501 552 553 554 555 556 560 581 590 602 605 609 610 612 690
+ 702 703 704 705 706 707 708 711 712 720 721 802 803 804 810 812 813 816
+ 820 821 822 823 824 830 833 835 836 890 891 900 901 910 931 932 933 934
+ 935 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
+ 960 961 962 963 964 971 972 973 974 975 976 977 978 980 981 982 983 990
+ 991 992 993 999).each do |code|
+ message = Buckaroo::Ideal::Status.status_codes[code]
+
+ it "returns :unknown for code #{code} (#{message})" do
+ status = Buckaroo::Ideal::Status.new(code)
+ status.state.should == :failed
+ end
+ end
+ end
+
+ describe '#completed?' do
+ it 'returns true if the state is completed' do
+ status.stub(:state).and_return(:completed)
+ status.should be_completed
+ end
+
+ it 'returns false if the state is not completed' do
+ status.stub(:state).and_return(:unknown)
+ status.should_not be_completed
+ end
+ end
+
+ describe '#pending?' do
+ it 'returns true if the state is pending' do
+ status.stub(:state).and_return(:pending)
+ status.should be_pending
+ end
+
+ it 'returns false if the state is not pending' do
+ status.stub(:state).and_return(:unknown)
+ status.should_not be_pending
+ end
+ end
+
+ describe '#failed?' do
+ it 'returns true if the state is failed' do
+ status.stub(:state).and_return(:failed)
+ status.should be_failed
+ end
+
+ it 'returns false if the state is not failed' do
+ status.stub(:state).and_return(:unknown)
+ status.should_not be_failed
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.