banking_data_validator 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +8 -8
  2. data/README.md +2 -2
  3. data/banking_data_validator.gemspec +3 -2
  4. data/lib/banking_data_validator/bank/base.rb +29 -0
  5. data/lib/banking_data_validator/bank/bradesco.rb +25 -0
  6. data/lib/banking_data_validator/bank/brasil.rb +24 -0
  7. data/lib/banking_data_validator/bank/caixa.rb +25 -0
  8. data/lib/banking_data_validator/bank/common.rb +9 -0
  9. data/lib/banking_data_validator/bank/hsbc.rb +25 -0
  10. data/lib/banking_data_validator/bank/itau.rb +37 -0
  11. data/lib/banking_data_validator/bank/santander.rb +31 -0
  12. data/lib/banking_data_validator/bank.rb +23 -0
  13. data/lib/banking_data_validator/validator.rb +35 -0
  14. data/lib/banking_data_validator/version.rb +3 -0
  15. data/lib/banking_data_validator.rb +2 -52
  16. data/spec/banking_data_validator/bank/bradesco_spec.rb +22 -0
  17. data/spec/banking_data_validator/bank/brasil_spec.rb +29 -0
  18. data/spec/banking_data_validator/bank/caixa_spec.rb +20 -0
  19. data/spec/banking_data_validator/bank/common_spec.rb +16 -0
  20. data/spec/banking_data_validator/bank/hsbc_spec.rb +23 -0
  21. data/spec/banking_data_validator/bank/itau_spec.rb +20 -0
  22. data/spec/banking_data_validator/bank/santander_spec.rb +19 -0
  23. data/spec/banking_data_validator/bank_spec.rb +17 -0
  24. data/spec/banking_data_validator/validator_spec.rb +56 -0
  25. data/spec/spec_helper.rb +2 -4
  26. metadata +37 -23
  27. data/lib/banking_data_validator/banco_base.rb +0 -17
  28. data/lib/banking_data_validator/banco_bradesco.rb +0 -22
  29. data/lib/banking_data_validator/banco_do_brasil.rb +0 -21
  30. data/lib/banking_data_validator/banco_itau.rb +0 -35
  31. data/lib/banking_data_validator/banco_santander.rb +0 -19
  32. data/spec/banking_data_validator/banco_bradesco_spec.rb +0 -16
  33. data/spec/banking_data_validator/banco_do_brasil_spec.rb +0 -22
  34. data/spec/banking_data_validator/banco_itau_spec.rb +0 -13
  35. data/spec/banking_data_validator/banco_santander_spec.rb +0 -13
  36. data/spec/banking_data_validator.rb +0 -62
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZGU1YjhiOGU3Njk2YWJhZGUxZjhkMWJjOWVlZDkxNGQ5NWFmOTRlOA==
4
+ ZWI4YjhhZjU0NTA5ZWM2MzA2YmU4MzIxNjgxMWM1Y2Q0ZTk5OGZiYw==
5
5
  data.tar.gz: !binary |-
6
- OTBhODk1YmRhNmE0YTU4NTVkNzIwMzk3ODdmZGZmMmE0ZTU2MzcxOA==
6
+ MDJlNDJhMTM4MTQyMTk1OTcyODI5NWMzM2MwMGExNGIwY2VjNjg2Nw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MGE0MWQ4ZmI4YmVjYWIwNTg4YzIyMjNmNTlhMjlhMTBjNmRjMjk4NWNmNjVm
10
- MDU1NTQwYTNiYjA2ZTU4ZDE3ZTQxMjUzMmM4MzU4MDQwNTIyNGI1YTUyNGNm
11
- YzBjNTgwYTdkMzI3MzMwYzJmMmI2NzAxYWUxNDI1MGMxYzZiYmE=
9
+ Mzc4YjZlMzc2ZDRhNzExNTQxZDVmNGYyMTU4NWUxOTI1NjkxNjFhNWZhYzkx
10
+ YmNiYjQ2NjQ4MWVhZTkwNTAwNTljMzU1MDg2ZDljODZhMGJkZDFkMjZmNmM4
11
+ ZWUyNzU1Y2M2MWI3YjFiYmE0ZjZmYjlhODBlYjQ3OTRiOTY0NmI=
12
12
  data.tar.gz: !binary |-
13
- NzUwNDJiNmNlZGI5NGY0YTQ4ZjM5ZWQxOTdiNjkwYTczZWI1ZDNlOGUyZjhk
14
- N2M5ZjEyNzRhOWQ1YmFmNGY2NTYwM2NkYmM2ZWQ3YzliZTJkZThkZDRkZmEx
15
- MjFlYjcyZjA3MTBkOTg5MmE0MzRmZTdiOThiY2Q4YjI3NjQ1ZTE=
13
+ OWQxODg3MThmOGMzOTkyZjMwZjQxMmFhMjM1ZDI2MDk0NWZlNDEwNDQ2YjBl
14
+ Nzk4MTQ5N2VlYjg5ZjNmNTVhMDU5YmM3NTBjYjU2MGYxNjBiM2Y0ZDYzMzU4
15
+ MTZmYmNjNTY2YmJkMDcxMzM1NTRkOGE0YzlkZTE0MmNkMjk5Y2Q=
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # BankingDataValidator
2
2
 
3
- [![Build Status](https://travis-ci.org/americodls/banking_data_validator.svg)](https://travis-ci.org/americodls/banking_data_validator)
3
+ [![Build Status](https://semaphoreapp.com/api/v1/projects/94783b1d-8994-47d8-b971-d1812dd31d40/339766/shields_badge.svg)](https://semaphoreapp.com/aduarte/banking_data_validator)
4
4
  [![Code Climate](https://codeclimate.com/github/americodls/banking_data_validator/badges/gpa.svg)](https://codeclimate.com/github/americodls/banking_data_validator)
5
- [![Coverage Status](https://coveralls.io/repos/americodls/banking_data_validator/badge.svg)](https://coveralls.io/r/americodls/banking_data_validator)
5
+ [![Test Coverage](https://codeclimate.com/github/americodls/banking_data_validator/badges/coverage.svg)](https://codeclimate.com/github/americodls/banking_data_validator)
6
6
  [![Dependency Status](https://gemnasium.com/americodls/banking_data_validator.svg)](https://gemnasium.com/americodls/banking_data_validator)
7
7
  [![security](https://hakiri.io/github/americodls/banking_data_validator/master.svg)](https://hakiri.io/github/americodls/banking_data_validator/master)
8
8
  [![Inline docs](http://inch-ci.org/github/americodls/banking_data_validator.svg?branch=master&style=shields)](http://inch-ci.org/github/americodls/banking_data_validator)
@@ -1,9 +1,10 @@
1
1
  lib = File.expand_path("../lib", __FILE__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "banking_data_validator/version"
3
4
 
4
5
  Gem::Specification.new do |spec|
5
6
  spec.name = "banking_data_validator"
6
- spec.version = "0.0.1"
7
+ spec.version = BankingDataValidator::VERSION
7
8
  spec.authors = ["Americo Duarte"]
8
9
  spec.email = ["americodls@gmail.com"]
9
10
  spec.summary = %q{An implementation of ActiveModel::Validator for bank accounts.}
@@ -20,7 +21,7 @@ Gem::Specification.new do |spec|
20
21
  spec.add_development_dependency "rake", "~> 10.0"
21
22
  spec.add_development_dependency "rspec", "~> 3.1"
22
23
  spec.add_development_dependency "mutant-rspec", "~> 0.7"
23
- spec.add_development_dependency "coveralls"
24
+ spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4.6"
24
25
 
25
26
  spec.add_runtime_dependency "activemodel", ">= 3.0"
26
27
  end
@@ -0,0 +1,29 @@
1
+ module BankingDataValidator
2
+ module Bank
3
+ class Base
4
+ def self.valid_account?(branch, account_number, account_digit)
5
+ new(branch, account_number, account_digit).valid_account?
6
+ end
7
+
8
+ def initialize(branch, account_number, account_digit)
9
+ @branch, @account_number, @account_digit = branch, account_number, account_digit
10
+ end
11
+
12
+ def valid_account?
13
+ @account_digit.upcase == checksum
14
+ end
15
+
16
+ private
17
+
18
+ def multiply_factors
19
+ digits.reverse.inject(0) do |total, algarism|
20
+ total + algarism * factors.next
21
+ end
22
+ end
23
+
24
+ def digits
25
+ @account_number.chars.map(&:to_i)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ require "banking_data_validator/bank/base"
2
+
3
+ module BankingDataValidator
4
+ module Bank
5
+ class Bradesco < Base
6
+ private
7
+
8
+ def checksum
9
+ case raw_checksum
10
+ when 0 then "0"
11
+ when 1 then "P"
12
+ else "#{11 - raw_checksum}"
13
+ end
14
+ end
15
+
16
+ def raw_checksum
17
+ @raw_checksum ||= multiply_factors % 11
18
+ end
19
+
20
+ def factors
21
+ @factors ||= (2..7).cycle
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ require "banking_data_validator/bank/base"
2
+
3
+ module BankingDataValidator
4
+ module Bank
5
+ class Brasil < Base
6
+ private
7
+
8
+ def checksum
9
+ case raw_checksum
10
+ when 10 then "X"
11
+ else "#{raw_checksum}"
12
+ end
13
+ end
14
+
15
+ def raw_checksum
16
+ @raw_checksum ||= multiply_factors % 11
17
+ end
18
+
19
+ def factors
20
+ @factors ||= (2..9).reverse_each.cycle
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ require "banking_data_validator/bank/base"
2
+
3
+ module BankingDataValidator
4
+ module Bank
5
+ class Caixa < Base
6
+ private
7
+
8
+ def checksum
9
+ "#{raw_checksum % 11}"
10
+ end
11
+
12
+ def raw_checksum
13
+ @raw_checksum ||= multiply_factors * 10
14
+ end
15
+
16
+ def factors
17
+ @factors ||= (2..9).cycle
18
+ end
19
+
20
+ def digits
21
+ "#{@branch}#{@account_number}".chars.map(&:to_i)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ module BankingDataValidator
2
+ module Bank
3
+ class Common < Base
4
+ def valid_account?
5
+ true
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ require "banking_data_validator/bank/base"
2
+
3
+ module BankingDataValidator
4
+ module Bank
5
+ class HSBC < Base
6
+ private
7
+
8
+ def checksum
9
+ "#{raw_checksum % 10}"
10
+ end
11
+
12
+ def raw_checksum
13
+ @raw_checksum ||= multiply_factors % 11
14
+ end
15
+
16
+ def factors
17
+ @factors ||= (2..9).reverse_each.cycle
18
+ end
19
+
20
+ def digits
21
+ "#{@branch}#{@account_number}".chars.map(&:to_i)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,37 @@
1
+ require "banking_data_validator/bank/base"
2
+
3
+ module BankingDataValidator
4
+ module Bank
5
+ class Itau < Base
6
+ private
7
+
8
+ def checksum
9
+ "#{raw_checksum % 10}"
10
+ end
11
+
12
+ def raw_checksum
13
+ 10 - (multiply_factors % 10)
14
+ end
15
+
16
+ def multiply_factors
17
+ digits.reverse.inject(0) do |total, digit|
18
+ total + sum_digits(digit * factors.next)
19
+ end
20
+ end
21
+
22
+ def digits
23
+ "#{@branch}#{@account_number}".chars.map(&:to_i)
24
+ end
25
+
26
+ def sum_digits(number)
27
+ number.to_s.chars.map(&:to_i).inject(0) do |total, digit|
28
+ total + digit
29
+ end
30
+ end
31
+
32
+ def factors
33
+ @factors ||= [2, 1].cycle
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ require "banking_data_validator/bank/base"
2
+
3
+ module BankingDataValidator
4
+ module Bank
5
+ class Santander < Base
6
+ private
7
+
8
+ def checksum
9
+ "#{raw_checksum % 10}"
10
+ end
11
+
12
+ def raw_checksum
13
+ 10 - multiply_factors
14
+ end
15
+
16
+ def multiply_factors
17
+ digits.inject(0) do |total, digit|
18
+ total + (digit * factors.next % 10)
19
+ end
20
+ end
21
+
22
+ def digits
23
+ "#{@branch}#{@account_number}".chars.map(&:to_i)
24
+ end
25
+
26
+ def factors
27
+ @factors ||= [9, 7, 3, 1, 9, 7, 1, 3, 1, 9, 7, 3].cycle
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ require "banking_data_validator/bank/brasil"
2
+ require "banking_data_validator/bank/santander"
3
+ require "banking_data_validator/bank/caixa"
4
+ require "banking_data_validator/bank/bradesco"
5
+ require "banking_data_validator/bank/itau"
6
+ require "banking_data_validator/bank/hsbc"
7
+ require "banking_data_validator/bank/common"
8
+
9
+ module BankingDataValidator
10
+ module Bank
11
+ def self.build_class(bank_number)
12
+ case bank_number
13
+ when "001" then Brasil
14
+ when "033" then Santander
15
+ when "104" then Caixa
16
+ when "237" then Bradesco
17
+ when "341" then Itau
18
+ when "399" then HSBC
19
+ else Common
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,35 @@
1
+ require "active_model"
2
+ require "banking_data_validator/bank"
3
+
4
+ module BankingDataValidator
5
+ class Validator < ActiveModel::Validator
6
+ def initialize(options = {})
7
+ super
8
+ @bank_number = options.fetch(:bank_number, :bank_number)
9
+ @branch_number = options.fetch(:branch_number, :branch_number)
10
+ @account_number = options.fetch(:account_number, :account_number)
11
+ @account_digit = options.fetch(:account_digit, :account_digit)
12
+ end
13
+
14
+ def validate(record)
15
+ bank_number = "#{record.public_send(@bank_number)}"
16
+ branch_number = "#{record.public_send(@branch_number)}"
17
+ account_number = "#{record.public_send(@account_number)}"
18
+ account_digit = "#{record.public_send(@account_digit)}"
19
+
20
+ bank = Bank.build_class(bank_number)
21
+
22
+ record.errors.add(@branch_number, :not_a_number) unless valid_number?(branch_number)
23
+ record.errors.add(@account_number, :not_a_number) unless valid_number?(account_number)
24
+ record.errors.add(@account_digit) unless bank.valid_account?(branch_number, account_number, account_digit)
25
+ end
26
+
27
+ private
28
+
29
+ def valid_number?(number)
30
+ Integer(number, 10)
31
+ rescue
32
+ false
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module BankingDataValidator
2
+ VERSION = "0.0.2"
3
+ end
@@ -1,53 +1,3 @@
1
- require "active_model"
2
-
3
- class BankingDataValidator < ActiveModel::Validator
4
- autoload :BancoBase, "banking_data_validator/banco_base"
5
- autoload :BancoBradesco, "banking_data_validator/banco_bradesco"
6
- autoload :BancoDoBrasil, "banking_data_validator/banco_do_brasil"
7
- autoload :BancoItau, "banking_data_validator/banco_itau"
8
- autoload :BancoSantander, "banking_data_validator/banco_santander"
9
-
10
- def initialize(options = {})
11
- super
12
- @bank_number_field_name = options.fetch(:bank_number, :bank_number)
13
- @branch_number_field_name = options.fetch(:branch_number, :branch_number)
14
- @account_number_field_name = options.fetch(:account_number, :account_number)
15
- @account_digit_field_name = options.fetch(:account_digit, :account_digit)
16
- end
17
-
18
- def validate(record)
19
- bank_number = record.public_send(@bank_number_field_name)
20
- branch_number = record.public_send(@branch_number_field_name)
21
- account_number = record.public_send(@account_number_field_name)
22
- account_digit = record.public_send(@account_digit_field_name)
23
-
24
- bank = build_bank(bank_number)
25
-
26
- if !valid_number?(branch_number)
27
- record.errors.add(@branch_number_field_name, :not_a_number)
28
- end
29
-
30
- if !valid_number?(account_number)
31
- record.errors.add(@account_number_field_name, :not_a_number)
32
- elsif !bank.valid_account?(branch_number, account_number, account_digit)
33
- record.errors.add(@account_digit_field_name)
34
- end
35
- end
36
-
37
- private
38
-
39
- def valid_number?(number)
40
- number && integer?(number)
41
- end
42
-
43
- def integer?(string)
44
- Integer(string, 10) && true rescue false
45
- end
46
-
47
- def build_bank(number)
48
- {
49
- "237" => BancoBradesco.new,
50
- nil => BancoBase.new,
51
- }[number]
52
- end
1
+ module BankingDataValidator
2
+ autoload "Validator", "banking_data_validator/validator"
53
3
  end
@@ -0,0 +1,22 @@
1
+ require "banking_data_validator/bank/bradesco"
2
+
3
+ module BankingDataValidator
4
+ module Bank
5
+ RSpec.describe Bradesco do
6
+ describe "#valid_account?" do
7
+ it "returns true when is given an account_number with a valid digit" do
8
+ expect(Bradesco.valid_account?(nil, "0238069", "2")).to eq(true)
9
+ expect(Bradesco.valid_account?(nil, "160000", "1")).to eq(true)
10
+ expect(Bradesco.valid_account?(nil, "87087", "0")).to eq(true)
11
+ expect(Bradesco.valid_account?(nil, "71000", "8")).to eq(true)
12
+ expect(Bradesco.valid_account?(nil, "3257", "3")).to eq(true)
13
+ expect(Bradesco.valid_account?(nil, "121", "p")).to eq(true)
14
+ end
15
+
16
+ it "returns false when is given an account_number with an invalid digit" do
17
+ expect(Bradesco.valid_account?(nil, "87362", "0")).to eq(false)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ require "banking_data_validator/bank/brasil"
2
+
3
+ module BankingDataValidator
4
+ module Bank
5
+ RSpec.describe Brasil do
6
+ describe "#valid_account?" do
7
+ it "returns true when is given an account_number with a valid digit" do
8
+ expect(Brasil.valid_account?(nil, "65005", "6")).to eq(true)
9
+ expect(Brasil.valid_account?(nil, "22029", "9")).to eq(true)
10
+ expect(Brasil.valid_account?(nil, "70000", "2")).to eq(true)
11
+ expect(Brasil.valid_account?(nil, "7107", "2")).to eq(true)
12
+ expect(Brasil.valid_account?(nil, "80000", "7")).to eq(true)
13
+ expect(Brasil.valid_account?(nil, "8354", "2")).to eq(true)
14
+ expect(Brasil.valid_account?(nil, "15007", "X")).to eq(true)
15
+ expect(Brasil.valid_account?(nil, "15007", "x")).to eq(true)
16
+ end
17
+
18
+ it "returns false when is given an account_number with an invalid digit" do
19
+ expect(Brasil.valid_account?(nil, "23433", "0")).to eq(false)
20
+ expect(Brasil.valid_account?(nil, "23433", "P")).to eq(false)
21
+ expect(Brasil.valid_account?(nil, "23433", "10")).to eq(false)
22
+ expect(Brasil.valid_account?(nil, "15007", "xx")).to eq(false)
23
+ expect(Brasil.valid_account?(nil, "15007", "10")).to eq(false)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,20 @@
1
+ require "banking_data_validator/bank/caixa"
2
+
3
+ module BankingDataValidator
4
+ module Bank
5
+ RSpec.describe Caixa do
6
+ describe "#valid_account?" do
7
+ it "returns true when is given an account_number with a valid digit" do
8
+ expect(Caixa.valid_account?("2004", "00100000448", "6")).to eq(true)
9
+ expect(Caixa.valid_account?("1314", "01300169300", "1")).to eq(true)
10
+ expect(Caixa.valid_account?("0268", "00100030339", "5")).to eq(true)
11
+ expect(Caixa.valid_account?("3498", "00100021718", "8")).to eq(true)
12
+ end
13
+
14
+ it "returns false when is given an account_number with an invalid digit" do
15
+ expect(Caixa.valid_account?("1234", "56789012345", "6")).to eq(false)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ require "banking_data_validator/bank/common"
2
+
3
+ module BankingDataValidator
4
+ module Bank
5
+ RSpec.describe Common do
6
+ describe "#valid_account?" do
7
+ it "always returns true" do
8
+ expect(Common.valid_account?(nil, "15007", "x")).to eq(true)
9
+ expect(Common.valid_account?(nil, "71000", "8")).to eq(true)
10
+ expect(Common.valid_account?(nil, "23433", "0")).to eq(true)
11
+ expect(Common.valid_account?(nil, "87362", "0")).to eq(true)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ require "banking_data_validator/bank/hsbc"
2
+
3
+ module BankingDataValidator
4
+ module Bank
5
+ RSpec.describe HSBC do
6
+ describe "#valid_account?" do
7
+ it "returns true when is given an account_number with a valid digit" do
8
+ expect(HSBC.valid_account?("0007", "853838", "6")).to eq(true)
9
+ expect(HSBC.valid_account?("1823", "023467", "7")).to eq(true)
10
+ expect(HSBC.valid_account?("0336", "006625", "3")).to eq(true)
11
+ expect(HSBC.valid_account?("0885", "008468", "2")).to eq(true)
12
+ end
13
+
14
+ it "returns false when is given an account_number with an invalid digit" do
15
+ expect(HSBC.valid_account?("0007", "853838", "9")).to eq(false)
16
+ expect(HSBC.valid_account?("1823", "023467", "9")).to eq(false)
17
+ expect(HSBC.valid_account?("0336", "006625", "9")).to eq(false)
18
+ expect(HSBC.valid_account?("0885", "008468", "9")).to eq(false)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ require "banking_data_validator/bank/itau"
2
+
3
+ module BankingDataValidator
4
+ module Bank
5
+ RSpec.describe Itau do
6
+ describe "#valid_account?" do
7
+ it "returns true when is given an account_number with a valid digit" do
8
+ expect(Itau.valid_account?("0262", "42602", "6")).to eq(true)
9
+ expect(Itau.valid_account?("1517", "15667", "3")).to eq(true)
10
+ expect(Itau.valid_account?("2545", "02366", "1")).to eq(true)
11
+ expect(Itau.valid_account?("8252", "09881", "2")).to eq(true)
12
+ end
13
+
14
+ it "returns false when is given an account_number with an invalid digit" do
15
+ expect(Itau.valid_account?("0991", "87362", "0")).to eq(false)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ require "banking_data_validator/bank/santander"
2
+
3
+ module BankingDataValidator
4
+ module Bank
5
+ RSpec.describe Santander do
6
+ describe "#valid_account?" do
7
+ it "returns true when is given an account_number with a valid digit" do
8
+ expect(Santander.valid_account?("0189", "01017417", "9")).to eq(true)
9
+ expect(Santander.valid_account?("3140", "13000470", "9")).to eq(true)
10
+ expect(Santander.valid_account?("1227", "43000005", "2")).to eq(true)
11
+ end
12
+
13
+ it "returns false when is given an account_number with an invalid digit" do
14
+ expect(Santander.valid_account?("0991", "01387362", "0")).to eq(false)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ require "banking_data_validator/bank"
2
+
3
+ module BankingDataValidator
4
+ RSpec.describe Bank do
5
+ describe ".build" do
6
+ it "return an bank instance based on the given bank_number" do
7
+ expect(Bank.build_class("001")).to eq(Bank::Brasil)
8
+ expect(Bank.build_class("033")).to eq(Bank::Santander)
9
+ expect(Bank.build_class("104")).to eq(Bank::Caixa)
10
+ expect(Bank.build_class("237")).to eq(Bank::Bradesco)
11
+ expect(Bank.build_class("341")).to eq(Bank::Itau)
12
+ expect(Bank.build_class("399")).to eq(Bank::HSBC)
13
+ expect(Bank.build_class("nil")).to eq(Bank::Common)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,56 @@
1
+ require "banking_data_validator/validator"
2
+
3
+ class Payment
4
+ include ActiveModel::Model
5
+ attr_accessor :bank_number, :branch_number, :account_number, :account_digit
6
+ validates_with BankingDataValidator::Validator
7
+ end
8
+
9
+ class CustomPayment
10
+ include ActiveModel::Model
11
+ attr_accessor :c_bank, :c_branch, :c_account, :c_digit
12
+ validates_with BankingDataValidator::Validator,
13
+ bank_number: :c_bank,
14
+ branch_number: :c_branch,
15
+ account_number: :c_account,
16
+ account_digit: :c_digit
17
+ end
18
+
19
+ module BankingDataValidator
20
+ RSpec.describe Validator do
21
+ it "requires a branch_number" do
22
+ [nil, "X"].each do |invalid_number|
23
+ payment = Payment.new(branch_number: invalid_number, account_number: "87362", account_digit: nil)
24
+ expect(payment).to_not be_valid
25
+ expect(payment.errors[:branch_number]).to include("is not a number")
26
+ end
27
+ end
28
+
29
+ it "requires a account_number" do
30
+ [nil, "X"].each do |invalid_number|
31
+ payment = Payment.new(branch_number: "123", account_number: invalid_number, account_digit: "2")
32
+ expect(payment).to_not be_valid
33
+ expect(payment.errors[:account_number]).to include("is not a number")
34
+ end
35
+ end
36
+
37
+ it "requires a valid account_digit" do
38
+ valid_payment = Payment.new(bank_number: "341", branch_number: "8252", account_number: "09881", account_digit: "2")
39
+ expect(valid_payment).to be_valid
40
+
41
+ invalid_payment = Payment.new(bank_number: "341", branch_number: "8252", account_number: "09881", account_digit: "3")
42
+ expect(invalid_payment).to_not be_valid
43
+ expect(invalid_payment.errors[:account_digit]).to include("is invalid")
44
+ end
45
+
46
+ it "uses default field names when custom fields are not given" do
47
+ expect(
48
+ Payment.new(bank_number: nil, branch_number: "123", account_number: "0238069", account_digit: "2")
49
+ ).to be_valid
50
+ end
51
+
52
+ it "accepts custom field names" do
53
+ expect(CustomPayment.new(c_bank: nil, c_branch: "123", c_account: "0238069", c_digit: "2")).to be_valid
54
+ end
55
+ end
56
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,7 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
 
3
- require "banking_data_validator"
4
-
5
- require 'coveralls'
6
- Coveralls.wear!
3
+ require "codeclimate-test-reporter"
4
+ CodeClimate::TestReporter.start
7
5
 
8
6
  # This file was generated by the `rspec --init` command. Conventionally, all
9
7
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: banking_data_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Americo Duarte
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-02 00:00:00.000000000 Z
11
+ date: 2015-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,19 +67,19 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0.7'
69
69
  - !ruby/object:Gem::Dependency
70
- name: coveralls
70
+ name: codeclimate-test-reporter
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ! '>='
73
+ - - ~>
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: 0.4.6
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ! '>='
80
+ - - ~>
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: 0.4.6
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: activemodel
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -110,16 +110,26 @@ files:
110
110
  - Rakefile
111
111
  - banking_data_validator.gemspec
112
112
  - lib/banking_data_validator.rb
113
- - lib/banking_data_validator/banco_base.rb
114
- - lib/banking_data_validator/banco_bradesco.rb
115
- - lib/banking_data_validator/banco_do_brasil.rb
116
- - lib/banking_data_validator/banco_itau.rb
117
- - lib/banking_data_validator/banco_santander.rb
118
- - spec/banking_data_validator.rb
119
- - spec/banking_data_validator/banco_bradesco_spec.rb
120
- - spec/banking_data_validator/banco_do_brasil_spec.rb
121
- - spec/banking_data_validator/banco_itau_spec.rb
122
- - spec/banking_data_validator/banco_santander_spec.rb
113
+ - lib/banking_data_validator/bank.rb
114
+ - lib/banking_data_validator/bank/base.rb
115
+ - lib/banking_data_validator/bank/bradesco.rb
116
+ - lib/banking_data_validator/bank/brasil.rb
117
+ - lib/banking_data_validator/bank/caixa.rb
118
+ - lib/banking_data_validator/bank/common.rb
119
+ - lib/banking_data_validator/bank/hsbc.rb
120
+ - lib/banking_data_validator/bank/itau.rb
121
+ - lib/banking_data_validator/bank/santander.rb
122
+ - lib/banking_data_validator/validator.rb
123
+ - lib/banking_data_validator/version.rb
124
+ - spec/banking_data_validator/bank/bradesco_spec.rb
125
+ - spec/banking_data_validator/bank/brasil_spec.rb
126
+ - spec/banking_data_validator/bank/caixa_spec.rb
127
+ - spec/banking_data_validator/bank/common_spec.rb
128
+ - spec/banking_data_validator/bank/hsbc_spec.rb
129
+ - spec/banking_data_validator/bank/itau_spec.rb
130
+ - spec/banking_data_validator/bank/santander_spec.rb
131
+ - spec/banking_data_validator/bank_spec.rb
132
+ - spec/banking_data_validator/validator_spec.rb
123
133
  - spec/spec_helper.rb
124
134
  homepage: http://www.bioritmo.com.br
125
135
  licenses:
@@ -141,14 +151,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
151
  version: '0'
142
152
  requirements: []
143
153
  rubyforge_project:
144
- rubygems_version: 2.4.5
154
+ rubygems_version: 2.4.4
145
155
  signing_key:
146
156
  specification_version: 4
147
157
  summary: An implementation of ActiveModel::Validator for bank accounts.
148
158
  test_files:
149
- - spec/banking_data_validator.rb
150
- - spec/banking_data_validator/banco_bradesco_spec.rb
151
- - spec/banking_data_validator/banco_do_brasil_spec.rb
152
- - spec/banking_data_validator/banco_itau_spec.rb
153
- - spec/banking_data_validator/banco_santander_spec.rb
159
+ - spec/banking_data_validator/bank/bradesco_spec.rb
160
+ - spec/banking_data_validator/bank/brasil_spec.rb
161
+ - spec/banking_data_validator/bank/caixa_spec.rb
162
+ - spec/banking_data_validator/bank/common_spec.rb
163
+ - spec/banking_data_validator/bank/hsbc_spec.rb
164
+ - spec/banking_data_validator/bank/itau_spec.rb
165
+ - spec/banking_data_validator/bank/santander_spec.rb
166
+ - spec/banking_data_validator/bank_spec.rb
167
+ - spec/banking_data_validator/validator_spec.rb
154
168
  - spec/spec_helper.rb
@@ -1,17 +0,0 @@
1
- class BankingDataValidator
2
- class BancoBase
3
- def valid_account?(branch, account_number, account_digit)
4
- true
5
- end
6
-
7
- private
8
-
9
- def multiplier(factors, string)
10
- multiplier_position = factors.cycle
11
-
12
- string.chars.map(&:to_i).reverse.inject(0) do |total, algarism|
13
- total + algarism * multiplier_position.next
14
- end
15
- end
16
- end
17
- end
@@ -1,22 +0,0 @@
1
- class BankingDataValidator
2
- class BancoBradesco < BancoBase
3
- def valid_account?(branch, account_number, account_digit)
4
- account_digit.upcase == modulo11_2to7_p_as_1(account_number)
5
- end
6
-
7
- private
8
-
9
- def modulo11_2to7_p_as_1(string)
10
- value = modulo11_2to7(string)
11
- case value
12
- when 0 then "0"
13
- when 1 then "P"
14
- else "#{11 - value}"
15
- end
16
- end
17
-
18
- def modulo11_2to7(string)
19
- multiplier([2, 3, 4, 5, 6, 7], string) % 11
20
- end
21
- end
22
- end
@@ -1,21 +0,0 @@
1
- class BankingDataValidator
2
- class BancoDoBrasil < BancoBase
3
- def valid_account?(branch, account_number, account_digit)
4
- account_digit.upcase == module11_9to2_10_as_x(account_number).to_s
5
- end
6
-
7
- private
8
-
9
- def module11_9to2_10_as_x(string)
10
- value = module11_9to2(string)
11
- case value
12
- when 10 then "X"
13
- else value
14
- end
15
- end
16
-
17
- def module11_9to2(string)
18
- multiplier([9, 8, 7, 6, 5, 4, 3, 2], string) % 11
19
- end
20
- end
21
- end
@@ -1,35 +0,0 @@
1
- class BankingDataValidator
2
- class BancoItau < BancoBase
3
- def valid_account?(branch, account_number, account_digit)
4
- account_digit == module10("#{branch}#{account_number}")
5
- end
6
-
7
- private
8
-
9
- def module10(str)
10
- total = 0
11
- multiplier = 2
12
-
13
- str.split(//).reverse!.each do |char|
14
- total += sum_digits(char.to_i * multiplier)
15
- multiplier = multiplier == 2 ? 1 : 2
16
- end
17
-
18
- value = (10 - (total % 10))
19
- value == 10 ? "0" : "#{value}"
20
- end
21
-
22
- def sum_digits(str)
23
- initial_value = str.kind_of?(Fixnum) ? str : str.to_i
24
- return 0 if initial_value == 0
25
- return initial_value if initial_value <= 9
26
-
27
- initial_value = initial_value.to_s
28
- total = 0
29
-
30
- 0.upto(initial_value.size-1) {|digit| total += initial_value[digit, 1].to_i }
31
-
32
- return total
33
- end
34
- end
35
- end
@@ -1,19 +0,0 @@
1
- class BankingDataValidator
2
- class BancoSantander < BancoBase
3
- def valid_account?(branch, account_number, account_digit)
4
- account_digit.upcase == module10_santander("#{branch}#{account_number}").to_s
5
- end
6
-
7
- private
8
-
9
- def module10_santander(account_number)
10
- factors = [9, 7, 3, 1, 9, 7, 1, 3, 1, 9, 7, 3].cycle
11
-
12
- total = account_number.chars.map(&:to_i).inject(0) do |acc, digito|
13
- acc + (digito * factors.next % 10)
14
- end
15
-
16
- 10 - total % 10
17
- end
18
- end
19
- end
@@ -1,16 +0,0 @@
1
- RSpec.describe BankingDataValidator::BancoBradesco do
2
- describe "#valid_account?" do
3
- it "is true when the given account data is valid" do
4
- expect(subject).to be_valid_account(nil, "0238069", "2")
5
- expect(subject).to be_valid_account(nil, "160000", "1")
6
- expect(subject).to be_valid_account(nil, "87087", "0")
7
- expect(subject).to be_valid_account(nil, "71000", "8")
8
- expect(subject).to be_valid_account(nil, "3257", "3")
9
- expect(subject).to be_valid_account(nil, "121", "p")
10
- end
11
-
12
- it "is false when the given account data is not valid" do
13
- expect(subject).to_not be_valid_account(nil, "87362", "0")
14
- end
15
- end
16
- end
@@ -1,22 +0,0 @@
1
- RSpec.describe BankingDataValidator::BancoDoBrasil do
2
- describe "#valid_account?" do
3
- it "valid account digits" do
4
- expect(subject).to be_valid_account(nil, "65005", "6")
5
- expect(subject).to be_valid_account(nil, "22029", "9")
6
- expect(subject).to be_valid_account(nil, "70000", "2")
7
- expect(subject).to be_valid_account(nil, "7107" , "2")
8
- expect(subject).to be_valid_account(nil, "80000", "7")
9
- expect(subject).to be_valid_account(nil, "8354" , "2")
10
- expect(subject).to be_valid_account(nil, "15007", "X")
11
- expect(subject).to be_valid_account(nil, "15007", "x")
12
- end
13
-
14
- it "invalid account digits" do
15
- expect(subject).to_not be_valid_account(nil, "23433", "0")
16
- expect(subject).to_not be_valid_account(nil, "23433", "P")
17
- expect(subject).to_not be_valid_account(nil, "23433", "10")
18
- expect(subject).to_not be_valid_account(nil, "15007", "xx")
19
- expect(subject).to_not be_valid_account(nil, "15007", "10")
20
- end
21
- end
22
- end
@@ -1,13 +0,0 @@
1
- RSpec.describe BankingDataValidator::BancoItau do
2
- describe "#valid_account?" do
3
- it "valid account digits" do
4
- expect(subject).to be_valid_account("0262", "42602", "6")
5
- expect(subject).to be_valid_account("1517", "15667", "3")
6
- expect(subject).to be_valid_account("2545", "02366", "1")
7
- end
8
-
9
- it "invalid account digits" do
10
- expect(subject).to_not be_valid_account("0991", "87362", "0")
11
- end
12
- end
13
- end
@@ -1,13 +0,0 @@
1
- RSpec.describe BankingDataValidator::BancoSantander do
2
- describe "#valid_account?" do
3
- it "valid account digits" do
4
- expect(subject).to be_valid_account("0189", "01017417", "9")
5
- expect(subject).to be_valid_account("3140", "13000470", "9")
6
- expect(subject).to be_valid_account("1227", "43000005", "2")
7
- end
8
-
9
- it "invalid account digits" do
10
- expect(subject).to_not be_valid_account("0991", "01387362", "0")
11
- end
12
- end
13
- end
@@ -1,62 +0,0 @@
1
- RSpec.describe BankingDataValidator do
2
-
3
- it "requires a branch_number" do
4
- [nil, "X"].each do |invalid_branch_number|
5
- payment = Payment.new(branch_number: invalid_branch_number, account_number: "87362", account_digit: nil)
6
- expect(payment).to_not be_valid
7
- expect(payment.errors[:branch_number]).to include("is not a number")
8
- end
9
- end
10
-
11
- it "requires a account_number" do
12
- [nil, "X"].each do |invalid_account_number|
13
- payment = Payment.new(branch_number: "123", account_number: invalid_account_number, account_digit: "2")
14
- expect(payment).to_not be_valid
15
- expect(payment.errors[:account_number]).to include("is not a number")
16
- end
17
- end
18
-
19
- it "requires a check digit" do
20
- payment = Payment.new(bank_number: "237", branch_number: "123", account_number: "87362", account_digit: "0")
21
- expect(payment).to_not be_valid
22
- expect(payment.errors[:account_digit]).to include("is invalid")
23
- end
24
-
25
- it "uses default field names when custom field is not given" do
26
- valid_attributes.each { |attributes| expect(Payment.new(attributes)).to be_valid }
27
- end
28
-
29
- it "accepts custom field names" do
30
- expect(CustomPayment.new(c_bank: "237", c_branch: "123", c_account: "0238069", c_digit: "2")).to be_valid
31
- end
32
-
33
- describe "Bradesco" do
34
- it "validates account digits" do
35
- valid_attributes.each { |attributes| expect(Payment.new(attributes)).to be_valid }
36
-
37
- expect(Payment.new(bank_number: "237", branch_number: "123", account_number: "87362", account_digit: "2")).to_not be_valid
38
- end
39
- end
40
- end
41
-
42
- def valid_attributes
43
- [
44
- {branch_number: "123", account_number: "0238069", account_digit: "2"},
45
- {branch_number: "123", account_number: "160000", account_digit: "1"},
46
- {branch_number: "123", account_number: "87087", account_digit: "0"},
47
- {branch_number: "123", account_number: "71000", account_digit: "8"},
48
- {branch_number: "123", account_number: "3257", account_digit: "3"}
49
- ]
50
- end
51
-
52
- class Payment
53
- include ActiveModel::Model
54
- attr_accessor :bank_number, :branch_number, :account_number, :account_digit
55
- validates_with BankingDataValidator
56
- end
57
-
58
- class CustomPayment
59
- include ActiveModel::Model
60
- attr_accessor :c_bank, :c_branch, :c_account, :c_digit
61
- validates_with BankingDataValidator, bank_number: :c_bank, branch_number: :c_branch, account_number: :c_account, account_digit: :c_digit
62
- end