Class: Phonie::Parser
- Inherits:
-
Object
- Object
- Phonie::Parser
- Defined in:
- lib/phonie/parser.rb
Overview
Parser is responsible for parsing and verifying phone numbers
Instance Attribute Summary collapse
-
#area_code ⇒ Object
readonly
Returns the value of attribute area_code.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#local_number_format ⇒ Object
readonly
Returns the value of attribute local_number_format.
-
#mobile_format ⇒ Object
readonly
Returns the value of attribute mobile_format.
-
#number_format ⇒ Object
readonly
Returns the value of attribute number_format.
Instance Method Summary collapse
-
#initialize(params) ⇒ Parser
constructor
Creates a new phone number parser based on a hash of
params
Theparams
hash requires * area_code * country_code - a number representing the International Subscriber Dialling code * local_number_format - a regex describing a local phone number (minus country and area code) * number_format - a regex describing a valid phone number * mobile_format (optional) - a regex describing a mobile phone number. -
#is_mobile?(number) ⇒ Boolean
Test if a phone
number
might be for a mobile phone Can produce false positives, and some countries have portable numbers between landlines and mobile phones in which case this will always be true. -
#is_valid_number?(phone_number) ⇒ Boolean
Test if
phone_number
is valid. -
#parse(number, default_area_code = nil) ⇒ Object
Parse a pass phone
number
returning it’s area_code & number as a hash if valid. -
#possible_valid_number?(phone_number, default_area_code = nil) ⇒ Boolean
Test if a phone number could possibly be valid by testing if it’s matches a number without a country code, and optionally testing for a local number against a default_area_code.
-
#valid? ⇒ Boolean
Test that a parser is valid and is capable of parsing phone numbers.
Constructor Details
#initialize(params) ⇒ Parser
Creates a new phone number parser based on a hash of params
The params
hash requires
-
area_code
-
country_code - a number representing the International Subscriber Dialling code
-
local_number_format - a regex describing a local phone number (minus country and area code)
-
number_format - a regex describing a valid phone number
-
mobile_format (optional) - a regex describing a mobile phone number
13 14 15 16 17 18 19 |
# File 'lib/phonie/parser.rb', line 13 def initialize(params) @area_code = params[:area_code] @country_code = params[:country_code] @local_number_format = params[:local_number_format] @mobile_format = params[:mobile_format] @number_format = params[:number_format] end |
Instance Attribute Details
#area_code ⇒ Object (readonly)
Returns the value of attribute area_code.
4 5 6 |
# File 'lib/phonie/parser.rb', line 4 def area_code @area_code end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
4 5 6 |
# File 'lib/phonie/parser.rb', line 4 def country_code @country_code end |
#local_number_format ⇒ Object (readonly)
Returns the value of attribute local_number_format.
4 5 6 |
# File 'lib/phonie/parser.rb', line 4 def local_number_format @local_number_format end |
#mobile_format ⇒ Object (readonly)
Returns the value of attribute mobile_format.
4 5 6 |
# File 'lib/phonie/parser.rb', line 4 def mobile_format @mobile_format end |
#number_format ⇒ Object (readonly)
Returns the value of attribute number_format.
4 5 6 |
# File 'lib/phonie/parser.rb', line 4 def number_format @number_format end |
Instance Method Details
#is_mobile?(number) ⇒ Boolean
Test if a phone number
might be for a mobile phone Can produce false positives, and some countries have portable numbers between landlines and mobile phones in which case this will always be true
25 26 27 28 |
# File 'lib/phonie/parser.rb', line 25 def is_mobile?(number) return true if mobile_format.nil? number =~ mobile_number_regex ? true : false end |
#is_valid_number?(phone_number) ⇒ Boolean
Test if phone_number
is valid
31 32 33 |
# File 'lib/phonie/parser.rb', line 31 def is_valid_number?(phone_number) matches_full_number?(phone_number) end |
#parse(number, default_area_code = nil) ⇒ Object
Parse a pass phone number
returning it’s area_code & number as a hash if valid. Optionally a default_area_code
may be passed in order to allow parsing local number numbers for a known area code
39 40 41 42 43 44 |
# File 'lib/phonie/parser.rb', line 39 def parse(number, default_area_code = nil) parse_full_match(number) || parse_area_code_match(number) || parse_with_default(number, default_area_code) || {} end |
#possible_valid_number?(phone_number, default_area_code = nil) ⇒ Boolean
Test if a phone number could possibly be valid by testing if it’s matches a number without a country code, and optionally testing for a local number against a default_area_code
49 50 51 52 53 |
# File 'lib/phonie/parser.rb', line 49 def possible_valid_number?(phone_number, default_area_code = nil) matches_full_number?(phone_number) || matches_local_number_with_area_code?(phone_number) || matches_local_number?(phone_number, default_area_code) end |
#valid? ⇒ Boolean
Test that a parser is valid and is capable of parsing phone numbers
56 57 58 |
# File 'lib/phonie/parser.rb', line 56 def valid? !!(country_code && area_code && local_number_format && number_format) end |