Module: Amka
- Defined in:
- lib/amka.rb,
lib/amka/luhn.rb,
lib/amka/utils.rb,
lib/amka/version.rb
Overview
The Amka module provides functionality for validating and generating Greek A.M.K.A (social security) IDs as well as generic Luhn algorithm IDs. A.M.K.A IDs follow the Luhn algorithm and have their first 6 digits representing the date of birth in format DDMMYY.
An AMKA (Αριθμός Μητρώου Κοινωνικής Ασφάλισης) is a unique 11-digit number assigned to each Greek citizen or resident who works, pays social security contributions, or is entitled to healthcare. It follows these rules:
-
The first 6 digits represent the date of birth in format DDMMYY
-
The remaining 5 digits include a check digit based on the Luhn algorithm
-
The total length is always 11 digits
Defined Under Namespace
Classes: Error, Luhn, Utils, ValidationError
Constant Summary collapse
- VERSION =
'2.1.0'
Class Method Summary collapse
-
.generate(date_of_birth = nil) ⇒ String
Generates a random valid AMKA.
-
.valid?(amka, year = nil) ⇒ Boolean
Validates whether a given string is a valid AMKA.
-
.validate(amka, year = nil) ⇒ Array<String>
Validates an AMKA and returns an array of validation errors.
-
.validate!(amka, year = nil) ⇒ true
Strictly validates an AMKA and raises exceptions for invalid input.
Class Method Details
.generate(date_of_birth = nil) ⇒ String
Generates a random valid AMKA
This method can create an AMKA with either:
-
A random valid birth date (when called without parameters)
-
A specific birth date (when called with a date parameter)
The generated AMKA will always satisfy all validation rules:
-
11 digits in length
-
First 6 digits form a valid date
-
Passes the Luhn algorithm check
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/amka.rb', line 190 def self.generate(date_of_birth = nil) return generate_with_date_of_birth(date_of_birth) unless date_of_birth.nil? date_6_digits = String.new loop do day = "#{rand(0..3)}#{rand(0..9)}" next if day == '00' || day.to_i > 31 month = "#{rand(0..1)}#{rand(0..2)}" next if month == '00' || month.to_i > 12 year = "#{rand(19..20)}#{rand(0..9)}#{rand(0..9)}" next if year.to_i > Date.today.year date = day + month + year[2..3] if Utils.valid_date?(date, year) date_6_digits = date break end end Luhn.generate(11, date_6_digits) end |
.valid?(amka, year = nil) ⇒ Boolean
Validates whether a given string is a valid AMKA
The validation checks three conditions:
-
The AMKA must be exactly 11 digits long
-
The first 6 digits must form a valid date (DDMMYY format)
-
The entire number must satisfy the Luhn algorithm check
This method will always return a boolean and never raise exceptions, returning false for any input that doesn’t meet the AMKA criteria.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/amka.rb', line 60 def self.valid?(amka, year = nil) # Return false for non-string input return false unless amka.is_a?(String) # Return false for strings with non-digits return false unless amka.match(/\A\d+\Z/) # Check length requirement return false unless length_is_11?(amka) # Check if date and Luhn algorithm are valid begin return false unless Utils.valid_date?(amka, year) Luhn.safe_valid?(amka) rescue ArgumentError # If any validation raises an exception, the AMKA is invalid false end end |
.validate(amka, year = nil) ⇒ Array<String>
Validates an AMKA and returns an array of validation errors
This method provides detailed feedback about why validation failed, returning an empty array for valid AMKAs or an array of error messages for invalid ones.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/amka.rb', line 95 def self.validate(amka, year = nil) errors = [] # Check for basic format issues and return early if found basic_format_errors = check_basic_format(amka) return basic_format_errors unless basic_format_errors.empty? # Check length errors << 'AMKA must be exactly 11 digits long' unless length_is_11?(amka) # Check date format check_date(errors, amka, year) # Check Luhn algorithm check_luhn(errors, amka) errors end |
.validate!(amka, year = nil) ⇒ true
Strictly validates an AMKA and raises exceptions for invalid input
Similar to valid? but raises specific exceptions when validation fails, which is useful for cases where you need detailed error information or when you prefer exceptions for control flow.
164 165 166 167 168 169 170 |
# File 'lib/amka.rb', line 164 def self.validate!(amka, year = nil) errors = validate(amka, year) raise ValidationError, errors.first unless errors.empty? true end |