Module: Phonelib::Core

Included in:
PhoneValidator
Defined in:
lib/phonelib/core.rb

Overview

main module that includes all basic data and methods

Constant Summary collapse

GENERAL =

Validation patterns keys constants General pattern for country key

:general_desc
PREMIUM_RATE =

Freephone line pattern key

:premium_rate
TOLL_FREE =

Freephone line pattern key

:toll_free
SHARED_COST =

Shared cost pattern key. The cost of this call is shared between caller and recipient, and is hence typically less than PREMIUM_RATE calls

:shared_cost
VOIP =

VoIP pattern key. This includes TSoIP (Telephony Service over IP)

:voip
PERSONAL_NUMBER =

A personal number is associated with a particular person, and may be routed to either a MOBILE or FIXED_LINE number.

:personal_number
PAGER =

Pager phone number pattern key

:pager
UAN =

Used for ‘Universal Access Numbers’ or ‘Company Numbers’. They may be further routed to specific offices, but allow one number to be used for a company.

:uan
VOICEMAIL =

Used for ‘Voice Mail Access Numbers’.

:voicemail
FIXED_LINE =

Fixed line pattern key

:fixed_line
MOBILE =

Mobile phone number pattern key

:mobile
FIXED_OR_MOBILE =

In case MOBILE and FIXED patterns are the same, this type is returned

:fixed_or_mobile
VALID_PATTERN =

Internal use keys for validations Valid regex pattern key

:national_number_pattern
POSSIBLE_PATTERN =

Possible regex pattern key

:possible_number_pattern
NATIONAL_PREFIX =

National prefix key

:national_prefix
NATIONAL_PREFIX_RULE =

National prefix rule key

:national_prefix_formatting_rule
COUNTRY_CODE =

Country code key

:country_code
DEFAULT_NUMBER_FORMAT =

Default number formatting data hash

{
  pattern: '(\\d+)(\\d{3})(\\d{4})',
  format: '$1 $2 $3'
}
TYPES =

hash of all phone types with human representation

{
  general_desc: 'General Pattern',
  premium_rate: 'Premium Rate',
  toll_free: 'Toll Free',
  shared_cost: 'Shared Cost',
  voip: 'VoIP',
  personal_number: 'Personal Number',
  pager: 'Pager',
  uan: 'UAN',
  voicemail: 'VoiceMail',
  fixed_line: 'Fixed Line',
  mobile: 'Mobile',
  fixed_or_mobile: 'Fixed Line or Mobile'
}
NOT_FOR_CHECK =

array of types not included for validation check in cycle

[:general_desc, :fixed_line, :mobile, :fixed_or_mobile]
@@phone_data =

variable will include hash with data for validation

nil
@@default_country =

default country for parsing variable setting

nil

Instance Method Summary collapse

Instance Method Details

#default_countryObject

getter method for default_country variable



11
12
13
# File 'lib/phonelib/core.rb', line 11

def default_country
  @@default_country
end

#default_country=(country) ⇒ Object

setter method for default_country variable



16
17
18
# File 'lib/phonelib/core.rb', line 16

def default_country=(country)
  @@default_country = country
end

#impossible?(phone_number) ⇒ Boolean

method checks if passed phone number is impossible



125
126
127
# File 'lib/phonelib/core.rb', line 125

def impossible?(phone_number)
  parse(phone_number).impossible?
end

#invalid?(phone_number) ⇒ Boolean

method checks if passed phone number is invalid



115
116
117
# File 'lib/phonelib/core.rb', line 115

def invalid?(phone_number)
  parse(phone_number).invalid?
end

#invalid_for_country?(phone_number, country) ⇒ Boolean

method checks if passed phone number is invalid for provided country



135
136
137
# File 'lib/phonelib/core.rb', line 135

def invalid_for_country?(phone_number, country)
  parse(phone_number, country).invalid_for_country?(country)
end

#parse(phone, passed_country = nil) ⇒ Object

method for parsing phone number. On first run fills @@phone_data with data present in yaml file



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/phonelib/core.rb', line 92

def parse(phone, passed_country = nil)
  load_data

  country = country_or_default_country(passed_country)
  if phone.nil? || country.nil?
    # has to return instance of Phonelib::Phone even if no phone passed
    Phonelib::Phone.new(phone, @@phone_data)
  else
    detected = detect_and_parse_by_country(phone, country)
    if passed_country.nil? && @@default_country && detected.invalid?
      Phonelib::Phone.new(phone, @@phone_data)
    else
      detected
    end
  end
end

#possible?(phone_number) ⇒ Boolean

method checks if passed phone number is possible



120
121
122
# File 'lib/phonelib/core.rb', line 120

def possible?(phone_number)
  parse(phone_number).possible?
end

#valid?(phone_number) ⇒ Boolean

method checks if passed phone number is valid



110
111
112
# File 'lib/phonelib/core.rb', line 110

def valid?(phone_number)
  parse(phone_number).valid?
end

#valid_for_country?(phone_number, country) ⇒ Boolean

method checks if passed phone number is valid for provided country



130
131
132
# File 'lib/phonelib/core.rb', line 130

def valid_for_country?(phone_number, country)
  parse(phone_number, country).valid_for_country?(country)
end