Skip to content

Instantly share code, notes, and snippets.

@krokrob
Created April 11, 2016 16:51
Show Gist options
  • Save krokrob/b80623d0828893b5b4dc35ea3c839e10 to your computer and use it in GitHub Desktop.
Save krokrob/b80623d0828893b5b4dc35ea3c839e10 to your computer and use it in GitHub Desktop.
require "date"
DPT = {
"76" => "Seine-Maritime",
"75" => "Paris"
}
REG_SSN = /^(?<gender>1|2)(?<year>\d{2})(?<month>0[1-9]|1[0-2])(?<zip>\d{2})\d{6}(?<key>\d{2})/
def ssn_info(ssn)
# TODO: return a string like this
# The number is valid: a man, born in December, 1984 in Seine-Maritime.
ssn = ssn.gsub(/\s/, "")
data = ssn.match(REG_SSN)
if data.nil?
return "The number is invalid."
else
calcul = 97 - (ssn[0...-2].to_i % 97)
if calcul == data[:key].to_i
if data[:gender] == "1"
gender = "man"
else
gender = "woman"
end
month = Date::MONTHNAMES[data[:month].to_i]
return "The number is valid: a #{gender}, born in #{month}, 19#{data[:year]} in #{DPT[data[:zip]]}"
else
return "The number is invalid."
end
end
end
numbers = [
"1 84 12 76 451 089 46",
"2 86 10 75 114 511 75",
"2 86 10 75 114 511 74",
"123"
]
numbers.each do |number|
puts ssn_info(number)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment