Class: TwitterCldr::Resources::PhoneCodesImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_cldr/resources/phone_codes_importer.rb

Constant Summary collapse

PHONE_CODES_PATH =
File.join('common', 'supplemental', 'telephoneCodeData.xml')
TERRITORY_REGEXP =
/^[A-Z]{2}$/

Instance Method Summary collapse

Constructor Details

#initialize(input_path, output_path) ⇒ PhoneCodesImporter

Arguments:

input_path  - path to a directory containing CLDR data
output_path - output directory for generated YAML file


24
25
26
27
# File 'lib/twitter_cldr/resources/phone_codes_importer.rb', line 24

def initialize(input_path, output_path)
  @input_path  = input_path
  @output_path = output_path
end

Instance Method Details

#importObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/twitter_cldr/resources/phone_codes_importer.rb', line 29

def import
  TwitterCldr::Resources.download_cldr_if_necessary(@input_path)

  doc = File.open(File.join(@input_path, PHONE_CODES_PATH)) { |file| Nokogiri::XML(file) }

  phone_codes = doc.xpath('//codesByTerritory').inject({}) do |memo, node|
    territory = node.attr('territory')
    memo[territory.downcase.to_sym] = node.at_xpath('telephoneCountryCode').attr('code') if territory =~ TERRITORY_REGEXP
    memo
  end

  phone_codes = Hash[phone_codes.sort_by(&:first)]

  File.open(File.join(@output_path, 'phone_codes.yml'), 'w') { |output| output.write(YAML.dump(phone_codes)) }
end