Sha256: 9130d9a0d84ad0b0ac96a4704b2ec02b314a6ed001c2f5b470304e9b74fc1670

Contents?: true

Size: 1.59 KB

Versions: 23

Compression:

Stored size: 1.59 KB

Contents

# encoding: UTF-8

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

require 'json'
require 'open-uri'
require 'set'
require 'yaml'

module TwitterCldr
  module Resources

    class PostalCodesImporter < Importer

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

      output_path 'shared'
      ruby_engine :mri

      private

      def execute
        data = YAML.dump(fetch_data)
        File.write(File.join(output_path, 'postal_codes.yml'), data)
        puts
      end

      def output_path
        params.fetch(:output_path)
      end

      def fetch_data
        territories = Set.new

        each_territory.each_with_object({}) do |territory, ret|
          if regex = get_regex_for(territory)
            ret[territory] = {
              regex: Regexp.compile(regex),
              ast: TwitterCldr::Utils::RegexpAst.dump(
                RegexpAstGenerator.generate(regex)
              )
            }
          end

          territories.add(territory)
          STDOUT.write("\rImported postal codes for #{territory}, #{territories.size} of #{territory_count} total")
        end
      end

      def get_regex_for(territory)
        result = URI.open("#{BASE_URL}#{territory.to_s.upcase}").read
        data = JSON.parse(result)
        data['zip']
      end

      def territory_count
        TwitterCldr::Shared::Territories.all.size
      end

      def each_territory
        return to_enum(__method__) unless block_given?

        TwitterCldr::Shared::Territories.all.each_pair do |territory, _|
          yield territory
        end
      end

    end

  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
twitter_cldr-6.11.5 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.11.4 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.11.3 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.11.2 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.11.1 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.11.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.10.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.9.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.8.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.7.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.6.2 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.6.1 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.6.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.5.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.4.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.3.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.2.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.1.0 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.0.2 lib/twitter_cldr/resources/postal_codes_importer.rb
twitter_cldr-6.0.1 lib/twitter_cldr/resources/postal_codes_importer.rb