Class: Fakie::PhoneNumber

Inherits:
Object
  • Object
show all
Defined in:
lib/fakie/phone_number.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ PhoneNumber

Returns a new instance of PhoneNumber.

Parameters:

  • hash (Hash)

    hash of phone number data



28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

Instance Attribute Details

#area_codeObject (readonly)

Returns the value of attribute area_code.



20
21
22
# File 'lib/fakie/phone_number.rb', line 20

def area_code
  @area_code
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



18
19
20
# File 'lib/fakie/phone_number.rb', line 18

def country_code
  @country_code
end

#country_code_sourceObject (readonly)

Returns the value of attribute country_code_source.



22
23
24
# File 'lib/fakie/phone_number.rb', line 22

def country_code_source
  @country_code_source
end

#e164Object (readonly)

Returns the value of attribute e164.



17
18
19
# File 'lib/fakie/phone_number.rb', line 17

def e164
  @e164
end

#national_numberObject (readonly)

Returns the value of attribute national_number.



19
20
21
# File 'lib/fakie/phone_number.rb', line 19

def national_number
  @national_number
end

#preferred_domestic_carrier_codeObject (readonly)

Returns the value of attribute preferred_domestic_carrier_code.



23
24
25
# File 'lib/fakie/phone_number.rb', line 23

def preferred_domestic_carrier_code
  @preferred_domestic_carrier_code
end

#raw_inputObject (readonly)

Returns the value of attribute raw_input.



21
22
23
# File 'lib/fakie/phone_number.rb', line 21

def raw_input
  @raw_input
end

#region_codeObject (readonly)

Returns the value of attribute region_code.



24
25
26
# File 'lib/fakie/phone_number.rb', line 24

def region_code
  @region_code
end

#typeObject (readonly)

Returns the value of attribute type.



25
26
27
# File 'lib/fakie/phone_number.rb', line 25

def type
  @type
end

Class Method Details

.parse(phone_number, options = {}) ⇒ PhoneNumber

Parse a phone number

Parameters:

  • phone_number (String)

    phone number to parse

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • default_country (String)

    ISO 3166-1 two-letter country code

Returns:



8
9
10
11
12
13
14
# 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

Instance Method Details

#country_nameObject



68
69
70
71
# 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) ⇒ Object



54
55
56
57
58
59
# 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?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/fakie/phone_number.rb', line 46

def is_possible?
  @is_possible
end

#is_valid?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/fakie/phone_number.rb', line 50

def is_valid?
  @is_valid
end

#local_format(region = self.region_code) ⇒ Object



61
62
63
64
65
66
# 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_sObject



42
43
44
# File 'lib/fakie/phone_number.rb', line 42

def to_s
  self.e164
end