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 Spanish citizen id and docs #1147

Merged
merged 16 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from 8 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Contents
- [Faker::Hobbit](doc/hobbit.md)
- [Faker::HowIMetYourMother](doc/how_i_met_your_mother.md)
- [Faker::HitchhikersGuideToTheGalaxy](doc/hitchhikers_guide_to_the_galaxy.md)
- [Faker::IDNumber](doc/id_number.md)
- [Faker::Internet](doc/internet.md)
- [Faker::Job](doc/job.md)
- [Faker::Kpop](doc/kpop.md)
Expand Down
15 changes: 15 additions & 0 deletions doc/id_number.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Faker::IDNumber

```ruby
# Generate a valid US Social Security number
Faker::IDNumber.valid #=> "552-56-3593"

# Generate an invalid US Social Security number
Faker::IDNumber.invalid #=> "311-72-0000"

# Generate a Spanish citizen identifier (DNI)
Faker::IDNumber.spanish_citizen_number

# Generate a Spanish foreign born citizen identifier (NIE)
Faker::IDNumber.spanish_foreign_citizen_number
```
11 changes: 11 additions & 0 deletions lib/faker/id_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ def ssn_valid
INVALID_SSN.any? { |regex| regex =~ ssn } ? ssn_valid : ssn
end

def spanish_citizen_number
checks = "TRWAGMYFPDXBNJZSQVHLCKE"
"#{Faker::Number.number(8)}-#{checks[rand(checks.length)]}"

Choose a reason for hiding this comment

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

If you want the identifier to be valid you have to follow and algorithm. https://www.ordenacionjuego.es/en/calculo-digito-control

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure how accurate it needed to be.

The spanish_organisation_number code in the company.rb class is just randomly sampling from the characters and I kinda just followed that example.

Before I try to read auto-translated code again :o ... are the rules for the checksum the same as the ones I outline here: #929 (comment) ?

Choose a reason for hiding this comment

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

I'm not sure if there are different ways to do it...the way I know is super simple.

<8 digits number> mod 23 = remainder

REMAINDER 0 1 2 3 4 5 6 7 8 9 10 11
LETTER T R W A G M Y F P D X B
REMAINDER 12 13 14 15 16 17 18 19 20 21 22
LETTER N J Z S Q V H L C K E

For instance, mine is: 47922235X, so 47922235 mod 23 = 10
10 = X in the table

end

def spanish_foreign_citizen_number
checks = "TRWAGMYFPDXBNJZSQVHLCKE"
Copy link
Member

@vbrazo vbrazo May 15, 2018

Choose a reason for hiding this comment

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

I can see that you're repeating this line checks = "TRWAGMYFPDXBNJZSQVHLCKE" a few times. Could we write a constant instead?

code = "XY"
"#{code[rand(code.length)]}-#{Faker::Number.number(7)}-#{checks[rand(checks.length)]}"
end

private

def _translate(key)
Expand Down
47 changes: 47 additions & 0 deletions test/test_faker_id_number.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb')

class TestFakerIdNumber < Test::Unit::TestCase

def setup
@tester = Faker::IDNumber
end

def test_valid_ssn
sample = @tester.valid
assert sample.length == 11
assert sample[3] == "-"
assert sample[6] == "-"
assert sample[0..2].split().map{:to_i}.all?{:is_digit?}
assert sample[4..5].split().map{:to_i}.all?{:is_digit?}
assert sample[7..9].split().map{:to_i}.all?{:is_digit?}
end

def test_invalid_ssn
sample = @tester.invalid
assert sample.length == 11
assert sample[3] == "-"
assert sample[6] == "-"
assert sample[0..2].split().map{:to_i}.all?{:is_digit?}
assert sample[4..5].split().map{:to_i}.all?{:is_digit?}
assert sample[7..9].split().map{:to_i}.all?{:is_digit?}
end

def test_spanish_DNI
sample = @tester.spanish_citizen_number
assert sample.length == 10
assert sample[0..7].split().map{:to_i}.all?{:is_digit?}
assert sample[8] == "-"
assert "TRWAGMYFPDXBNJZSQVHLCKE".include?(sample[9])
end

def test_spanish_NIE
sample = @tester.spanish_foreign_citizen_number
assert sample.length == 11
assert "XY".include?(sample[0])
assert sample[1] == "-"
assert sample[2..8].split().map{:to_i}.all?{:is_digit?}
assert sample[9] == "-"
assert "TRWAGMYFPDXBNJZSQVHLCKE".include?(sample[10])
end

end