Created
January 18, 2021 17:57
-
-
Save progapandist/c4246aec878888a65c02d49538a9d368 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 'pry' | |
require 'date' | |
require 'frenchdepartments' | |
# The way to stop the program and land in a console in the middle of a run | |
# Put in any right place of your choosing | |
# binding.pry | |
# Gender (1 == man, 2 == woman) | |
# Year of birth (84) | |
# Month of birth (12) | |
# Department of birth (76, basically included between 01 and 99) | |
# 6 random digits (451 089) | |
# A 2 digits key (46, equal to the remainder of the division of (97 - ssn_without_key) by 97.) | |
REGEX = /^(?<gender>[1-2])(?<year_of_birth>\d{2})(?<month_of_birth>(0[1-9]|1[0-2]))(?<department>(0[1-9]|[1-9]\d))(?<random>\d{6})(?<key>\d{2})/ | |
def french_ssn_info(str) | |
str = str.delete(" ") | |
result = str.match(REGEX) # MatchData | |
# Guard statement | |
return "The number is invalid" if result.nil? | |
# We extract captures from matchdata and compose a sentence | |
# Implementation part0 | |
gender = result[:gender] == 1 ? "woman" : "man" | |
month_name = Date::MONTHNAMES[result[:month_of_birth].to_i] # 01 - 12 | |
year = "19#{result[:year_of_birth]}" | |
region = FrenchDepartments.all_departments_names[result[:department].to_i] # "01-99" | |
return "a #{gender}, born in #{month_name}, #{year} in #{region}." | |
end | |
# # String | |
# p french_ssn_info("1 84 12 76 451 089 46") # API application programming interface | |
# # "a man, born in December, 1984 in Seine-Maritime." # Str | |
# p french_ssn_info("123") | |
# # "The number is invalid" # Str |
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.rb" | |
describe "#french_ssn_info" do | |
it "should print a message if the number is invalid" do | |
actual = french_ssn_info("123") | |
expected = "The number is invalid" | |
expect(actual).to eq(expected) | |
end | |
it "should print invalid if passed a non-numeric string" do | |
actual = french_ssn_info("YOLO") | |
expected = "The number is invalid" | |
expect(actual).to eq(expected) | |
end | |
it "should compose a sentence if the number is valid" 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 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment