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
101 102 103 104 105 106 107 108 |
# File 'lib/phonelib/phone_formatter.rb', line 101 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
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/phonelib/phone_formatter.rb', line 89 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(prefix = '+') ⇒ String
Returns e164 unformatted phone number
82 83 84 85 |
# File 'lib/phonelib/phone_formatter.rb', line 82 def e164(prefix = '+') international = self.international(false, '') international && "#{prefix}#{international}" end |
#full_e164(prefix = '+') ⇒ String
returns e164 format of phone with extension added
75 76 77 |
# File 'lib/phonelib/phone_formatter.rb', line 75 def full_e164(prefix = '+') "#{e164(prefix)}#{formatted_extension}" end |
#full_international(prefix = '+') ⇒ String
returns international formatted number with extension added
68 69 70 |
# File 'lib/phonelib/phone_formatter.rb', line 68 def full_international(prefix = '+') "#{international(true, prefix)}#{formatted_extension}" end |
#full_national ⇒ String
returns national formatted number with extension added
61 62 63 |
# File 'lib/phonelib/phone_formatter.rb', line 61 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
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/phonelib/phone_formatter.rb', line 43 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 valid? return "#{prefix}#{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}#{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 |