Class: Phonejack::Country

Inherits:
Object
  • Object
show all
Defined in:
lib/phonejack/country.rb

Constant Summary collapse

MOBILE_TOKEN_COUNTRIES =
{ AR: '9' }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_hash) ⇒ Country

Returns a new instance of Country.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/phonejack/country.rb', line 9

def initialize(data_hash)
  @country_code = data_hash[:country_code]
  @country_id = data_hash[:id]
  @formats = data_hash.fetch(:formats, []).map { |format| NumberFormat.new(format) }
  @general_validation = NumberValidation.new(:general_desc, data_hash[:validations][:general_desc]) if data_hash.fetch(:validations, {})[:general_desc]
  @international_prefix = Regexp.new(data_hash[:international_prefix]) if data_hash[:international_prefix]
  @main_country_for_code = data_hash[:main_country_for_code] == 'true'
  @mobile_token = MOBILE_TOKEN_COUNTRIES[@country_id.to_sym]
  @national_prefix = data_hash[:national_prefix]
  @national_prefix_formatting_rule = data_hash[:national_prefix_formatting_rule]
  @national_prefix_for_parsing = Regexp.new(data_hash[:national_prefix_for_parsing]) if data_hash[:national_prefix_for_parsing]
  @national_prefix_transform_rule = data_hash[:national_prefix_transform_rule]
  @validations = data_hash.fetch(:validations, {})
                          .except(:general_desc, :area_code_optional)
                          .map { |name, data| NumberValidation.new(name, data) }
end

Instance Attribute Details

#country_codeObject (readonly)

Returns the value of attribute country_code.



3
4
5
# File 'lib/phonejack/country.rb', line 3

def country_code
  @country_code
end

#country_idObject (readonly)

Returns the value of attribute country_id.



3
4
5
# File 'lib/phonejack/country.rb', line 3

def country_id
  @country_id
end

#formatsObject (readonly)

Returns the value of attribute formats.



3
4
5
# File 'lib/phonejack/country.rb', line 3

def formats
  @formats
end

#general_validationObject (readonly)

Returns the value of attribute general_validation.



3
4
5
# File 'lib/phonejack/country.rb', line 3

def general_validation
  @general_validation
end

#international_prefixObject (readonly)

Returns the value of attribute international_prefix.



3
4
5
# File 'lib/phonejack/country.rb', line 3

def international_prefix
  @international_prefix
end

#main_country_for_codeObject (readonly)

Returns the value of attribute main_country_for_code.



3
4
5
# File 'lib/phonejack/country.rb', line 3

def main_country_for_code
  @main_country_for_code
end

#mobile_tokenObject (readonly)

Returns the value of attribute mobile_token.



3
4
5
# File 'lib/phonejack/country.rb', line 3

def mobile_token
  @mobile_token
end

#national_prefixObject (readonly)

Returns the value of attribute national_prefix.



3
4
5
# File 'lib/phonejack/country.rb', line 3

def national_prefix
  @national_prefix
end

#national_prefix_for_parsingObject (readonly)

Returns the value of attribute national_prefix_for_parsing.



3
4
5
# File 'lib/phonejack/country.rb', line 3

def national_prefix_for_parsing
  @national_prefix_for_parsing
end

#national_prefix_formatting_ruleObject (readonly)

Returns the value of attribute national_prefix_formatting_rule.



3
4
5
# File 'lib/phonejack/country.rb', line 3

def national_prefix_formatting_rule
  @national_prefix_formatting_rule
end

#national_prefix_transform_ruleObject (readonly)

Returns the value of attribute national_prefix_transform_rule.



3
4
5
# File 'lib/phonejack/country.rb', line 3

def national_prefix_transform_rule
  @national_prefix_transform_rule
end

#validationsObject (readonly)

Returns the value of attribute validations.



3
4
5
# File 'lib/phonejack/country.rb', line 3

def validations
  @validations
end

Class Method Details

.all_countriesObject



63
64
65
# File 'lib/phonejack/country.rb', line 63

def self.all_countries
  @all_countries ||= phone_data.values.map {|data| new(data)}
end

.find(country_id) ⇒ Object



50
51
52
53
# File 'lib/phonejack/country.rb', line 50

def self.find(country_id)
  data = phone_data[country_id.to_s.upcase.to_sym]
  new(data) if data
end

.load_dataObject



55
56
57
58
59
60
61
# File 'lib/phonejack/country.rb', line 55

def self.load_data
  data_file = "#{File.dirname(__FILE__)}/../../data/telephone_number_data_file.dat"
  main_data = Marshal.load(File.binread(data_file))
  override_data = {}
  override_data = Marshal.load(File.binread(Phonejack.override_file)) if Phonejack.override_file
  return main_data.deep_deep_merge!(override_data)
end

.phone_dataObject



46
47
48
# File 'lib/phonejack/country.rb', line 46

def self.phone_data
  @phone_data ||= load_data
end

Instance Method Details

#detect_format(number) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/phonejack/country.rb', line 26

def detect_format(number)
  native_format = formats.detect do |format|
    number =~ Regexp.new("^(#{format.leading_digits})") && number =~ Regexp.new("^(#{format.pattern})$")
  end

  return native_format if native_format || main_country_for_code
  parent_country.detect_format(number) if parent_country
end

#full_general_patternObject



42
43
44
# File 'lib/phonejack/country.rb', line 42

def full_general_pattern
  %r{^(#{country_code})?(#{national_prefix})?(?<national_num>#{general_validation.pattern})$}
end

#parent_countryObject



35
36
37
38
39
40
# File 'lib/phonejack/country.rb', line 35

def parent_country
  return if main_country_for_code
  Country.all_countries.detect do |country|
    country.country_code == self.country_code && country.main_country_for_code
  end
end