Last active
June 11, 2020 11:49
-
-
Save sheesh19/d3c6fa826b14369b8612933de3b9df4c to your computer and use it in GitHub Desktop.
This file contains 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
SSN_PATTERN = /^(?<gender>[12])\s(?<year>\d{2})\s(?<month>\d{2})\s(?<zip>\d{2})\s(?<digits>\d{3}\s\d{3})\s(?<key>\d{2})/ | |
# define a method named french_ssn_info; takes one string parameter | |
# see if our ssn is a match for our pattern | |
# check IF we have a valid key | |
# (97 - rest of the numbers besides 46) % 97 MUST BE EQUAL TO key | |
# 1 == man, 2 == woman; check & store the gender | |
# return the year of birth (19) & store the full birth year | |
# convert month numbers to month names (require 'date') | |
# read our YAML file ('require yaml'); extract correct zip from yaml file -> LOOKS LIKE A HASH | |
# create the end string (lots of interpolation) | |
require 'date' | |
require 'yaml' | |
require 'pry' | |
# binding.pry | |
# MONTHNAMES = [0, January, February, March...][12] | |
def french_ssn_info(ssn) | |
match_ssn = ssn.match(SSN_PATTERN) | |
if match_ssn && valid_key?(ssn, match_ssn) | |
gender = gender(match_ssn) | |
year = "19#{match_ssn[:year]}" | |
month = Date::MONTHNAMES[match_ssn[:month].to_i] | |
zip = zip_yaml(match_ssn) | |
puts "a #{gender}, born in #{month}, #{year} in #{zip}" | |
else | |
puts "You are INVALID" | |
end | |
end | |
def valid_key?(ssn, match_ssn) | |
(97 - ssn.gsub(' ', '')[0..-3].to_i) % 97 == match_ssn[:key].to_i | |
end | |
def gender(match_ssn) | |
match_ssn[:gender] == "1" ? 'male' : 'female' | |
end | |
def zip_yaml(match_ssn) | |
YAML.load_file('data/french_departments.yml')[match_ssn[:zip]] | |
end | |
french_ssn_info('1 84 12 76 451 089 46') | |
french_ssn_info('1 84 46') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment