Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created July 2, 2018 10:46
Show Gist options
  • Save rodloboz/cb4bfaef943a58f0b898544afb192e18 to your computer and use it in GitHub Desktop.
Save rodloboz/cb4bfaef943a58f0b898544afb192e18 to your computer and use it in GitHub Desktop.
lw-livecode
require 'date'
require 'yaml'
PATTERN = /^(?<gender>[12])\s?(?<yob>\d{2})\s?(?<mob>0[1-9]|1[0-2])\s?(?<department>\d\d|2[AB])\s?\d{3}\s?\d{3}\s?(?<key>\d\d)$/
# hash
DEPARTMENTS = YAML.load_file('./data/french_departments.yml')
def french_ssn(ssn)
return "The number is invalid" if ssn.empty?
match_data = PATTERN.match(ssn)
gender = gender(match_data[:gender])
p gender
month = month(match_data[:mob])
p month
year = year(match_data[:yob])
department = DEPARTMENTS[match_data[:department]]
valid_key?(ssn, match_data[:key])
"a #{gender}, born in #{month}, #{year} in #{department}."
end
def gender(code)
code == "1" ? "man" : "woman"
end
def month(code)
Date::MONTHNAMES[code.to_i]
end
def year(code)
"19" + code
end
def valid_key?(ssn, key)
# A 2 digits key (46, equal
# to the remainder of the division of
# (97 - ssn_without_key) by 97.)
ssn_without_key = ssn.gsub(' ', '')[0..-3].to_i
remainder = (97 - ssn_without_key) % 97
key.to_i == remainder
end
# french_ssn("295062A65734578")
valid_key?("1 84 12 76 451 089 46", "46")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment