Skip to content

Commit

Permalink
Constrain BankUS.routing_number's first two digits (#545)
Browse files Browse the repository at this point in the history
* Constrain BankUS.routing_number's first two digits

* Update lib/ffaker/bank_us.rb

Co-authored-by: marocchino <marocchino@users.noreply.github.com>

* Update lib/ffaker/bank_us.rb

Co-authored-by: marocchino <marocchino@users.noreply.github.com>

* PR feedback

---------

Co-authored-by: marocchino <marocchino@users.noreply.github.com>
  • Loading branch information
professor and marocchino committed Sep 21, 2023
1 parent d6007d3 commit fe33804
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changelog.md
@@ -1,6 +1,7 @@
## development

- Add your change HERE
- Limit FFaker::BankUS.routing_number first two digits [@professor]

# 2.23.0
- Add FFaker::BankUS.accounting_number [@professor]
Expand Down
6 changes: 5 additions & 1 deletion lib/ffaker/bank_us.rb
Expand Up @@ -5,12 +5,16 @@ module BankUS
extend ModuleUtils
extend self

ROUTING_NUMBER_PREFIXES = [*'00'..'12', *'21'..'32', *'61'..'72', '80'].freeze

def account_number(min_digits: 9, max_digits: 17)
FFaker.numerify('#' * rand(min_digits..max_digits))
end

def routing_number
partial_routing_number = FFaker.numerify('########')
first_two_digits = fetch_sample(ROUTING_NUMBER_PREFIXES)

partial_routing_number = FFaker.numerify("#{first_two_digits}######")
ninth_digit = generate_ninth_digit(partial_routing_number)

"#{partial_routing_number}#{ninth_digit}"
Expand Down
2 changes: 2 additions & 0 deletions test/test_bank_us.rb
Expand Up @@ -24,6 +24,8 @@ def test_routing_number
routing_number = @tester.routing_number
assert_match(/\A\d{9}\z/, routing_number)

assert_true(@tester::ROUTING_NUMBER_PREFIXES.include?(routing_number[0..1]))

checksum = (
(7 * (routing_number[0].to_i + routing_number[3].to_i + routing_number[6].to_i)) +
(3 * (routing_number[1].to_i + routing_number[4].to_i + routing_number[7].to_i)) +
Expand Down

0 comments on commit fe33804

Please sign in to comment.