Skip to content

Instantly share code, notes, and snippets.

@juliends
Created October 23, 2017 16:50
Show Gist options
  • Save juliends/156a28512e877be4a8bf266156e43cda to your computer and use it in GitHub Desktop.
Save juliends/156a28512e877be4a8bf266156e43cda to your computer and use it in GitHub Desktop.
ssn livecode
require "date"
REG = /^(?<gender>[12])(?<year>\d{2})(?<month>\d{2})(?<dept>\d{2})\d{6}(?<key>\d{2})/
DEPARTEMENT = {
"75" => "Paris",
"76" => "Seine Maritime"
}
def ssn_info(ssn)
# TODO => "a man, born in March, 1986 in Paris."
ssn = ssn.gsub(" ","")
result = ssn.match(REG)
result[:gender] == "1" ? gender = "a man" : gender = "a woman"
year = "19#{result[:year]}"
month = Date::MONTHNAMES[result[:month].to_i]
dept = DEPARTEMENT[result[:dept]]
number = ssn[0..12].to_i
key = result[:key].to_i
expected_key = 97 - (number % 97)
if expected_key == key
"You are #{gender}, born in #{month} #{year}, in #{dept}"
else
"Not a valid number"
end
end
puts ssn_info("186037510803191")
puts ssn_info("286107511451175")
puts ssn_info("1 84 12 76 451 089 46")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment