Module: Yubinbang::RawData

Defined in:
lib/yubinbang/raw_data.rb

Class Method Summary collapse

Class Method Details

.decode(source, target) ⇒ Object


22
23
24
# File 'lib/yubinbang/raw_data.rb', line 22

def self.decode(source, target)
  s = `cd #{target}; lha x #{source}`
end

.fetch(path) ⇒ Object


16
17
18
19
20
# File 'lib/yubinbang/raw_data.rb', line 16

def self.fetch(path)
  open("http://www.post.japanpost.jp/zipcode/dl/kogaki/lzh/ken_all.lzh") do |f|
    File.open(path, "w") {|o| o.write(f.read)}
  end
end

.parse(source) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/yubinbang/raw_data.rb', line 26

def self.parse(source)
  iconv = Iconv.new("UTF-8", "Shift_JIS")
  s = iconv.iconv(File.read(source))
  a = []
  FasterCSV.parse(s) do |row|
    a << {
      :postal_code => row[2],
      :prefecture => row[6],
      :municipality => row[7],
    }
  end
  a
end

.parse_from_source(tmp_path) ⇒ Object

Fetch data from Japan Post site and parse it to an array of hashes


8
9
10
11
12
13
14
# File 'lib/yubinbang/raw_data.rb', line 8

def self.parse_from_source(tmp_path)
  raw = File.join(tmp_path, "ken_all.lzh")
  csv = File.join(tmp_path, "ken_all.csv")
  fetch(raw)
  decode(raw, tmp_path)
  parse(csv)
end