Class: ITU::E164::Checker
- Inherits:
-
Object
- Object
- ITU::E164::Checker
- Defined in:
- lib/itu/e164/checker.rb
Constant Summary collapse
- ASCII_INTERNATIONAL_NUMBER =
/\A\+[0-9]+\z/- MAXIMUM_DIGITS =
15- SUPPORTED_PROFILES =
[:geographic].freeze
- ERROR_MESSAGES =
{ not_a_string: "must be a String", invalid_representation: "must be + followed by ASCII decimal digits", too_long: "must contain at most 15 digits after +", country_code_unassigned: "must start with an assigned geographic country code", missing_national_number: "must include a national significant number after the country code", profile_mismatch: "belongs to a non-geographic E.164 category" }.freeze
Instance Method Summary collapse
- #call(input) ⇒ Object
-
#initialize(profile:) ⇒ Checker
constructor
A new instance of Checker.
Constructor Details
#initialize(profile:) ⇒ Checker
Returns a new instance of Checker.
22 23 24 25 26 27 28 |
# File 'lib/itu/e164/checker.rb', line 22 def initialize(profile:) unless SUPPORTED_PROFILES.include?(profile) raise ArgumentError, "unsupported profile #{profile.inspect}; expected :geographic" end @profile = profile end |
Instance Method Details
#call(input) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/itu/e164/checker.rb', line 30 def call(input) return result(input:, errors: [error(:not_a_string)]) unless input.is_a?(String) stable_input = immutable(input) unless ASCII_INTERNATIONAL_NUMBER.match?(stable_input) return result(input: stable_input, errors: [error(:invalid_representation)]) end digits = immutable(stable_input.delete_prefix("+")) errors = [] errors << error(:too_long) if digits.length > MAXIMUM_DIGITS category, country_code = resolve_country_code(digits) national_number = resolve_national_number(digits, category, country_code) if category.nil? errors << error(:country_code_unassigned) elsif category != @profile errors << error(:profile_mismatch) elsif national_number.empty? errors << error(:missing_national_number) end result( input: stable_input, digits:, canonical: errors.empty? ? stable_input : nil, category:, country_code:, national_number:, errors: ) end |