Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bank.iban_country_code. #2248

Merged
merged 1 commit into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/faker/default/bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ def account_number(legacy_digits = NOT_GIVEN, digits: 10)
##
# Produces a bank iban number.
#
# @param country_code [String] Specifies what country prefix is used to generate the iban code.
# @param country_code [String, nil] Specifies what country prefix is used to generate the iban code. Providing `nil` will use a random country.
# @return [String]
#
# @example
# Faker::Bank.iban #=> "GB76DZJM33188515981979"
# Faker::Bank.iban(country_code: "be") #=> "BE6375388567752043"
# Faker::Bank.iban(country_code: nil) #=> "DE45186738071857270067"
#
# @faker.version 1.7.0
def iban(legacy_country_code = NOT_GIVEN, country_code: 'GB')
Expand All @@ -47,6 +48,8 @@ def iban(legacy_country_code = NOT_GIVEN, country_code: 'GB')
keywords << :country_code if legacy_country_code != NOT_GIVEN
end

country_code ||= iban_country_code

begin
pattern = fetch("bank.iban_details.#{country_code.downcase}.bban_pattern")
rescue I18n::MissingTranslationData
Expand All @@ -60,6 +63,19 @@ def iban(legacy_country_code = NOT_GIVEN, country_code: 'GB')
country_code.upcase + iban_checksum(country_code, account) + account
end

##
# Produces the ISO 3166 code of a country that uses the IBAN system.
#
# @return [String]
#
# @example
# Faker::Bank.iban_country_code #=> "CH"
#
# @faker.version next
def iban_country_code
sample(translate('faker.bank.iban_details').keys).to_s.upcase
end

##
# Produces a bank name.
#
Expand Down
12 changes: 10 additions & 2 deletions test/faker/default/test_faker_bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_swift_bic
assert @tester.swift_bic.match(/(\w+\.? ?){2,3}/)
end

# This test makes sure there are no collissions in BIC number pool
# This test makes sure there are no collisions in BIC number pool
# https://github.com/faker-ruby/faker/pull/2130#issuecomment-703213837
# def test_swift_bic_collission
# 10.times do
Expand All @@ -56,8 +56,16 @@ def test_swift_bic
# end
# end

def test_iban_country_code
assert_match(/^[A-Z]{2}$/, @tester.iban_country_code)
end

def test_iban_default
assert @tester.iban.match(/[A-Z]{4}\d{14}/)
assert_match(/^GB\d{2}[A-Z]{4}\d{14}$/, @tester.iban)
end

def test_iban_rand_country
assert_match(/^[A-Z]{2}\d{2}[A-Z\d]{10,30}$/, @tester.iban(country_code: nil))
end

# Andorra
Expand Down