Created
April 23, 2018 17:46
-
-
Save rodloboz/56104f30274cba8ea2433a8a91af91a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SETUP: | |
# touch french_ssn.rb | |
# mkdir spec | |
# touch spec/french_ssn_spec.rb | |
# mkdir data | |
# touch data/french_departments.yml | |
# curl -L https://gist.githubusercontent.com/Eschults/279682088e2f87564161e4dbf8390d68/raw/bdbc97ed036a5b21978defd9987e800c7fa67dce > 'data/french_departments.yml' | |
require 'date' | |
require 'yaml' | |
require 'pry-byebug' | |
SSN_PATTERN = /\A(?<gender>[10])\s?(?<year>\d{2})\s?(?<month>(0[1-9])|(1[12]))\s?(?<department>\d[1-9]|2[AB])\s?(\d{3}\s?){2}(?<key>\d{2})\Z/ | |
DEPARTMENTS = YAML.load_file('data/french_departments.yml') | |
def french_ssn_info(ssn) | |
match_data = ssn.match(SSN_PATTERN) | |
if match_data && valid_key?(ssn, match_data[:key]) | |
gender = gender(match_data[:gender]) | |
month = month(match_data[:month]) | |
year = "19" + match_data[:year] | |
department = department(match_data[:department]) | |
"a #{gender}, born in #{month}, #{year} in #{department}." | |
else | |
"The number is invalid" | |
end | |
end | |
def gender(code) | |
code == "1" ? "man" : "woman" | |
end | |
def month(code) | |
Date::MONTHNAMES[code.to_i] | |
end | |
def department(code) | |
DEPARTMENTS[code] | |
end | |
def valid_key?(ssn, key) | |
# equal to the remainder of the division of (97 - ssn_without_key) by 97.) | |
ssn_without_key = ssn[0, ssn.length - 2].gsub(/\W/, '') | |
key.to_i == (97 - ssn_without_key.to_i) % 97 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_relative "../french_ssn" | |
describe "#french_ssn_info" do | |
it "should return 'The number is invalid' when passed an empty string" do | |
actual = french_ssn_info("") | |
expected = "The number is invalid" | |
expect(actual).to eq(expected) | |
end | |
it "should return 'a man, born in December, 1984 in Seine-Maritime.' when passed a valid social security number" do | |
expected = "a man, born in December, 1984 in Seine-Maritime." | |
valid_ssn1 = french_ssn_info("1 84 12 76 451 089 46") | |
expect(valid_ssn1).to eq(expected) | |
valid_ssn2 = french_ssn_info("184127645108946") | |
expect(valid_ssn2).to eq(expected) | |
end | |
end | |
describe "#gender" do | |
it "should return 'man' if passed '1'" do | |
actual = gender("1") | |
expected = "man" | |
expect(actual).to eq(expected) | |
end | |
it "should return 'woman' if passed '2'" do | |
actual = gender("2") | |
expected = "woman" | |
expect(actual).to eq(expected) | |
end | |
end | |
describe "#month" do | |
it "should return 'January' if passed '01'" do | |
actual = month("01") | |
expected = "January" | |
expect(actual).to eq(expected) | |
end | |
it "should return 'December' if passed '12'" do | |
actual = month("12") | |
expected = "December" | |
expect(actual).to eq(expected) | |
end | |
end | |
describe "#department" do | |
it "should return 'Charente-Maritime' if passed '17'" do | |
actual = department("17") | |
expected = "Charente-Maritime" | |
expect(actual).to eq(expected) | |
end | |
end | |
describe "#valid_key?" do | |
it "should return true if passed '1 84 12 76 451 089 46', '46'" do | |
valid_key = valid_key?('1 84 12 76 451 089 46', '46') | |
expect(valid_key).to be(true) | |
end | |
it "should return false if passed '1 84 12 76 451 089 47', '47'" do | |
valid_key = valid_key?('1 84 12 76 451 089 47', '47') | |
expect(valid_key).to be(false) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment