Module: Phonelib::PhoneFormatter

Included in:
Phone
Defined in:
lib/phonelib/phone_formatter.rb

Overview

module includes all formatting methods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/phonelib/phone_formatter.rb', line 110

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_codeString|nil

returns area code of parsed number



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/phonelib/phone_formatter.rb', line 98

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_codeString

Returns the country code from the original phone number.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/phonelib/phone_formatter.rb', line 34

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



91
92
93
94
# File 'lib/phonelib/phone_formatter.rb', line 91

def e164(prefix = '+')
  international = self.international(false, '')
  international && "#{prefix}#{international}"
end

#full_e164(prefix = '+') ⇒ String

returns e164 format of phone with extension added



84
85
86
# File 'lib/phonelib/phone_formatter.rb', line 84

def full_e164(prefix = '+')
  "#{e164(prefix)}#{formatted_extension}"
end

#full_international(prefix = '+') ⇒ String

returns international formatted number with extension added



77
78
79
# File 'lib/phonelib/phone_formatter.rb', line 77

def full_international(prefix = '+')
  "#{international(true, prefix)}#{formatted_extension}"
end

#full_nationalString

returns national formatted number with extension added



70
71
72
# File 'lib/phonelib/phone_formatter.rb', line 70

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/phonelib/phone_formatter.rb', line 52

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



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 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_nationalString

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 data_country_code && sanitized.start_with?(data_country_code)
    sanitized[data_country_code.size..-1]
  else
    sanitized
  end
end