Skip to content

Instantly share code, notes, and snippets.

@sheesh19
Last active June 11, 2020 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheesh19/d3c6fa826b14369b8612933de3b9df4c to your computer and use it in GitHub Desktop.
Save sheesh19/d3c6fa826b14369b8612933de3b9df4c to your computer and use it in GitHub Desktop.
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