geo_master_jp 0.1.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +52 -0
  4. data/Rakefile +27 -0
  5. data/lib/generators/geo_master_jp/data/company20180424.csv.zip +0 -0
  6. data/lib/generators/geo_master_jp/data/join20190405.csv.zip +0 -0
  7. data/lib/generators/geo_master_jp/data/line20190405free.csv.zip +0 -0
  8. data/lib/generators/geo_master_jp/data/prefectures.yml +329 -0
  9. data/lib/generators/geo_master_jp/data/station20190405free.csv.zip +0 -0
  10. data/lib/generators/geo_master_jp/install_area_data_generator.rb +128 -0
  11. data/lib/generators/geo_master_jp/install_generator.rb +18 -0
  12. data/lib/generators/geo_master_jp/install_migration_generator.rb +51 -0
  13. data/lib/generators/geo_master_jp/install_railway_data_generator.rb +201 -0
  14. data/lib/generators/geo_master_jp/templates/create_cities.rb.erb +16 -0
  15. data/lib/generators/geo_master_jp/templates/create_lines.rb.erb +23 -0
  16. data/lib/generators/geo_master_jp/templates/create_prefectures.rb.erb +13 -0
  17. data/lib/generators/geo_master_jp/templates/create_railway_companies.rb.erb +18 -0
  18. data/lib/generators/geo_master_jp/templates/create_station_connections.rb.erb +14 -0
  19. data/lib/generators/geo_master_jp/templates/create_stations.rb.erb +27 -0
  20. data/lib/generators/geo_master_jp/templates/create_towns.rb.erb +19 -0
  21. data/lib/generators/geo_master_jp/templates/create_versions.rb.erb +10 -0
  22. data/lib/generators/geo_master_jp/templates/geo_master_jp.rb +15 -0
  23. data/lib/geo_master_jp/association_helper.rb +13 -0
  24. data/lib/geo_master_jp/config.rb +15 -0
  25. data/lib/geo_master_jp/engine.rb +27 -0
  26. data/lib/geo_master_jp/models/city.rb +21 -0
  27. data/lib/geo_master_jp/models/line.rb +21 -0
  28. data/lib/geo_master_jp/models/prefecture.rb +21 -0
  29. data/lib/geo_master_jp/models/railway_company.rb +19 -0
  30. data/lib/geo_master_jp/models/station.rb +22 -0
  31. data/lib/geo_master_jp/models/station_connection.rb +17 -0
  32. data/lib/geo_master_jp/models/town.rb +21 -0
  33. data/lib/geo_master_jp/models/version.rb +16 -0
  34. data/lib/geo_master_jp/railtie.rb +4 -0
  35. data/lib/geo_master_jp/version.rb +3 -0
  36. data/lib/geo_master_jp.rb +9 -0
  37. data/lib/tasks/geo_master_jp_tasks.rake +4 -0
  38. metadata +149 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e37df4a089fc32f61b506648b3335984f8c1dce556e78d358e7e1c9a79ddb9aa
4
+ data.tar.gz: 19fb76758bc175698394db53048286a7a1b9eaa453072a4115892d50cf5af75b
5
+ SHA512:
6
+ metadata.gz: f2d121d737c1f492d54175baff56f2f07218f8f6fe8ba04ddd83215809bac5cbc7416e9580f9b4452a3527dc87d70f3ec23b9dffb7d6fd73aa1395e967fc4ac2
7
+ data.tar.gz: 254fd7458157d3b9ec67253f446d0c58d0edb459169f1ed35e3214c434206e8097cfe97438dc4591d62ad768a44c050f354ec1b71a9a959b0ebacc3d0bf2d2f0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 ykogure
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # WIP
2
+ This gem is WIP!
3
+
4
+ # GeoMasterJp
5
+ Short description and motivation.
6
+
7
+ ## Usage
8
+ How to use my plugin.
9
+
10
+ ## Installation
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'geo_master_jp'
15
+ ```
16
+
17
+ And then execute:
18
+ ```bash
19
+ $ bundle
20
+ ```
21
+
22
+ Or install it yourself as:
23
+ ```bash
24
+ $ gem install geo_master_jp
25
+ ```
26
+
27
+ ## Initialize
28
+ ### Create Config File
29
+ ```bash
30
+ rails g geo_master_jp:install
31
+ ```
32
+
33
+ ### Create Migration Files
34
+ ```bash
35
+ rails g geo_master_jp:install_migration
36
+ ```
37
+
38
+ ### Import Area Data
39
+ ```bash
40
+ rails g geo_master_jp:install_area_data
41
+ ```
42
+
43
+ ### Import Railway Data
44
+ ```bash
45
+ rails g geo_master_jp:install_railway_data
46
+ ```
47
+
48
+ ## Contributing
49
+ Contribution directions go here.
50
+
51
+ ## License
52
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'GeoMasterJp'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,329 @@
1
+ -
2
+ id: 1
3
+ code: '01'
4
+ name: 北海道
5
+ name_kana: ホッカイドウ
6
+ name_alphabet: hokkaido
7
+ short_name: 北海道
8
+ -
9
+ id: 2
10
+ code: '02'
11
+ name: 青森県
12
+ name_kana: アオモリケン
13
+ name_alphabet: aomori
14
+ short_name: 青森
15
+ -
16
+ id: 3
17
+ code: '03'
18
+ name: 岩手県
19
+ name_kana: イワテケン
20
+ name_alphabet: iwate
21
+ short_name: 岩手
22
+ -
23
+ id: 4
24
+ code: '04'
25
+ name: 宮城県
26
+ name_kana: ミヤギケン
27
+ name_alphabet: miyagi
28
+ short_name: 宮城
29
+ -
30
+ id: 5
31
+ code: '05'
32
+ name: 秋田県
33
+ name_kana: アキタケン
34
+ name_alphabet: akita
35
+ short_name: 秋田
36
+ -
37
+ id: 6
38
+ code: '06'
39
+ name: 山形県
40
+ name_kana: ヤマガタケン
41
+ name_alphabet: yamagata
42
+ short_name: 山形
43
+ -
44
+ id: 7
45
+ code: '07'
46
+ name: 福島県
47
+ name_kana: フクシマケン
48
+ name_alphabet: fukushima
49
+ short_name: 福島
50
+ -
51
+ id: 8
52
+ code: '08'
53
+ name: 茨城県
54
+ name_kana: イバラキケン
55
+ name_alphabet: ibaraki
56
+ short_name: 茨城
57
+ -
58
+ id: 9
59
+ code: '09'
60
+ name: 栃木県
61
+ name_kana: トチギケン
62
+ name_alphabet: tochigi
63
+ short_name: 栃木
64
+ -
65
+ id: 10
66
+ code: '10'
67
+ name: 群馬県
68
+ name_kana: グンマケン
69
+ name_alphabet: gumma
70
+ short_name: 群馬
71
+ -
72
+ id: 11
73
+ code: '11'
74
+ name: 埼玉県
75
+ name_kana: サイタマケン
76
+ name_alphabet: saitama
77
+ short_name: 埼玉
78
+ -
79
+ id: 12
80
+ code: '12'
81
+ name: 千葉県
82
+ name_kana: チバケン
83
+ name_alphabet: chiba
84
+ short_name: 千葉
85
+ -
86
+ id: 13
87
+ code: '13'
88
+ name: 東京都
89
+ name_kana: トウキョウト
90
+ name_alphabet: tokyo
91
+ short_name: 東京
92
+ -
93
+ id: 14
94
+ code: '14'
95
+ name: 神奈川県
96
+ name_kana: カナガワケン
97
+ name_alphabet: kanagawa
98
+ short_name: 神奈川
99
+ -
100
+ id: 15
101
+ code: '15'
102
+ name: 新潟県
103
+ name_kana: ニイガタケン
104
+ name_alphabet: niigata
105
+ short_name: 新潟
106
+ -
107
+ id: 16
108
+ code: '16'
109
+ name: 富山県
110
+ name_kana: トヤマケン
111
+ name_alphabet: toyama
112
+ short_name: 富山
113
+ -
114
+ id: 17
115
+ code: '17'
116
+ name: 石川県
117
+ name_kana: イシカワケン
118
+ name_alphabet: ishikawa
119
+ short_name: 石川
120
+ -
121
+ id: 18
122
+ code: '18'
123
+ name: 福井県
124
+ name_kana: フクイケン
125
+ name_alphabet: fukui
126
+ short_name: 福井
127
+ -
128
+ id: 19
129
+ code: '19'
130
+ name: 山梨県
131
+ name_kana: ヤマナシケン
132
+ name_alphabet: yamanashi
133
+ short_name: 山梨
134
+ -
135
+ id: 20
136
+ code: '20'
137
+ name: 長野県
138
+ name_kana: ナガノケン
139
+ name_alphabet: nagano
140
+ short_name: 長野
141
+ -
142
+ id: 21
143
+ code: '21'
144
+ name: 岐阜県
145
+ name_kana: ギフケン
146
+ name_alphabet: gifu
147
+ short_name: 岐阜
148
+ -
149
+ id: 22
150
+ code: '22'
151
+ name: 静岡県
152
+ name_kana: シズオカケン
153
+ name_alphabet: shizuoka
154
+ short_name: 静岡
155
+ -
156
+ id: 23
157
+ code: '23'
158
+ name: 愛知県
159
+ name_kana: アイチケン
160
+ name_alphabet: aichi
161
+ short_name: 愛知
162
+ -
163
+ id: 24
164
+ code: '24'
165
+ name: 三重県
166
+ name_kana: ミエケン
167
+ name_alphabet: mie
168
+ short_name: 三重
169
+ -
170
+ id: 25
171
+ code: '25'
172
+ name: 滋賀県
173
+ name_kana: シガケン
174
+ name_alphabet: shiga
175
+ short_name: 滋賀
176
+ -
177
+ id: 26
178
+ code: '26'
179
+ name: 京都府
180
+ name_kana: キョウトフ
181
+ name_alphabet: kyoto
182
+ short_name: 京都
183
+ -
184
+ id: 27
185
+ code: '27'
186
+ name: 大阪府
187
+ name_kana: オオサカフ
188
+ name_alphabet: osaka
189
+ short_name: 大阪
190
+ -
191
+ id: 28
192
+ code: '28'
193
+ name: 兵庫県
194
+ name_kana: ヒョウゴケン
195
+ name_alphabet: hyogo
196
+ short_name: 兵庫
197
+ -
198
+ id: 29
199
+ code: '29'
200
+ name: 奈良県
201
+ name_kana: ナラケン
202
+ name_alphabet: nara
203
+ short_name: 奈良
204
+ -
205
+ id: 30
206
+ code: '30'
207
+ name: 和歌山県
208
+ name_kana: ワカヤマケン
209
+ name_alphabet: wakayama
210
+ short_name: 和歌山
211
+ -
212
+ id: 31
213
+ code: '31'
214
+ name: 鳥取県
215
+ name_kana: トットリケン
216
+ name_alphabet: tottori
217
+ short_name: 鳥取
218
+ -
219
+ id: 32
220
+ code: '32'
221
+ name: 島根県
222
+ name_kana: シマネケン
223
+ name_alphabet: shimane
224
+ short_name: 島根
225
+ -
226
+ id: 33
227
+ code: '33'
228
+ name: 岡山県
229
+ name_kana: オカヤマケン
230
+ name_alphabet: okayama
231
+ short_name: 岡山
232
+ -
233
+ id: 34
234
+ code: '34'
235
+ name: 広島県
236
+ name_kana: ヒロシマケン
237
+ name_alphabet: hiroshima
238
+ short_name: 広島
239
+ -
240
+ id: 35
241
+ code: '35'
242
+ name: 山口県
243
+ name_kana: ヤマグチケン
244
+ name_alphabet: yamaguchi
245
+ short_name: 山口
246
+ -
247
+ id: 36
248
+ code: '36'
249
+ name: 徳島県
250
+ name_kana: トクシマケン
251
+ name_alphabet: tokushima
252
+ short_name: 徳島
253
+ -
254
+ id: 37
255
+ code: '37'
256
+ name: 香川県
257
+ name_kana: カガワケン
258
+ name_alphabet: kagawa
259
+ short_name: 香川
260
+ -
261
+ id: 38
262
+ code: '38'
263
+ name: 愛媛県
264
+ name_kana: エヒメケン
265
+ name_alphabet: ehime
266
+ short_name: 愛媛
267
+ -
268
+ id: 39
269
+ code: '39'
270
+ name: 高知県
271
+ name_kana: コウチケン
272
+ name_alphabet: kochi
273
+ short_name: 高知
274
+ -
275
+ id: 40
276
+ code: '40'
277
+ name: 福岡県
278
+ name_kana: フクオカケン
279
+ name_alphabet: fukuoka
280
+ short_name: 福岡
281
+ -
282
+ id: 41
283
+ code: '41'
284
+ name: 佐賀県
285
+ name_kana: サガケン
286
+ name_alphabet: saga
287
+ short_name: 佐賀
288
+ -
289
+ id: 42
290
+ code: '42'
291
+ name: 長崎県
292
+ name_kana: ナガサキケン
293
+ name_alphabet: nagasaki
294
+ short_name: 長崎
295
+ -
296
+ id: 43
297
+ code: '43'
298
+ name: 熊本県
299
+ name_kana: クマモトケン
300
+ name_alphabet: kumamoto
301
+ short_name: 熊本
302
+ -
303
+ id: 44
304
+ code: '44'
305
+ name: 大分県
306
+ name_kana: オオイタケン
307
+ name_alphabet: oita
308
+ short_name: 大分
309
+ -
310
+ id: 45
311
+ code: '45'
312
+ name: 宮崎県
313
+ name_kana: ミヤザキケン
314
+ name_alphabet: miyazaki
315
+ short_name: 宮崎
316
+ -
317
+ id: 46
318
+ code: '46'
319
+ name: 鹿児島県
320
+ name_kana: カゴシマケン
321
+ name_alphabet: kagoshima
322
+ short_name: 鹿児島
323
+ -
324
+ id: 47
325
+ code: '47'
326
+ name: 沖縄県
327
+ name_kana: オキナワケン
328
+ name_alphabet: okinawa
329
+ short_name: 沖縄
@@ -0,0 +1,128 @@
1
+ require 'zip'
2
+ require 'open-uri'
3
+ require 'yaml'
4
+ require 'romaji'
5
+ require 'nkf'
6
+
7
+ module GeoMasterJp
8
+ module Generators
9
+ class InstallAreaDataGenerator < Rails::Generators::Base
10
+ VERSION = '20201201'
11
+
12
+ source_root File.expand_path('templates', __dir__)
13
+
14
+ desc "Download and insert geo data to your application databases."
15
+
16
+ def set_prefectures
17
+ if GeoMasterJp::Prefecture.count > 0
18
+ puts "Prefecture is already exists and skip."
19
+ return
20
+ end
21
+ YAML.load_file("#{__dir__}/data/prefectures.yml").each do |prefecture|
22
+ GeoMasterJp::Prefecture.create(prefecture)
23
+ end
24
+
25
+ puts "Set Prefecture is complete."
26
+ end
27
+
28
+ def set_cities_and_towns
29
+ if (GeoMasterJp::Version.last_version(GeoMasterJp::City).present? && GeoMasterJp::Version.last_version(GeoMasterJp::City) >= Date.parse(VERSION)) && (GeoMasterJp::Version.last_version(GeoMasterJp::Town).present? && GeoMasterJp::Version.last_version(GeoMasterJp::Town) >= Date.parse(VERSION))
30
+ puts "City and Town is already latest version and skip."
31
+ return
32
+ end
33
+
34
+ csv_data = download_city_and_town_data_and_convert_to_csv
35
+ csv_data.group_by{|row| row[0][0..1]}.each_with_index do |(prefecture_code, rows), idx|
36
+ print "Setting City and Town ...#{idx+1}/#{47}\r"
37
+
38
+ prefecture = GeoMasterJp::Prefecture.find_by(code: prefecture_code)
39
+
40
+ towns = {}
41
+ rows.group_by{|row| row[0][0..4]}.each do |city_code, town_rows|
42
+ next if town_rows[0][10].blank?
43
+
44
+ if city = GeoMasterJp::City.find_by(code: city_code)
45
+ if town_rows[0][26] == '2' # 変更
46
+ city = set_city(city, town_rows[0])
47
+
48
+ city.save
49
+ end
50
+ else # 新規
51
+ city = prefecture.cities.build(code: city_code)
52
+ city = set_city(city, town_rows[0])
53
+
54
+ city.save
55
+ end
56
+
57
+ towns[city.code] = []
58
+ town_rows.each do |town_row|
59
+ town_code = town_row[0]
60
+
61
+ if town = GeoMasterJp::Town.find_by(code: town_code)
62
+ if town_row[26] == '2' # 変更
63
+ town = set_town(town, row)
64
+ town.city = city
65
+
66
+ town.save
67
+ elsif town_row[26] == '3' # 廃止
68
+ town.update!(deleted_at: Time.current)
69
+ end
70
+ else # 新規
71
+ town = city.towns.build(geo_master_jp_prefecture_code: prefecture.code, code: town_code)
72
+ town = set_town(town, town_row)
73
+ town.city = city
74
+
75
+ towns[city.code] << town
76
+ end
77
+ end
78
+ end
79
+
80
+ towns.each do |_city_code, ts|
81
+ GeoMasterJp::Town.import ts
82
+ end
83
+ end
84
+
85
+ GeoMasterJp::Version.create!(model_name_string: GeoMasterJp::City.to_s, version: Date.parse(VERSION))
86
+ GeoMasterJp::Version.create!(model_name_string: GeoMasterJp::Town.to_s, version: Date.parse(VERSION))
87
+
88
+ print "\n"
89
+ puts "Set City and Town is complete."
90
+ end
91
+
92
+ private
93
+
94
+ def download_city_and_town_data_and_convert_to_csv
95
+ url = "https://www.japan-database.jp/database/#{VERSION[0..3]}/addressfc#{VERSION[0..5]}.zip"
96
+
97
+ csv_data = nil
98
+ OpenURI.open_uri(url) do |file|
99
+ Zip::File.open_buffer(file.read) do |zip|
100
+ entry = zip.glob("addressf#{VERSION[0..5]}.csv").first
101
+ csv_data = entry.get_input_stream.read
102
+ end
103
+ end
104
+
105
+ csv_data.encode("UTF-8", "CP932").split(/\r\n/).map{|row| row.split(',')}.map{|row| row.map{|val| val.gsub(/(^[[:space:]]+)|([[:space:]]+$)/, '')}}
106
+ end
107
+
108
+ def set_city(city, row)
109
+ city.name = row[10]
110
+ city.name_kana = row[6].present? ? NKF.nkf("-Xw", row[6]) : ''
111
+ city.name_alphabet = Romaji.kana2romaji(city.name_kana)
112
+
113
+ city
114
+ end
115
+
116
+ def set_town(town, row)
117
+ town.name = row[11] + row[12]
118
+ town.name_kana = (row[7] + row[8]).present? ? NKF.nkf("-Xw", row[7] + row[8]) : ''
119
+ town.name_alphabet = Romaji.kana2romaji(town.name_kana)
120
+ town.zip_code = row[2]
121
+ town.deleted_at = Time.current if row[26] == '3' # 廃止
122
+
123
+ town
124
+ end
125
+
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,18 @@
1
+ require "rails/generators"
2
+ require "rails/generators/active_record"
3
+
4
+ module GeoMasterJp
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include ::Rails::Generators::Migration
8
+
9
+ source_root File.expand_path('templates', __dir__)
10
+
11
+ desc "Creates a Geo Master Jp initializer to your application."
12
+
13
+ def copy_initializer
14
+ template "geo_master_jp.rb", "config/initializers/geo_master_jp.rb"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,51 @@
1
+ require "rails/generators"
2
+ require "rails/generators/active_record"
3
+
4
+ module GeoMasterJp
5
+ module Generators
6
+ class InstallMigrationGenerator < Rails::Generators::Base
7
+ include ::Rails::Generators::Migration
8
+
9
+ source_root File.expand_path('templates', __dir__)
10
+
11
+ desc "Creates a Geo Master Jp Migaration Files to your application."
12
+
13
+ def self.next_migration_number(dirname)
14
+ ::ActiveRecord::Generators::Base.next_migration_number(dirname)
15
+ end
16
+
17
+ def create_migration_file
18
+ templates = ['create_versions']
19
+ if GeoMasterJp.config.use_models.include?(:area)
20
+ templates += ['create_prefectures', 'create_cities', 'create_towns', 'create_versions']
21
+ end
22
+ if GeoMasterJp.config.use_models.include?(:railway)
23
+ templates += ['create_prefectures', 'create_cities', 'create_towns', 'create_railway_companies', 'create_lines', 'create_stations', 'create_station_connections']
24
+ end
25
+
26
+ migration_dir = File.expand_path("db/migrate")
27
+ templates.each do |template|
28
+ if self.class.migration_exists?(migration_dir, template)
29
+ ::Kernel.warn "Migration already exists: #{template}"
30
+ else
31
+ migration_template(
32
+ "#{template}.rb.erb",
33
+ "db/migrate/#{template}.rb",
34
+ migration_version: migration_version
35
+ )
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def migration_version
43
+ major = ActiveRecord::VERSION::MAJOR
44
+ if major >= 5
45
+ "[#{major}.#{ActiveRecord::VERSION::MINOR}]"
46
+ end
47
+ end
48
+
49
+ end
50
+ end
51
+ end