class Phony::Country

Attributes

codes[RW]

Public Class Methods

new(*codes) click to toggle source

TODO Doc.

# File lib/phony/country.rb, line 19
def initialize *codes
  @codes = codes
end

Public Instance Methods

clean(number) click to toggle source

Clean number of all non-numeric characters and return a copy.

# File lib/phony/country.rb, line 140
def clean number
  clean! number && number.dup
end
clean!(number) click to toggle source

Clean number of all non-numeric characters and return it.

# File lib/phony/country.rb, line 145
def clean! number
  number.gsub!(@@basic_cleaning_pattern, EMPTY_STRING) || number
end
format(national_number, options = {}) click to toggle source

Format the number, given the national part of it.

# File lib/phony/country.rb, line 75
def format national_number, options = {}
  type         = options[:format]       || @format
  space        = options[:spaces]       || @space       || @@default_space
  local_space  = options[:local_spaces] || @local_space || space           || @@default_local_space
  parentheses  = options[:parentheses]
  parentheses  = @parentheses || @@default_parentheses if parentheses.nil?
  use_trunk    = options[:trunk]
  
  trunk, ndc, *local_pieces = split national_number
  
  local = format_local local_pieces, local_space
  
  format_cc_ndc trunk, ndc, local, type, space, parentheses, use_trunk
end
format_cc_ndc(trunk, ndc, local, type, space, parentheses, use_trunk) click to toggle source
# File lib/phony/country.rb, line 97
def format_cc_ndc trunk, ndc, local, type, space, parentheses, use_trunk
  case type
  when String
    trunk &&= trunk.format(space, use_trunk)
    type % { :trunk => trunk, :cc => @cc, :ndc => ndc, :local => local }
  when nil, :international_absolute, :international, :+
    if ndc
      format_with_ndc(@@international_absolute_format, @cc, format_ndc(ndc, parentheses), local, space)
    else
      format_without_ndc(@@international_absolute_format, @cc, local, space)
    end
  when :international_relative
    if ndc
      format_with_ndc(@@international_relative_format, @cc, format_ndc(ndc, parentheses), local, space)
    else
      format_without_ndc(@@international_relative_format, @cc, local, space)
    end
  when :national
    trunk &&= trunk.format(space, use_trunk)
    if ndc && !ndc.empty?
      @@national_format % [trunk, format_ndc(ndc, parentheses), space, local]
    else
      @@national_format % [trunk, nil, nil,   local]
    end
  when :local
    local
  end
end
format_local(local, local_space) click to toggle source
# File lib/phony/country.rb, line 89
def format_local local, local_space
  if local.empty?
    EMPTY_STRING
  else
    local.compact!
    local.join local_space.to_s
  end
end
format_ndc(ndc, parentheses) click to toggle source
# File lib/phony/country.rb, line 125
def format_ndc ndc, parentheses
  parentheses ? "(#{ndc})" : ndc
end
format_with_ndc(format, cc, ndc, local, space) click to toggle source
# File lib/phony/country.rb, line 128
def format_with_ndc format, cc, ndc, local, space
  format % [cc, space, ndc, space, local]
end
format_without_ndc(format, cc, local, space) click to toggle source
# File lib/phony/country.rb, line 131
def format_without_ndc format, cc, local, space
  format % [cc, space, local, nil, nil]
end
internal_split(national_number) click to toggle source

@return [Splitters::Local, Trunk, String (ndc), Array<String> (national pieces)]

# File lib/phony/country.rb, line 61
def internal_split national_number
  trunk = nil
  @codes.each do |national_splitter|
    new_trunk, ndc, *rest = national_splitter.split national_number
    trunk ||= new_trunk
    return [national_splitter.local_splitter, trunk, ndc, *rest] if rest && !rest.empty?
  end
  
  # Best effort.
  [nil, trunk, national_number, []]
end
normalize(national_number) click to toggle source

Removes 0s from partially normalized numbers such as 410443643533.

Example:

410443643533 -> 41443643533

In some cases it doesn’t, like Italy.

# File lib/phony/country.rb, line 157
def normalize national_number
  clean! national_number
  normalized = @codes.reduce national_number do |number, code|
    result = code.normalize number
    break result if result
    number
  end
  normalized
end
plausible?(rest, hints = {}) click to toggle source

Tests for plausibility of this national number.

# File lib/phony/country.rb, line 169
def plausible? rest, hints = {}
  local, _, ndc, *rest = internal_split rest

  # Element based checking.
  #
  # Note: ndc == false means the country has none.
  #
  return false if ndc.nil?
  return false if ndc && ndc.empty?
  return false if @invalid_ndcs && @invalid_ndcs === ndc

  # # A valid range for the rest is 0 or 3+ total digits.
  # #
  # return false if (1..2) === rest_size

  # National destination code plausible?
  #
  ndc_needed = hints[:ndc]
  return false if ndc_needed && !(ndc_needed === ndc)

  # Local code specific checks.
  #
  return local.plausible? rest, hints
end
split(national_number) click to toggle source

A number is split with the code handlers as given in the initializer.

Note: If the ndc is nil, it will not return it.

@return [Trunk, String (ndc), Array<String> (national pieces)]

# File lib/phony/country.rb, line 53
def split national_number
  _, trunk, ndc, *rest = internal_split national_number
  [trunk, ndc, *rest]
end
vanity?(national_number) click to toggle source

Is this national number a vanity number?

# File lib/phony/country.rb, line 196
def vanity? national_number
  Vanity.vanity? national_number
end
vanity_to_number(vanity_number) click to toggle source
# File lib/phony/country.rb, line 201
def vanity_to_number vanity_number
  _, ndc, *rest = split vanity_number
  "#{ndc}#{Vanity.replace(rest.join)}"
end
with(cc, options = {}) click to toggle source

Options.

TODO Rewrite.

# File lib/phony/country.rb, line 36
def with cc, options = {}
  @cc           = cc
  
  @invalid_ndcs = options[:invalid_ndcs]
  
  @format       = options[:format]
  @space        = options[:space]
  @local_space  = options[:local_space]
  @parentheses  = options[:parentheses]
end
|(other) click to toggle source

DSL method.

Chain two codes together.

# File lib/phony/country.rb, line 27
def | other
  self.codes = self.codes + other.codes
  self
end