Sha256: 7287ee41a13ce12b9eace98acfedb1c1a6b400ba6921691786dd71f1ee08f81c

Contents?: true

Size: 1.29 KB

Versions: 39

Compression:

Stored size: 1.29 KB

Contents

# encoding: UTF-8

# Copyright 2012 Twitter, Inc
# http://www.apache.org/licenses/LICENSE-2.0

require 'nokogiri'

require 'twitter_cldr/resources/download'

module TwitterCldr
  module Resources

    class PhoneCodesImporter

      PHONE_CODES_PATH = File.join('common', 'supplemental', 'telephoneCodeData.xml')

      TERRITORY_REGEXP = /^[A-Z]{2}$/

      # Arguments:
      #
      #   input_path  - path to a directory containing CLDR data
      #   output_path - output directory for generated YAML file
      #
      def initialize(input_path, output_path)
        @input_path  = input_path
        @output_path = output_path
      end

      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

    end

  end
end

Version data entries

39 entries across 39 versions & 2 rubygems

Version Path
twitter_cldr-4.0.0 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.6.0 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.5.0 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.4.0 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.3.0 lib/twitter_cldr/resources/phone_codes_importer.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/twitter_cldr-3.2.1/lib/twitter_cldr/resources/phone_codes_importer.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/twitter_cldr-3.2.1/lib/twitter_cldr/resources/phone_codes_importer.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/twitter_cldr-3.1.2/lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.2.1 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.2.0 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.1.2 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.1.1 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.1.0 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.0.10 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.0.9 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.0.8 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.0.7 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.0.6 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.0.5 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-3.0.4 lib/twitter_cldr/resources/phone_codes_importer.rb