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 12 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
```
19 changes: 19 additions & 0 deletions lib/faker/id_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ def ssn_valid
INVALID_SSN.any? { |regex| regex =~ ssn } ? ssn_valid : ssn
end

def spanish_citizen_number
checks = "TRWAGMYFPDXBNJZSQVHLCKE"
num = Faker::Number.number(8)
mod = num.to_i % 23
check = checks[mod]
"#{num}-#{check}"
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 = "XYZ"
digits = Faker::Number.number(7)
prefix = code[rand(code.length)]
prefix_val = "XYZ".index(prefix).to_s
mod = ("#{prefix_val}#{digits.to_s}").to_i % 23
check = checks[mod]
"#{prefix}-#{digits}-#{check}"
end

private

def _translate(key)
Expand Down
50 changes: 50 additions & 0 deletions test/test_faker_id_number.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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_equal "-", sample[3]
assert_equal "-", 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_equal "-", sample[3]
assert_equal "-", 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_equal 10, sample.length
assert sample[0..7].split().map{:to_i}.all?{:is_digit?}
assert_equal sample[8], "-"
mod = sample[0..7].to_i % 23
assert_equal "TRWAGMYFPDXBNJZSQVHLCKE"[mod], sample[9]
end

def test_spanish_NIE
sample = @tester.spanish_foreign_citizen_number
assert_equal 11, sample.length
assert "XYZ".include?(sample[0])
assert_equal "-", sample[1]
assert sample[2..8].split().map{:to_i}.all?{:is_digit?}
assert_equal "-", sample[9]
prefix = "XYZ".index(sample[0]).to_s
mod = ("#{prefix}#{sample[2..8]}").to_i % 23
assert_equal "TRWAGMYFPDXBNJZSQVHLCKE"[mod], sample[10]
end

end