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 Faker::Finance.vat_number #1348

Merged
merged 6 commits into from
Aug 29, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- [PR #1329](https://github.com/stympy/faker/pull/1329) Update docs on behavior of price [@softwaregravy](https://github.com/softwaregravy)

### Feature Request
- [PR #1348](https://github.com/stympy/faker/pull/1348) Add Faker::Finance.vat_number [@vbrazo](https://github.com/vbrazo)
- [PR #1342](https://github.com/stympy/faker/pull/1342) Added Faker::CryptoCoin scope [@jacksonpires](https://github.com/jacksonpires)
- [PR #1338](https://github.com/stympy/faker/pull/1338) Add new translations to the en-ZA locale [@bradleymarques](https://github.com/bradleymarques)
- [PR #1341](https://github.com/stympy/faker/pull/1341) Add Faker::Construction [@benwyrosdick](https://github.com/benwyrosdick)
Expand Down
4 changes: 4 additions & 0 deletions doc/finance.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
Faker::Finance.credit_card #=> "3018-348979-1853"
Faker::Finance.credit_card(:mastercard) #=> "6771-8921-2291-6236"
Faker::Finance.credit_card(:mastercard, :visa) #=> "4448-8934-1277-7195"

# Random vat_number
Faker::Finance.vat_number #=> "BR38.395.329/2471-83"
Faker::Finance.vat_number('DE') #=> "DE593306671"
```
11 changes: 11 additions & 0 deletions lib/faker/finance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@ def credit_card(*types)
multiplier = (multiplier == 2 ? 1 : 2)
sum + (digit * multiplier).to_s.split('').map(&:to_i).inject(0) { |digit_sum, cur| digit_sum + cur }
end

# the sum plus whatever the last digit is must be a multiple of 10. So, the
# last digit must be 10 - the last digit of the sum.
luhn_digit = (10 - (luhn_sum % 10)) % 10

template.gsub('L', luhn_digit.to_s)
end

def vat_number(country = 'BR')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for the default country to be BR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, there isn't

numerify(fetch("finance.vat_number.#{country}"))
rescue I18n::MissingTranslationData
raise ArgumentError, "Could not find vat number for #{country}"
end

def vat_number_keys
translate('faker.finance.vat_number').keys
end
end
end
end
60 changes: 60 additions & 0 deletions lib/locales/en/finance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,63 @@ en:
- /6706#########{5,6}L/
- /6771#########{5,6}L/
- /6709#########{5,6}L/
vat_number:
AT: "ATU########"
AR: "AR###########"
AU: "AU###########"
BR: "BR##.###.###/####-##"
BY: "УНП #########"
BE: "BE0#########"
BG:
- "BG#########"
- "BG##########"
CH: "CH######"
CL: "CL########-#"
CZ:
- "CZ########"
- "CZ#########"
- "CZ##########"
DK: "DK## ## ## ##"
EC: "EC#############"
EE: "EE#########"
FI: "FI########"
DE: "DE#########"
GB:
- "GB### #### ##"
- "GB### #### ## ###"
- "GBGD###"
- "GBHA###"
- "GB888 8### ##"
GR: "EL#########"
HR: "HR### ### ### ##"
"NO": "NO#########"
HU: "HU########"
IT: "IT##########"
LT:
- "LT#########"
- "LT############"
LU: "LU########"
LV: "LV###########"
MT: "MT########"
MX: "MX#### ###### ###"
PH: "PH### ### ### ###"
PL: "PL##########"
PT: "PT#########"
RO:
- "RO##"
- "RO###"
- "RO####"
- "RO#####"
- "RO######"
- "RO#######"
- "RO########"
- "RO#########"
- "RO##########"
RU:
- "RU##########"
- "RU############"
SE: "SE##########01"
SI: "SI########"
SK: "SK##########"
SM: "SM#####"
UA: "UA##########"
25 changes: 25 additions & 0 deletions test/test_faker_finance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require_relative 'test_helper'

class TestFakerFinance < Test::Unit::TestCase
def setup
Faker::Config.locale = nil
end

def test_vat_number
assert Faker::Finance.vat_number.match(/\w+/)
end

def test_vat_number_with_invalid_params
assert_raise ArgumentError do
Faker::Finance.vat_number(Faker::Lorem.word)
end
end

def test_vat_number_with_valid_params
Faker::Finance.vat_number_keys.each do |country|
assert Faker::Finance.vat_number(country).match(/\w+/)
end
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'simplecov'
SimpleCov.formatter = SimpleCov::Formatter::HTMLFormatter
SimpleCov.start do
add_filter ['.bundle', 'test', 'lib/extensions']
add_filter ['.bundle', 'lib/extensions', 'test']
end

require 'test/unit'
Expand Down