Created
April 26, 2021 17:11
-
-
Save Oceantidote/befbd3b1fb753b1b70a9e13c873c794d 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
require_relative "../french_ssn" | |
describe "#french_ssn_info" do | |
it "returns '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 "returns 'a man, born in December, 1984 in Seine-Maritime.' when passed '1 84 12 76 451 089 46'" do | |
actual = french_ssn_info("1 84 12 76 451 089 46") | |
expected = "a man, born in December, 1984 in Seine-Maritime." | |
expect(actual).to eq(expected) | |
end | |
it "returns 'The number is invalid' when passed '1 84 12 76 451 089 32'" do | |
actual = french_ssn_info("1 84 12 76 451 089 32") | |
expected = "The number is invalid" | |
expect(actual).to eq(expected) | |
end | |
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 "pry-byebug" | |
require "Date" | |
def french_ssn_info(number) | |
depts = YAML.load_file('data/french_departments.yml') | |
regex = /^(?<gender>\d)\s(?<year>\d{2})\s(?<month>1[0-2]|0[1-9])\s(?<dept>\d{2})(\s\d{3}){2}\s(?<key>\d{2})/ | |
matchdata = number.match(regex) | |
return "The number is invalid" if number == "" || !valid?(number, matchdata[:key]) | |
months = Date::MONTHNAMES | |
sentence = "a #{get_gender(matchdata[:gender])}, born in #{months[matchdata[:month].to_i]}, #{get_year(matchdata[:year])} in #{depts[matchdata[:dept]]}." | |
end | |
def get_gender(num) | |
if num == "1" | |
"man" | |
else | |
"woman" | |
end | |
end | |
def get_year(year) | |
if year.to_i < Time.now.year - 2000 | |
"20#{year}" | |
else | |
"19#{year}" | |
end | |
end | |
def valid?(number, key) | |
new_number = 97 - number.split(" ")[0..-2].join('').to_i | |
new_number % 97 == key.to_i | |
end | |
puts french_ssn_info("1 84 12 76 451 089 36") | |
# a man, born in December, 1984 in Seine-Maritime. | |
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 "returns '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 "returns 'a man, born in December, 1984 in Seine-Maritime.' when passed '1 84 12 76 451 089 46'" do | |
actual = french_ssn_info("1 84 12 76 451 089 46") | |
expected = "a man, born in December, 1984 in Seine-Maritime." | |
expect(actual).to eq(expected) | |
end | |
it "returns 'The number is invalid' when passed '1 84 12 76 451 089 32'" do | |
actual = french_ssn_info("1 84 12 76 451 089 32") | |
expected = "The number is invalid" | |
expect(actual).to eq(expected) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment