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 ⇒ String
Returns e164 unformatted phone number.
-
#full_e164 ⇒ String
returns e164 format of phone with extension added.
-
#full_international ⇒ String
returns international formatted number with extension added.
-
#full_national ⇒ String
returns national formatted number with extension added.
-
#international(formatted = true) ⇒ String
Returns e164 formatted phone number.
-
#national(formatted = true) ⇒ String
Returns formatted national number.
-
#raw_national ⇒ String
Returns the raw national number that was defined during parsing.
Instance Method Details
#area_code ⇒ String|nil
returns area code of parsed number
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/phonelib/phone_formatter.rb', line 84 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.
34 35 36 37 |
# File 'lib/phonelib/phone_formatter.rb', line 34 def country_code @country_code ||= Phonelib.phone_data[country] && \ Phonelib.phone_data[country][Core::COUNTRY_CODE] end |
#e164 ⇒ String
Returns e164 unformatted phone number
77 78 79 80 |
# File 'lib/phonelib/phone_formatter.rb', line 77 def e164 international = self.international international && international.gsub(/[^+0-9]/, '') end |
#full_e164 ⇒ String
returns e164 format of phone with extension added
71 72 73 |
# File 'lib/phonelib/phone_formatter.rb', line 71 def full_e164 "#{e164}#{formatted_extension}" end |
#full_international ⇒ String
returns international formatted number with extension added
65 66 67 |
# File 'lib/phonelib/phone_formatter.rb', line 65 def full_international "#{international}#{formatted_extension}" end |
#full_national ⇒ String
returns national formatted number with extension added
59 60 61 |
# File 'lib/phonelib/phone_formatter.rb', line 59 def full_national "#{national}#{formatted_extension}" end |
#international(formatted = true) ⇒ String
Returns e164 formatted phone number
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/phonelib/phone_formatter.rb', line 42 def international(formatted = true) return nil if sanitized.empty? return "+#{country_prefix_or_not}#{sanitized}" unless valid? return 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] } end "+#{country_code} #{national}" end |
#national(formatted = true) ⇒ String
Returns formatted national number
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/phonelib/phone_formatter.rb', line 7 def national(formatted = true) return @national_number unless valid? 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
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/phonelib/phone_formatter.rb', line 21 def raw_national return nil if sanitized.nil? || sanitized.empty? if valid? @national_number elsif country_code && sanitized.start_with?(country_code) sanitized[country_code.size..-1] else sanitized end end |