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

Fixed the inaccurate swedish organization number generator #715

Merged
merged 2 commits into from
Dec 18, 2016
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ Faker::Company.duns_number #=> "08-341-3736"
# Get a random company logo url in PNG format.
Faker::Company.logo #=> "https://pigment.github.com/fake-logos/logos/medium/color/5.png"

Faker::Company.swedish_organisation_number #=> "7718797652"
Faker::Company.swedish_organisation_number #=> "7962578022"

# Generate an Australian Business Number
Faker::Company.australian_business_number #=> "81137773602"
Expand Down
6 changes: 5 additions & 1 deletion lib/faker/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ def logo
"https://pigment.github.io/fake-logos/logos/medium/color/#{rand_num}.png"
end

# Get a random Swedish organization number. See more here https://sv.wikipedia.org/wiki/Organisationsnummer
def swedish_organisation_number
base = ('%09d' % rand(10 ** 9))
# Valid leading digit: 1, 2, 3, 5, 6, 7, 8, 9
# Valid third digit: >= 2
# Last digit is a control digit
base = [[1, 2, 3, 5, 6, 7, 8, 9].sample, (0..9).to_a.sample, (2..9).to_a.sample, ('%06d' % rand(10 ** 6))].join
base + luhn_algorithm(base).to_s
end

Expand Down
2 changes: 2 additions & 0 deletions test/test_faker_company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def test_buzzword
def test_swedish_organisation_number
org_no = @tester.swedish_organisation_number
assert org_no.match(/\d{10}/)
assert [1, 2, 3, 5, 6, 7, 8, 9].include?(org_no[0].to_i)
assert org_no[2].to_i >= 2
assert org_no[9] == @tester.send(:luhn_algorithm, org_no[0..8]).to_s
end

Expand Down