Skip to content

Commit

Permalink
Add norwegian organization number (#827)
Browse files Browse the repository at this point in the history
  • Loading branch information
leifcr authored and stympy committed Feb 16, 2017
1 parent 5f05153 commit a72136f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/company.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Faker::Company.logo #=> "https://pigment.github.com/fake-logos/logos/medium/colo

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

Faker::Company.norwegian_organisation_number #=> "839071558"

# Generate an Australian Business Number
Faker::Company.australian_business_number #=> "81137773602"

Expand Down
34 changes: 33 additions & 1 deletion lib/faker/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,20 @@ def swedish_organisation_number
base + luhn_algorithm(base).to_s
end

# Get a random Norwegian organization number. Info: https://www.brreg.no/om-oss/samfunnsoppdraget-vart/registera-vare/einingsregisteret/organisasjonsnummeret/
def norwegian_organisation_number
# Valid leading digit: 8, 9
mod11_check = nil
while mod11_check.nil?
base = [[8, 9].sample, ('%07d' % rand(10 ** 7))].join
mod11_check = mod11(base)
end
base + mod11_check.to_s
end

def australian_business_number
base = ('%09d' % rand(10 ** 9))
abn = "00#{base}"
abn = "00#{base}"

(99 - (abn_checksum(abn) % 89)).to_s + base
end
Expand All @@ -65,6 +76,27 @@ def profession

private

# Mod11 functionality from https://github.com/badmanski/mod11/blob/master/lib/mod11.rb
def mod11(number)
weight = [2, 3, 4, 5, 6, 7,
2, 3, 4, 5, 6, 7,
2, 3, 4, 5, 6, 7]

sum = 0

number.to_s.reverse.chars.each_with_index do |char, i|
sum += char.to_i * weight[i]
end

remainder = sum % 11

case remainder
when 0 then remainder
when 1 then nil
else 11 - remainder
end
end

def luhn_algorithm(number)
multiplications = []

Expand Down
11 changes: 9 additions & 2 deletions test/test_faker_company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ def test_swedish_organisation_number
assert org_no[9] == @tester.send(:luhn_algorithm, org_no[0..8]).to_s
end

def test_norwegian_organisation_number
org_no = @tester.norwegian_organisation_number
assert org_no.match(/\d{9}/)
assert [8, 9].include?(org_no[0].to_i)
assert org_no[8] == @tester.send(:mod11, org_no[0..7]).to_s
end

def test_australian_business_number
abn = @tester.australian_business_number
checksum = abn_checksum(abn)
Expand All @@ -41,14 +48,14 @@ def test_profession
assert @tester.profession.match(/[a-z ]+\.?/)
end

private
private

def abn_checksum(abn)
abn_weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

abn.split('').map(&:to_i).each_with_index.map do |n, i|
(i == 0 ? n-1 : n) * abn_weights[i]
end.inject(:+)
end
end

end

0 comments on commit a72136f

Please sign in to comment.