Sha256: b5e05f930903dad42c8095be681d5d90e31a39695665c9a5c6ceb3db79383bf3

Contents?: true

Size: 1.46 KB

Versions: 17

Compression:

Stored size: 1.46 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)]

        postal_codes = postal_codes.each_with_object({}) do |(territory, regex), memo|
          memo[territory] = {
            :regex => regex,
            :ast => TwitterCldr::Utils::RegexpAst.dump(
              RegexpAstGenerator.generate(regex.source)
            )
          }
        end

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

    end

  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
solidus_backend-1.0.0.pre3 vendor/bundle/gems/twitter_cldr-3.2.1/lib/twitter_cldr/resources/postal_codes_importer.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/twitter_cldr-3.2.1/lib/twitter_cldr/resources/postal_codes_importer.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/twitter_cldr-3.1.2/lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.2.1 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.2.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.1.2 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.1.1 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.1.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.0.10 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.0.9 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.0.8 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.0.7 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.0.6 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.0.5 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.0.4 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.0.3 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-3.0.2 lib/twitter_cldr/resources/postal_codes_importer.rb