Module: Phonelib::PhoneFormatter

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

Overview

module includes all formatting methods

Instance Method Summary collapse

Instance Method Details

#area_codeString|nil

returns area code of parsed number

Returns:

  • (String|nil)

    parsed phone area code if available



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_codeString

Returns the country code from the original phone number.

Returns:

  • (String)

    matched country phone code



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

#e164String

Returns e164 unformatted phone number

Returns:

  • (String)

    phone formatted in E164 format



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

def e164
  international = self.international
  international && international.gsub(/[^+0-9]/, '')
end

#full_e164String

returns e164 format of phone with extension added

Returns:

  • (String)

    phone formatted in E164 format with extension



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

def full_e164
  "#{e164}#{formatted_extension}"
end

#full_internationalString

returns international formatted number with extension added

Returns:

  • (String)

    formatted internation phone number with extension



65
66
67
# File 'lib/phonelib/phone_formatter.rb', line 65

def full_international
  "#{international}#{formatted_extension}"
end

#full_nationalString

returns national formatted number with extension added

Returns:

  • (String)

    formatted national number with extension



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

Parameters:

  • formatted (Boolean) (defaults to: true)

    whether to return numbers only or formatted

Returns:

  • (String)

    formatted international 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

Parameters:

  • formatted (Boolean) (defaults to: true)

    whether to return numbers only or formatted

Returns:

  • (String)

    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_nationalString

Returns the raw national number that was defined during parsing

Returns:

  • (String)

    raw national number



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