kamome 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kamome
4
+ class Operation
5
+ TYPE_HASH = :hash
6
+ TYPE_DETAIL = :detail
7
+ TYPE_DEFAULT = :default
8
+
9
+ GENRE_GENERAL = :general
10
+ GENRE_JIGYOSHO = :jigyosho
11
+
12
+ URL_GENERAL_ALL = 'https://www.post.japanpost.jp/zipcode/dl/kogaki/zip/ken_all.zip'
13
+ URL_GENERAL_ADD = 'https://www.post.japanpost.jp/zipcode/dl/kogaki/zip/add_%s.zip'
14
+ URL_GENERAL_DEL = 'https://www.post.japanpost.jp/zipcode/dl/kogaki/zip/del_%s.zip'
15
+ URL_JIGYOSHO_ALL = 'https://www.post.japanpost.jp/zipcode/dl/jigyosyo/zip/jigyosyo.zip'
16
+ URL_JIGYOSHO_ADD = 'https://www.post.japanpost.jp/zipcode/dl/jigyosyo/zip/jadd%s.zip'
17
+ URL_JIGYOSHO_DEL = 'https://www.post.japanpost.jp/zipcode/dl/jigyosyo/zip/jdel%s.zip'
18
+
19
+ class << self
20
+ def general_all(type: TYPE_DEFAULT)
21
+ new(type: type, genre: GENRE_GENERAL, urls: [URL_GENERAL_ALL])
22
+ end
23
+
24
+ def jigyosho_all(type: TYPE_DEFAULT)
25
+ new(type: type, genre: GENRE_JIGYOSHO, urls: [URL_JIGYOSHO_ALL])
26
+ end
27
+
28
+ def general_diff(date:, type: TYPE_DEFAULT)
29
+ yymm = format_yymm(date)
30
+ new(type: type, genre: GENRE_GENERAL, urls: [URL_GENERAL_ADD % yymm, URL_GENERAL_DEL % yymm])
31
+ end
32
+
33
+ def jigyosho_diff(date:, type: TYPE_DEFAULT)
34
+ yymm = format_yymm(date)
35
+ new(type: type, genre: GENRE_JIGYOSHO, urls: [URL_JIGYOSHO_ADD % yymm, URL_JIGYOSHO_DEL % yymm])
36
+ end
37
+
38
+ private
39
+
40
+ YYMM_FORMAT = /\d{2}(0[1-9]|1[0-2])/.freeze
41
+ def format_yymm(date)
42
+ yymm = date.respond_to?(:strftime) ? date.strftime('%y%m') : date.to_s
43
+ return yymm if yymm =~ YYMM_FORMAT
44
+
45
+ raise ::Kamome::ArgumentError, 'Wrong argument "date" (required "yymm" format)'
46
+ end
47
+ end
48
+
49
+ attr_reader :type
50
+ attr_reader :genre
51
+ attr_reader :urls
52
+
53
+ def initialize(type:, genre:, urls:)
54
+ @type = type
55
+ @genre = genre
56
+ @urls = urls
57
+ end
58
+
59
+ def type_hash?
60
+ type == TYPE_HASH
61
+ end
62
+
63
+ def type_detail?
64
+ type == TYPE_DETAIL
65
+ end
66
+
67
+ def type_default?
68
+ type == TYPE_DEFAULT
69
+ end
70
+
71
+ def genre_general?
72
+ genre == GENRE_GENERAL
73
+ end
74
+
75
+ def genre_jigyosho?
76
+ genre == GENRE_JIGYOSHO
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kamome
4
+ module Transformations
5
+ module Cleaner
6
+ module_function
7
+
8
+ TOWN_KAN_SUB_RULE = /(\((?!.カイ\)).+|イカニケイサイガナイバアイ|.+ノツギニバンチガクルバアイ|.+イチエン)$/.freeze
9
+ def clean_town_kana(value)
10
+ value.sub(TOWN_KAN_SUB_RULE, '')
11
+ end
12
+
13
+ TOWN_SUB_RULE = /(((?!.+階)).+)|以下に掲載がない場合|.+の次に番地がくる場合|.+一円)$/.freeze
14
+ def clean_town(value)
15
+ value.sub(TOWN_SUB_RULE, '')
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kamome
4
+ module Transformations
5
+ module Extractor
6
+ module_function
7
+
8
+ POST_OFFICE_REG = /((.+私書箱第.+号))$/.freeze
9
+ def extract_street(value)
10
+ value.sub(POST_OFFICE_REG, '')
11
+ end
12
+
13
+ def extract_post_office_box(value)
14
+ value[POST_OFFICE_REG, 1] || ''
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kamome/transformations/cleaner'
4
+ require 'kamome/models/general'
5
+ require 'kamome/models/address'
6
+
7
+ module Kamome
8
+ module Transformations
9
+ # Transform a GENERAL csv row to a Hash or others
10
+ class GeneralCsv
11
+ include Kamome::Transformations::Cleaner
12
+
13
+ # Generate hash from a general csv row
14
+ # @return [Hash]
15
+ def generate_hash(row)
16
+ validate_row!(row)
17
+ transform(row)
18
+ end
19
+
20
+ def generate_detail_model(row)
21
+ attributes = generate_hash(row)
22
+ ::Kamome::Models::General.new(attributes)
23
+ end
24
+
25
+ def generate_model(row)
26
+ attributes = extract(generate_hash(row))
27
+ ::Kamome::Models::Address.new(attributes)
28
+ end
29
+
30
+ private
31
+
32
+ VALID_ROW_SIZE = 15
33
+
34
+ def validate_row!(row)
35
+ return if row&.size == VALID_ROW_SIZE
36
+
37
+ raise ArgumentError, "Wrong row size (expected #{VALID_ROW_SIZE}, got #{row&.size})"
38
+ end
39
+
40
+ def transform(row) # rubocop:disable Metrics/AbcSize
41
+ {
42
+ code: row[0].to_s,
43
+ zipcode: row[2].to_s,
44
+ prefecture_kana: row[3].to_s,
45
+ city_kana: row[4].to_s,
46
+ town_kana: clean_town_kana(row[5].to_s),
47
+ prefecture: row[6].to_s,
48
+ city: row[7].to_s,
49
+ town: clean_town(row[8].to_s),
50
+ ambiguous_town: row[9].to_i == 1,
51
+ ambiguous_street: row[10].to_i == 1,
52
+ required_chome: row[11].to_i == 1,
53
+ ambiguous_zipcode: row[12].to_i == 1,
54
+ state: row[13].to_i,
55
+ reason: row[14].to_i
56
+ }
57
+ end
58
+
59
+ def extract(attributes)
60
+ {
61
+ code: attributes[:code],
62
+ zipcode: attributes[:zipcode],
63
+ prefecture: attributes[:prefecture],
64
+ city: attributes[:city],
65
+ town: attributes[:town],
66
+ street: '',
67
+ company_name: '',
68
+ post_office_box: '',
69
+ state: attributes[:state]
70
+ }
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kamome/transformations/extractor'
4
+ require 'kamome/models/jigyosho'
5
+ require 'kamome/models/address'
6
+
7
+ module Kamome
8
+ module Transformations
9
+ # Transform a JIGYOSHO csv row to a Hash or others
10
+ class JigyoshoCsv
11
+ include ::Kamome::Transformations::Extractor
12
+
13
+ # Generate a Hash from a JIGYOSHO csv row
14
+ # @return [Hash]
15
+ def generate_hash(row)
16
+ validate_row!(row)
17
+ transform(row)
18
+ end
19
+
20
+ # Generate an instance of Kamome::Models::Jigyosho from a JIGYOSHO csv row
21
+ # @return [Kamome::Models::Jigyosho]
22
+ def generate_detail_model(row)
23
+ attributes = generate_hash(row)
24
+ ::Kamome::Models::Jigyosho.new(attributes)
25
+ end
26
+
27
+ # Generate an instance of Kamome::Models::Address from a JIGYOSHO csv row
28
+ # @return [Kamome::Models::Address]
29
+ def generate_model(row)
30
+ attributes = extract(generate_hash(row))
31
+ ::Kamome::Models::Address.new(attributes)
32
+ end
33
+
34
+ private
35
+
36
+ VALID_ROW_SIZE = 13
37
+
38
+ def validate_row!(row)
39
+ return if row&.size == VALID_ROW_SIZE
40
+
41
+ raise ArgumentError, "Wrong row size (expected #{VALID_ROW_SIZE}, got #{row&.size})"
42
+ end
43
+
44
+ def transform(row) # rubocop:disable Metrics/AbcSize
45
+ {
46
+ code: row[0].to_s,
47
+ company_name_kana: row[1].to_s,
48
+ company_name: row[2].to_s,
49
+ prefecture: row[3].to_s,
50
+ city: row[4].to_s,
51
+ town: row[5].to_s,
52
+ street: extract_street(row[6].to_s),
53
+ post_office_box: extract_post_office_box(row[6].to_s),
54
+ zipcode: row[7].to_s,
55
+ japanpost_office_name: row[9].to_s,
56
+ has_post_office_box: row[10].to_i == 1,
57
+ multiple: row[11].to_i,
58
+ state: row[12].to_i
59
+ }
60
+ end
61
+
62
+ def extract(attributes)
63
+ {
64
+ code: attributes[:code],
65
+ zipcode: attributes[:zipcode],
66
+ prefecture: attributes[:prefecture],
67
+ city: attributes[:city],
68
+ town: attributes[:town],
69
+ street: attributes[:street],
70
+ company_name: attributes[:company_name],
71
+ post_office_box: attributes[:post_office_box],
72
+ state: convert_state(attributes[:state])
73
+ }
74
+ end
75
+
76
+ def convert_state(state)
77
+ state == 5 ? 2 : state
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kamome
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kamome
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kengos
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-11-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: vcr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.60'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.60'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.30'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.30'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubyzip
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.2'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.2'
125
+ description: the Japanese postcode loading interface
126
+ email:
127
+ - kengo@kengos.jp
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".travis.yml"
136
+ - ".yardopts"
137
+ - CHANGELOG.md
138
+ - Gemfile
139
+ - Gemfile.lock
140
+ - LICENSE
141
+ - Makefile
142
+ - README.md
143
+ - Rakefile
144
+ - bin/console
145
+ - bin/setup
146
+ - kamome.gemspec
147
+ - lib/kamome.rb
148
+ - lib/kamome/configuration.rb
149
+ - lib/kamome/csv_handler.rb
150
+ - lib/kamome/downloader.rb
151
+ - lib/kamome/loader.rb
152
+ - lib/kamome/models/address.rb
153
+ - lib/kamome/models/general.rb
154
+ - lib/kamome/models/jigyosho.rb
155
+ - lib/kamome/operation.rb
156
+ - lib/kamome/transformations/cleaner.rb
157
+ - lib/kamome/transformations/extractor.rb
158
+ - lib/kamome/transformations/general_csv.rb
159
+ - lib/kamome/transformations/jigyosho_csv.rb
160
+ - lib/kamome/version.rb
161
+ homepage: https://github.com/kengos/kamome
162
+ licenses:
163
+ - MIT
164
+ metadata:
165
+ homepage_uri: https://github.com/kengos/kamome
166
+ source_code_uri: https://github.com/kengos/kamome
167
+ changelog_uri: https://github.com/kengos/kamome/blob/master/CHANGELOG.md
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubyforge_project:
184
+ rubygems_version: 2.7.6
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: kamome-0.1.0
188
+ test_files: []