Sha256: b4ed346c919cadd17609065fb93c4da77adfe95d8eb2479c7f3c5e1584a50744

Contents?: true

Size: 1.15 KB

Versions: 17

Compression:

Stored size: 1.15 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 PostalCodesImporter

      POSTAL_CODES_PATH = File.join('common', 'supplemental', 'postalCodeData.xml')

      # 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, POSTAL_CODES_PATH)) { |file| Nokogiri::XML(file) }

        postal_codes = doc.xpath('//postCodeRegex').inject({}) do |memo, node|
          memo[node.attr('territoryId').downcase.to_sym] = Regexp.new(node.text); memo
        end

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

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

    end

  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
twitter_cldr-3.0.1 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-2.4.3 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.0.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.0.0.beta1 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-2.4.2 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-2.4.1 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-2.4.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-2.2.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-2.1.1 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-2.1.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-2.0.2 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-2.0.1 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-2.0.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-1.9.1 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-1.9.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-1.8.1 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-1.8.0 lib/twitter_cldr/resources/postal_codes_importer.rb