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

Conversation

PuZZleDucK
Copy link
Contributor

As requested in #872

Also add docs for the IDNumber class in general

@@ -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

@PuZZleDucK
Copy link
Contributor Author

Alright, added checksum for DNI... would it be the same for NIE but with the 7 numbers?

@jmolina4
Copy link

It changes a bit @PuZZleDucK.
NIE starts with a letter (X,Y or Z) following by 7 numbers and another character. You have to replace the first character for a number and then do the same in order to get the last character.

Initial character mapping:
X = 0
Y = 1
Z = 2

for instance, X1234567 == 01234567 or Y1234567 == 11234567 or Z1234567 == 21234567
then you do mod 23 and you get the last character.

@PuZZleDucK
Copy link
Contributor Author

I dear :o ... I got an error in CI I don't understand...
"Error: test_determinism(TestDeterminism): RuntimeError: Faker::IDNumber.spanish_foreign_citizen_number raised "undefined method `+@' for "6215066":String""

I'm not sure what is different to the last change, other than using to_s or string concatenation?

@jmolina4
Copy link

@PuZZleDucK actually the problem is related with the concatenation:
https://stackoverflow.com/questions/16624131/what-do-def-and-def-mean

However, I don't see where is the problem exactly.

@PuZZleDucK
Copy link
Contributor Author

Ok, now I know and will never again forget why I was warned (but failed to listen to) the advice to always use interpolation and never concatenation... sorry, recovering java programmer :p

@PuZZleDucK
Copy link
Contributor Author

There seem to be no change or withdraw requests to address.
I'm not sure what to do with being "assigned" here... all I can do is update, comment or close.
Same with #1142 and #1146

@jmolina4
Copy link

I think you can merge the request @PuZZleDucK 😄 Thank you!

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?

Copy link
Member

@vbrazo vbrazo left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks for contributing 🥇

Just left a comment suggesting a minor change. I personally enjoyed the idea. Since I'm Brazilian, I'll try to add the Brazilian Portuguese ID Number methods.

@vbrazo
Copy link
Member

vbrazo commented May 15, 2018

btw I just created an issue with my idea 👏 👏

@PuZZleDucK
Copy link
Contributor Author

I like the constant idea... I'll get to that when I have a moment.
I don't have merge rights (as far as I can tell... and I have done merges before in other repos).

@vbrazo
Copy link
Member

vbrazo commented May 15, 2018

@PuZZleDucK I made an effort and pushed a few commits. I basically cleaned the code up with Rubocop and added the constant. I think it looks ok. Rubocop was added yesterday and it's integrated with Travis.

@vbrazo vbrazo merged commit 2b7c10b into faker-ruby:master May 15, 2018
@PuZZleDucK PuZZleDucK deleted the spanish-id-and-docs branch May 16, 2018 07:13
@vbrazo vbrazo self-requested a review July 19, 2018 01:28
davidmorton0 pushed a commit to davidmorton0/faker that referenced this pull request Jul 12, 2021
* update rake

* add id_number docs

* add tests for ssn

* add failing tests for spanish IDs

* add spanish citizen ids for issue faker-ruby#872

* cleanup comments

* give spanish_citizen_number correct checksum

* add validation checksum to Spanish NIE

* convert asserts into asert_equals

* replacing string concatenation with interpolation

* Minor changes

* Run rubocop and fix lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants