Sha256: 9248cc6aff50e9b76d83c34b7883797c1b7d80df5548aa427a97ef9f62bfae68
Contents?: true
Size: 1.54 KB
Versions: 1
Compression:
Stored size: 1.54 KB
Contents
#!/usr/bin/env rake # coding: utf-8 require "bundler/gem_tasks" desc 'zip.yml を作成します' task :create_zips do require 'csv' require 'jp_prefecture' file_name = 'ken_all_utf8.csv' # download files `wget http://www.post.japanpost.jp/zipcode/dl/kogaki/lzh/ken_all.lzh` `lha x ken_all.lzh` `iconv -f sjis -t utf-8 ken_all.csv > #{file_name}` `rm ken_all.lzh` `rm ken_all.csv` zips = [] # read CSV CSV.foreach(file_name, headers: false) { |line| zips << [line[2], line[6]] } # create sorted list of zips -> prefecture_code zips = zips .collect { |zip, prefecture| [zip.to_i, JpPrefecture::Prefecture.find(name: prefecture).code] } .sort{ |x, y| x[0] <=> y[0] } # prepare calculation ranged_zips = [] current = [ zips.first[0], zips.first[0], zips.first[1] ] # calculate the zip ranges of each prefecture zips.each do |zip, code| if current[2] == code current[1] = zip else ranged_zips << current current = [zip, zip, code] end end # add last prefecture ranged_zips << current # create prefecture hash prefectures_to_zip = {} ranged_zips.each do |r0, r1, code| prefectures_to_zip[code] ||= [] prefectures_to_zip[code] << [r0, r1] end prefectures_to_zip = Hash[*prefectures_to_zip.sort.flatten(1)] # save result File.open('data/zip.yml', 'w') do |file| file.write "# { prefecture_code: [[from_zip_1, to_zip_1], [from_zip_2, to_zip_2], ...], ... }\n" file.write prefectures_to_zip.to_yaml end # delete temporary file `rm #{file_name}` end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
jp_prefecture-0.11.0 | Rakefile |