Sha256: af56f2f2ecd99bd0dda923aae1456ef887f92ab23f8190502ec28c6485450419

Contents?: true

Size: 1.6 KB

Versions: 11

Compression:

Stored size: 1.6 KB

Contents

namespace :postal_codes do
  desc 'Retrieve and update postal codes and their format'
  task :update do
    require 'json'
    require 'open-uri'
    require 'yaml'

    country_files = Dir['lib/countries/data/countries/*.yaml'].sort

    country_files.each do |country_file|
      yaml = YAML.load_file(country_file)
      country_code = File.basename(country_file, File.extname(country_file))
      print "\rUpdating #{country_code}"

      data = yaml[country_code].to_a
      postal_code_index = data.find_index { |d| d[0] == 'postal_code' }
      postal_code_format_index = data.find_index { |d| d[0] == 'postal_code_format' }

      response = URI.open("https://chromium-i18n.appspot.com/ssl-address/data/#{country_code}").read
      json = JSON.parse(response) rescue {}
      puts ' - Returned empty data. Skipping ' and next if json.empty?

      postal_code = ['postal_code', !!json['zip']]
      postal_code_format = ['postal_code_format', json['zip']]

      if postal_code_index
        data[postal_code_index] = postal_code
      else
        postal_code_index = data.find_index { |d| d[0] == 'nationality' } + 1 || data.size
        data.insert(postal_code_index, postal_code)
      end

      if json['zip']
        if postal_code_format_index
          data[postal_code_format_index] = postal_code_format
        else
          data.insert(postal_code_index + 1, postal_code_format)
        end
      elsif postal_code_format_index
        data.delete_at(postal_code_format_index)
      end

      yaml[country_code] = data.to_h

      File.open(country_file, 'w') { |file| file.write(yaml.to_yaml) }
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
countries-4.2.3 lib/countries/tasks/postal_codes.rake
countries-4.2.2 lib/countries/tasks/postal_codes.rake
countries-4.2.1 lib/countries/tasks/postal_codes.rake
countries-4.2.0 lib/countries/tasks/postal_codes.rake
countries-4.1.3 lib/countries/tasks/postal_codes.rake
countries-4.1.2 lib/countries/tasks/postal_codes.rake
countries-4.1.1 lib/countries/tasks/postal_codes.rake
countries-4.1.0 lib/countries/tasks/postal_codes.rake
countries-4.0.1 lib/countries/tasks/postal_codes.rake
countries-4.0.0 lib/countries/tasks/postal_codes.rake
countries-3.1.0 lib/countries/tasks/postal_codes.rake