Skip to content

Commit

Permalink
Fixed the inaccurate swedish organization number generator (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
hex0cter authored and stympy committed Dec 18, 2016
1 parent 7a44e6c commit e1eefa8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,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

0 comments on commit e1eefa8

Please sign in to comment.