Sha256: 65cb407f947e1477f624d00b487b99044d41e94c793aa8334eb25fc4832d8364

Contents?: true

Size: 1.14 KB

Versions: 12

Compression:

Stored size: 1.14 KB

Contents

# encoding: UTF-8

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

require 'nokogiri'

module TwitterCldr
  module Resources

    class PhoneCodesImporter < Importer
      TERRITORY_REGEXP = /^[A-Z]{2}$/

      requirement :cldr, Versions.cldr_version
      output_path 'shared'
      ruby_engine :mri

      private

      def execute
        doc = File.open(File.join(source_path)) do |file|
          Nokogiri::XML(file)
        end

        phone_codes = doc.xpath('//codesByTerritory').each_with_object({}) do |node, memo|
          territory = node.attr('territory')

          if territory =~ TERRITORY_REGEXP
            memo[territory.downcase.to_sym] = node.at_xpath('telephoneCountryCode').attr('code')
          end
        end

        phone_codes = Hash[phone_codes.sort_by(&:first)]
        File.write(output_path, YAML.dump(phone_codes))
      end

      def output_path
        File.join(params.fetch(:output_path), 'phone_codes.yml')
      end

      def source_path
        @source_path ||= File.join(
          requirements[:cldr].common_path, 'supplemental', 'telephoneCodeData.xml'
        )
      end
    end

  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/twitter_cldr-4.4.5/lib/twitter_cldr/resources/phone_codes_importer.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/twitter_cldr-4.4.5/lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-4.4.5 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-4.4.4 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-4.4.3 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-4.4.2 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-4.4.1 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-4.4.0 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-4.3.1 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-4.3.0 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-4.2.0 lib/twitter_cldr/resources/phone_codes_importer.rb
twitter_cldr-4.1.0 lib/twitter_cldr/resources/phone_codes_importer.rb