Module: Phonelib::PhoneFormatter
- Included in:
- Phone
- Defined in:
- lib/phonelib/phone_formatter.rb
Overview
module includes all formatting methods
Instance Method Summary collapse
-
#area_code ⇒ String|nil
returns area code of parsed number.
-
#country_code ⇒ String
Returns the country code from the original phone number.
-
#e164(prefix = '+') ⇒ String
Returns e164 unformatted phone number.
-
#full_e164(prefix = '+') ⇒ String
returns e164 format of phone with extension added.
-
#full_international(prefix = '+') ⇒ String
returns international formatted number with extension added.
-
#full_national ⇒ String
returns national formatted number with extension added.
-
#international(formatted = true, prefix = '+') ⇒ String
Returns e164 formatted phone number.
- #method_missing(method, *args) ⇒ Object
-
#national(formatted = true) ⇒ String
Returns formatted national number.
-
#raw_national ⇒ String
Returns the raw national number that was defined during parsing.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/phonelib/phone_formatter.rb', line 112 def method_missing(method, *args) prefix_methods = %w(international_ full_international_ e164_ full_e164_) method_s = method.to_s prefix_methods.each do |key| return send(key[0..-2], method_s.gsub(key, '')) if method_s.start_with?(key) end super end |
Instance Method Details
#area_code ⇒ String|nil
returns area code of parsed number
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/phonelib/phone_formatter.rb', line 100 def area_code return nil unless area_code_possible? format_match, _format_string = formatting_data take_group = 1 if type == Core::MOBILE && Core::AREA_CODE_MOBILE_TOKENS[country] && \ format_match[1] == Core::AREA_CODE_MOBILE_TOKENS[country] take_group = 2 end format_match[take_group] end |
#country_code ⇒ String
Returns the country code from the original phone number.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/phonelib/phone_formatter.rb', line 36 def country_code return @country_code if @country_code code = Phonelib.phone_data[country] && Phonelib.phone_data[country][Core::COUNTRY_CODE] return @country_code = code unless code == '1' && Phonelib.phone_data[country][Core::LEADING_DIGITS] match = e164.match(/\A\+(1(#{Phonelib.phone_data[country][Core::LEADING_DIGITS]}))/) if match @country_code = match[1] else @country_code = '1' end end |
#e164(prefix = '+') ⇒ String
Returns e164 unformatted phone number
93 94 95 96 |
# File 'lib/phonelib/phone_formatter.rb', line 93 def e164(prefix = '+') international = self.international(false, '') international && "#{prefix}#{international}" end |
#full_e164(prefix = '+') ⇒ String
returns e164 format of phone with extension added
86 87 88 |
# File 'lib/phonelib/phone_formatter.rb', line 86 def full_e164(prefix = '+') "#{e164(prefix)}#{formatted_extension}" end |
#full_international(prefix = '+') ⇒ String
returns international formatted number with extension added
79 80 81 |
# File 'lib/phonelib/phone_formatter.rb', line 79 def full_international(prefix = '+') "#{international(true, prefix)}#{formatted_extension}" end |
#full_national ⇒ String
returns national formatted number with extension added
72 73 74 |
# File 'lib/phonelib/phone_formatter.rb', line 72 def full_national "#{national}#{formatted_extension}" end |
#international(formatted = true, prefix = '+') ⇒ String
Returns e164 formatted phone number. Method can receive single string parameter that will be defined as prefix
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/phonelib/phone_formatter.rb', line 54 def international(formatted = true, prefix = '+') prefix = formatted if formatted.is_a?(String) return nil if sanitized.empty? return "#{prefix}#{country_prefix_or_not}#{sanitized}" unless possible? return "#{prefix}#{data_country_code}#{@national_number}" unless formatted fmt = @data[country][:format] national = @national_number if (matches = @national_number.match(cr(fmt[Core::PATTERN]))) fmt = fmt[:intl_format] || fmt[:format] national = fmt.gsub(/\$\d/) { |el| matches[el[1].to_i] } unless fmt == 'NA' end "#{prefix}#{data_country_code} #{national}" end |
#national(formatted = true) ⇒ String
Returns formatted national number
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/phonelib/phone_formatter.rb', line 9 def national(formatted = true) return @national_number unless possible? format_match, format_string = formatting_data if format_match out = format_string.gsub(/\$\d/) { |el| format_match[el[1].to_i] } formatted ? out : out.gsub(/[^0-9]/, '') else @national_number end end |
#raw_national ⇒ String
Returns the raw national number that was defined during parsing
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/phonelib/phone_formatter.rb', line 23 def raw_national return nil if sanitized.nil? || sanitized.empty? if valid? @national_number elsif data_country_code && sanitized.start_with?(data_country_code) sanitized[data_country_code.size..-1] else sanitized end end |