class TwitterCldr::Resources::PostalCodesImporter

Constants

BASE_URL

Private Instance Methods

each_territory() { |territory| ... } click to toggle source
# File lib/twitter_cldr/resources/postal_codes_importer.rb, line 61
def each_territory
  return to_enum(__method__) unless block_given?

  TwitterCldr::Shared::Territories.all.each_pair do |territory, _|
    yield territory
  end
end
execute() click to toggle source
# File lib/twitter_cldr/resources/postal_codes_importer.rb, line 23
def execute
  data = YAML.dump(fetch_data)
  File.write(File.join(output_path, 'postal_codes.yml'), data)
  puts
end
fetch_data() click to toggle source
# File lib/twitter_cldr/resources/postal_codes_importer.rb, line 33
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
get_regex_for(territory) click to toggle source
# File lib/twitter_cldr/resources/postal_codes_importer.rb, line 51
def get_regex_for(territory)
  result = URI.open("#{BASE_URL}#{territory.to_s.upcase}").read
  data = JSON.parse(result)
  data['zip']
end
output_path() click to toggle source
# File lib/twitter_cldr/resources/postal_codes_importer.rb, line 29
def output_path
  params.fetch(:output_path)
end
territory_count() click to toggle source
# File lib/twitter_cldr/resources/postal_codes_importer.rb, line 57
def territory_count
  TwitterCldr::Shared::Territories.all.size
end