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



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fakie/phone_number.rb', line 25

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.



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

def area_code
  @area_code
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



15
16
17
# File 'lib/fakie/phone_number.rb', line 15

def country_code
  @country_code
end

#country_code_sourceObject (readonly)

Returns the value of attribute country_code_source.



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

def country_code_source
  @country_code_source
end

#e164Object (readonly)

Returns the value of attribute e164.



14
15
16
# File 'lib/fakie/phone_number.rb', line 14

def e164
  @e164
end

#national_numberObject (readonly)

Returns the value of attribute national_number.



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

def national_number
  @national_number
end

#preferred_domestic_carrier_codeObject (readonly)

Returns the value of attribute preferred_domestic_carrier_code.



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

def preferred_domestic_carrier_code
  @preferred_domestic_carrier_code
end

#raw_inputObject (readonly)

Returns the value of attribute raw_input.



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

def raw_input
  @raw_input
end

#region_codeObject (readonly)

Returns the value of attribute region_code.



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

def region_code
  @region_code
end

#typeObject (readonly)

Returns the value of attribute type.



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

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
# File 'lib/fakie/phone_number.rb', line 8

def parse(phone_number, options = {})
  region_code = options[:default_country]
  self.new(JavaScript.call('Fakie.parse', phone_number, region_code))
end

Instance Method Details

#country_nameObject



57
58
59
60
# File 'lib/fakie/phone_number.rb', line 57

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

Raises:



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

def international_format(region = self.region_code)
  raise InvalidPhoneNumber unless self.is_valid?
  @international_format ||= JavaScript.call('Fakie.formatInternational', region, self.e164)
end

#is_possible?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/fakie/phone_number.rb', line 39

def is_possible?
  @is_possible
end

#is_valid?Boolean

Returns:

  • (Boolean)


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

def is_valid?
  @is_valid
end

#local_format(region = self.region_code) ⇒ Object

Raises:



52
53
54
55
# File 'lib/fakie/phone_number.rb', line 52

def local_format(region = self.region_code)
  raise InvalidPhoneNumber unless self.is_valid?
  @local_format ||= JavaScript.call('Fakie.formatLocal', region, self.e164)
end