class Fakie::PhoneNumber

Attributes

area_code[R]
country_code[R]
country_code_source[R]
e164[R]
national_number[R]
preferred_domestic_carrier_code[R]
raw_input[R]
region_code[R]
type[R]

Public Class Methods

new(hash) click to toggle source

@param hash [Hash] hash of phone number data

# File lib/fakie/phone_number.rb, line 28
def initialize(hash)
  @e164 = hash['e164']
  @country_code = hash['country_code']
  @national_number = hash['national_number']
  @area_code = hash['area_code']
  @raw_input = hash['raw_input'].to_s
  @country_code_source = hash['country_code_source']
  @preferred_domestic_carrier_code = hash['preferred_domestic_carrier_code']
  @is_possible = hash['is_possible']
  @is_valid = hash['is_valid']
  @region_code = hash['region']
  @type = hash['type']
end
parse(phone_number, options = {}) click to toggle source

Parse a phone number @param phone_number [String] phone number to parse @option options default_country [String] ISO 3166-1 two-letter country code @return [PhoneNumber] phone number object

# File lib/fakie/phone_number.rb, line 8
def parse(phone_number, options = {})
  options = Fakie.default_options.merge(options)
  region_code = options[:default_country]
  self.new(JavaScript.call('Fakie.parse', phone_number, region_code))
rescue ExecJS::ProgramError => e
  self.new({'raw_input' => phone_number, 'is_valid' => false})
end

Public Instance Methods

country_name() click to toggle source
# File lib/fakie/phone_number.rb, line 68
def country_name
  return nil unless self.region_code
  Fakie.country_name_for_region_code(self.region_code)
end
international_format(region = self.region_code) click to toggle source
# File lib/fakie/phone_number.rb, line 54
def international_format(region = self.region_code)
  raise InvalidPhoneNumber, self.raw_input unless self.is_valid?
  @international_format ||= JavaScript.call('Fakie.formatInternational', region, self.e164)
rescue ExecJS::ProgramError => e
  self.e164
end
is_possible?() click to toggle source
# File lib/fakie/phone_number.rb, line 46
def is_possible?
  @is_possible
end
is_valid?() click to toggle source
# File lib/fakie/phone_number.rb, line 50
def is_valid?
  @is_valid
end
local_format(region = self.region_code) click to toggle source
# File lib/fakie/phone_number.rb, line 61
def local_format(region = self.region_code)
  raise InvalidPhoneNumber, self.raw_input unless self.is_valid?
  @local_format ||= JavaScript.call('Fakie.formatLocal', region, self.e164)
rescue ExecJS::ProgramError => e
  self.e164
end
to_s() click to toggle source
# File lib/fakie/phone_number.rb, line 42
def to_s
  self.e164
end