Sha256: 2c911fa0ba9d7072705285d5d47065e962de221f97971cafee25a063db345ca8

Contents?: true

Size: 1.34 KB

Versions: 9

Compression:

Stored size: 1.34 KB

Contents

# encoding: UTF-8

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

require 'rest-client'
require 'json'
require 'yaml'

module TwitterCldr
  module Resources

    class PostalCodesImporter < Importer

      BASE_URL = 'http://i18napis.appspot.com/address/data/'

      output_path 'shared'
      ruby_engine :mri

      private

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

      def output_path
        params.fetch(:output_path)
      end

      def load
        each_territory.each_with_object({}) do |territory, ret|
          next unless regex = get_regex_for(territory)

          ret[territory] = {
            regex: Regexp.compile(regex),
            ast: TwitterCldr::Utils::RegexpAst.dump(
              RegexpAstGenerator.generate(regex)
            )
          }
        end
      end

      def get_regex_for(territory)
        result = RestClient.get("#{BASE_URL}#{territory.to_s.upcase}")
        data = JSON.parse(result.body)
        data['zip']
      end

      def each_territory
        if block_given?
          TwitterCldr::Shared::Territories.all.each_pair do |territory, _|
            yield territory
          end
        else
          to_enum(__method__)
        end
      end

    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
twitter_cldr-4.4.4 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-4.4.3 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-4.4.2 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-4.4.1 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-4.4.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-4.3.1 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-4.3.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-4.2.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-4.1.0 lib/twitter_cldr/resources/postal_codes_importer.rb