jp_zip_code 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e290002e137bf4cd02f53d7d2cda45d86924b9c5
4
- data.tar.gz: bd03bbebc89e81b7ce2b5a59efe2b7aa8cee414e
3
+ metadata.gz: 3524bca9cdb88daacd1f053247ee11406cd2f17b
4
+ data.tar.gz: 443ff9193fb193f70ea0ad3bde73964b0fe06c57
5
5
  SHA512:
6
- metadata.gz: 784451997f12f43e7074d3dbdadf6402a2add0f5b888b93072ceb6efb113e31b46760c5bff1035b64a4c0665579de012b292865a515864ba68a7ecb18e8687b2
7
- data.tar.gz: bfbfd1b7a7984a508e3cae9c3176b0a2c58b1a12c889fa6007ac628178e4d77a6a9e696f20da33ec63641e74bafe940f45c79c914eb33a702cc2040db9e75b3f
6
+ metadata.gz: 3aeebe5cbf4017997fd2c0f2f9373419ce4516288bc8998d57972c273a35937c8ed4f0ad05d867323d5ed3a7bb23d71c3e51607e1f26942a789151f140b2a39e
7
+ data.tar.gz: f5eb9d87aecf55f4b14d3088c68fdd7722c7bfe4fb31fcee98861c7e2b6f7ca3b3e683170ea61a229c98161ddca5f7eb532d9ac4e18ed8cfc84e163adbeb4a13
@@ -0,0 +1,91 @@
1
+ require 'open-uri'
2
+ require 'zip'
3
+ require 'yaml'
4
+
5
+ module JpZipCode
6
+ class Filer
7
+ def initialize(type)
8
+ @type = type.to_sym
9
+ end
10
+
11
+ def download_url
12
+ case @type
13
+ when :jp
14
+ 'http://www.post.japanpost.jp/zipcode/dl/kogaki/zip/ken_all.zip'
15
+ when :roman
16
+ 'http://www.post.japanpost.jp/zipcode/dl/roman/ken_all_rome.zip'
17
+ end
18
+ end
19
+
20
+ def fetch_data
21
+ download_file
22
+ unzip_file_and_get_data
23
+ end
24
+
25
+ def zip_file_name
26
+ File.basename(download_url)
27
+ end
28
+
29
+ def download_file
30
+ open(zip_file_name, 'wb') do |file|
31
+ open(download_url) do |data|
32
+ file.write(data.read)
33
+ end
34
+ end
35
+ end
36
+
37
+ def unzip_file_and_get_data
38
+ hash = {}
39
+ unzip(zip_file_name) do |file|
40
+ get_row(file).each do |row|
41
+ row_data = to_hash(row.split(',').map(&:strip))
42
+ hash[row_data[:zip_code]] = row_data
43
+ end
44
+ end
45
+ hash
46
+ end
47
+
48
+ def unzip(file_name)
49
+ Zip::File.open(file_name) do |csv_files|
50
+ csv_files.each do |file|
51
+ yield(file)
52
+ end
53
+ end
54
+ end
55
+
56
+ def get_row(file)
57
+ file.get_input_stream.read.encode('utf-8', 'cp932').delete('"').split("\n")
58
+ end
59
+
60
+ def to_hash(datas)
61
+ case @type
62
+ when :jp
63
+ {
64
+ code: datas[0],
65
+ old_zip_code: datas[1],
66
+ zip_code: datas[2],
67
+ pref_kana: datas[3],
68
+ city_kana: datas[4],
69
+ town_kana: datas[5],
70
+ pref_kanji: datas[6],
71
+ city_kanji: datas[7],
72
+ town_kanji: datas[8]
73
+ }
74
+ when :roman
75
+ {
76
+ zip_code: datas[0],
77
+ pref_kanji: datas[1],
78
+ city_kanji: datas[2],
79
+ town_kanji: datas[3],
80
+ pref_roman: datas[4],
81
+ city_roman: datas[5],
82
+ town_roman: datas[6]
83
+ }
84
+ end
85
+ end
86
+
87
+ def clean
88
+ File.delete(zip_file_name) if File.exist?(zip_file_name)
89
+ end
90
+ end
91
+ end
@@ -2,8 +2,8 @@ module JpZipCode
2
2
  class Updater
3
3
  def initialize(dry_run = true)
4
4
  @dry_run = dry_run
5
- @jp_filer = JpZipCode::Filer::Jp.new
6
- @roman_filer = JpZipCode::Filer::Roman.new
5
+ @jp_filer = JpZipCode::Filer.new(:jp)
6
+ @roman_filer = JpZipCode::Filer.new(:roman)
7
7
  end
8
8
 
9
9
  def execute
@@ -1,3 +1,3 @@
1
1
  module JpZipCode
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
data/lib/jp_zip_code.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  require 'jp_zip_code/version'
2
2
  require 'jp_zip_code/updater'
3
- require 'jp_zip_code/filer/base'
4
- require 'jp_zip_code/filer/jp'
5
- require 'jp_zip_code/filer/roman'
3
+ require 'jp_zip_code/filer'
6
4
  require 'json'
7
5
  require 'rash'
8
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jp_zip_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - kimromi
@@ -1726,9 +1726,7 @@ files:
1726
1726
  - data/zip_code/9998.json
1727
1727
  - jp_zip_code.gemspec
1728
1728
  - lib/jp_zip_code.rb
1729
- - lib/jp_zip_code/filer/base.rb
1730
- - lib/jp_zip_code/filer/jp.rb
1731
- - lib/jp_zip_code/filer/roman.rb
1729
+ - lib/jp_zip_code/filer.rb
1732
1730
  - lib/jp_zip_code/updater.rb
1733
1731
  - lib/jp_zip_code/version.rb
1734
1732
  homepage: http://kimromi.hatenablog.jp/
@@ -1,55 +0,0 @@
1
- require 'open-uri'
2
- require 'zip'
3
- require 'yaml'
4
-
5
- module JpZipCode
6
- module Filer
7
- class Base
8
- attr_accessor :download_url
9
-
10
- def fetch_data
11
- download_file
12
- unzip_file_and_get_data
13
- end
14
-
15
- def zip_file_name
16
- File.basename(download_url)
17
- end
18
-
19
- def download_file
20
- open(zip_file_name, 'wb') do |file|
21
- open(download_url) do |data|
22
- file.write(data.read)
23
- end
24
- end
25
- end
26
-
27
- def unzip_file_and_get_data
28
- hash = {}
29
- unzip(zip_file_name) do |file|
30
- get_row(file).each do |row|
31
- row_data = to_hash(row)
32
- hash[row_data[:zip_code]] = row_data
33
- end
34
- end
35
- hash
36
- end
37
-
38
- def unzip(file_name)
39
- Zip::File.open(file_name) do |csv_files|
40
- csv_files.each do |file|
41
- yield(file)
42
- end
43
- end
44
- end
45
-
46
- def get_row(file)
47
- file.get_input_stream.read.encode('utf-8', 'cp932').delete('"').split("\n")
48
- end
49
-
50
- def clean
51
- File.delete(zip_file_name) if File.exist?(zip_file_name)
52
- end
53
- end
54
- end
55
- end
@@ -1,29 +0,0 @@
1
- require 'open-uri'
2
- require 'zip'
3
- require 'yaml'
4
-
5
- module JpZipCode
6
- module Filer
7
- class Jp < Base
8
- def initialize
9
- self.download_url = 'http://www.post.japanpost.jp/zipcode/dl/kogaki/zip/ken_all.zip'
10
- end
11
-
12
- def to_hash(row)
13
- datas = row.split(',').map(&:strip)
14
- {
15
- code: datas[0],
16
- old_zip_code: datas[1],
17
- zip_code: datas[2],
18
- pref_kana: datas[3],
19
- city_kana: datas[4],
20
- town_kana: datas[5],
21
- pref_kanji: datas[6],
22
- city_kanji: datas[7],
23
- town_kanji: datas[8]
24
- }
25
- end
26
-
27
- end
28
- end
29
- end
@@ -1,26 +0,0 @@
1
- require 'open-uri'
2
- require 'zip'
3
- require 'yaml'
4
-
5
- module JpZipCode
6
- module Filer
7
- class Roman < Base
8
- def initialize
9
- self.download_url = 'http://www.post.japanpost.jp/zipcode/dl/roman/ken_all_rome.zip'
10
- end
11
-
12
- def to_hash(row)
13
- datas = row.split(',').map(&:strip)
14
- {
15
- zip_code: datas[0],
16
- pref_kanji: datas[1],
17
- city_kanji: datas[2],
18
- town_kanji: datas[3],
19
- pref_roman: datas[4],
20
- city_roman: datas[5],
21
- town_roman: datas[6]
22
- }
23
- end
24
- end
25
- end
26
- end