kamome 0.1.0 → 0.8.0

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
  SHA256:
3
- metadata.gz: eabc3d7c8fab14f88254defa7d52769a9d674951c3af50f5332e1f5af678fa77
4
- data.tar.gz: ae51e66cd3b662dc93ea8cd31223281004db1b097acb07bb07c123d8e0a8ab00
3
+ metadata.gz: 580db2ebe5e52ce51d8014ad886247ef89ae3ab9f8bccb82397a3c3ce0a15e75
4
+ data.tar.gz: 0605ca8f9f680d15fb87b2396e4f8db0930a8d3984ee18eaa5b3b95d51ec9a8b
5
5
  SHA512:
6
- metadata.gz: f354384ca83990e9eddf6367c7d4453bff84729067a137aa1ebae59398f8d4dae94e1a21657e9830ba8289d8d671caec13314ec27235b0db935c95a1c90d2572
7
- data.tar.gz: 705d96a73c879e96c18fa7afc6e186913fab28308bf9e4f560d39d25f564712302e1af941382468786a1b42f2b10bda2f7f8043f9b824898efc17f12bec18424
6
+ metadata.gz: 6a2cd80afb342ff0ca2f68ff769a6f11b478dc06e5514d836142763102cff983a9760fa9bea9334d59fd76744a09c766a727cad3a5f79e52719a5c89330b9fa1
7
+ data.tar.gz: 2e17669f0e652e42ba98f8429ca034d5d770b0b7ba5bd5e0380402b05a7ad7d7d3883aa3c57738cbb638e376722eef40e656fc9f56d92f2e0ce98aa9318194e2
data/.rubocop.yml CHANGED
@@ -23,3 +23,6 @@ Metrics/LineLength:
23
23
 
24
24
  RSpec/NestedGroups:
25
25
  Max: 4
26
+
27
+ RSpec/MessageSpies:
28
+ EnforcedStyle: receive
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kamome (0.1.0)
4
+ kamome (0.8.0)
5
5
  rubyzip (~> 1.2)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -12,6 +12,15 @@ Kamome.import_general_all do |model, lineno|
12
12
  end
13
13
  ```
14
14
 
15
+ 対応CSV
16
+
17
+ * 全国一括 (全件, 差分)
18
+ * 事業所(全件, 差分)
19
+
20
+ 各CSVは共通のモデルにマッピングされます(type オプションで詳細なモデル or Hashに変更可能)
21
+
22
+ 詳細は [Kamome::Models::Address](https://github.com/kengos/kamome/blob/master/lib/kamome/models/address.rb) を参照してください
23
+
15
24
  ## Installation
16
25
 
17
26
  Add this line to your application's Gemfile:
@@ -43,7 +52,7 @@ end
43
52
  ```rb
44
53
  date = Time.local(2018, 11) # 2018/11 更新分の取り込み
45
54
  Kamome.import_general_diff(date: date) do |model, lineno|
46
- if model.changed?
55
+ if model.update?
47
56
  puts "追加データ: #{model.zipcode} => #{model.prefecture}#{model.city}#{model.town}"
48
57
  else
49
58
  puts "削除データ: #{model.zipcode} => #{model.prefecture}#{model.city}#{model.town}"
@@ -64,7 +73,7 @@ end
64
73
  ```rb
65
74
  date = Time.local(2018, 11) # 2018/11 更新分の取り込み
66
75
  Kamome.import_jigyosho_diff(date: date) do |model, lineno|
67
- if model.changed?
76
+ if model.update?
68
77
  puts "追加データ: #{model.zipcode} => #{model.prefecture}#{model.city}#{model.town}"
69
78
  else
70
79
  puts "削除データ: #{model.zipcode} => #{model.prefecture}#{model.city}#{model.town}"
@@ -104,6 +113,13 @@ Kamome.import_general_all(type: :detail or Kamome::Operation::TYPE_DETAIL)
104
113
  |XXXXX(YYYY階)|XXXXX(YYYY階)|元の値を維持しています|
105
114
  |XXXXX(YYYYY)|XXXXX|() の部分が除去されます|
106
115
 
116
+ ※ 以下のような削除データが発生する可能性があり、 `town` が正確に取得できないことがあります
117
+
118
+ ```
119
+ 26106,"600 ","6008093","キョウトフ","キョウトシシモギョウク","タケヤチョウ","京都府","京都市下京区","竹屋町(高倉通綾小路下る、高倉通仏光寺上る、堺町通綾小路下",0,0,0,0,2,6
120
+ 26106,"600 ","6008093","キョウトフ","キョウトシシモギョウク","タケヤチョウ","京都府","京都市下京区","る西入)",0,0,0,0,2,6
121
+ ```
122
+
107
123
  #### `Kamome::Models::General#town_kana`
108
124
 
109
125
  |元データ|変換後|備考|
@@ -126,8 +142,6 @@ Kamome.import_general_all(type: :detail or Kamome::Operation::TYPE_DETAIL)
126
142
 
127
143
  として設定されます
128
144
 
129
- #### `Kamome::Models::General#town_kana`
130
-
131
145
  ## Development
132
146
 
133
147
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `make` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -2,17 +2,30 @@
2
2
 
3
3
  module Kamome
4
4
  class Configuration
5
- require 'securerandom'
6
5
  require 'tmpdir'
7
6
 
7
+ # @!attribute [rw] tmp_path
8
+ # @return [String]
8
9
  attr_accessor :tmp_path
10
+
11
+ # @!attribute [rw] open_uri_options
12
+ # OpenURI options
13
+ # @see https://docs.ruby-lang.org/ja/latest/class/OpenURI.html
14
+ # @return [Hash]
9
15
  attr_accessor :open_uri_options
10
16
 
17
+ # @!attribute [rw] cleanup
18
+ # If you do not want to delete the downloaded csv, please specify `false`
19
+ # @return [Bolean]
20
+ attr_accessor :cleanup
21
+
11
22
  def initialize
12
- @tmp_path = ::File.join(Dir.tmpdir, 'kamome', SecureRandom.hex(8))
23
+ @tmp_path = ::File.join(Dir.tmpdir, 'kamome')
13
24
  @open_uri_options = {}
25
+ @cleanup = true
14
26
  end
15
27
 
28
+ # @return [Pathname]
16
29
  def working_directory
17
30
  @working_directory ||= ::Pathname.new(@tmp_path.to_s)
18
31
  end
@@ -13,7 +13,7 @@ module Kamome
13
13
  def call(csv_path)
14
14
  ::CSV.foreach(csv_path) do |row|
15
15
  obj = transform(row)
16
- yield(obj, $INPUT_LINE_NUMBER)
16
+ yield(obj, $INPUT_LINE_NUMBER) if block_given?
17
17
  end
18
18
  end
19
19
 
@@ -26,7 +26,7 @@ module Kamome
26
26
 
27
27
  # @return [Pathname] downloaded file
28
28
  def download(url)
29
- dest = @config.working_directory.join(SecureRandom.hex(8) + '.zip')
29
+ dest = @config.working_directory.join(download_filename)
30
30
  ::FileUtils.mkdir_p(dest.dirname)
31
31
 
32
32
  ::OpenURI.open_uri(url, @config.open_uri_options) do |response|
@@ -35,11 +35,13 @@ module Kamome
35
35
  dest
36
36
  end
37
37
 
38
- def unpack(src)
39
- # convert encoding Shift_JIS to UTF-8 (and half width Katakana to full width Katakana)
40
- content = ::NKF.nkf('-w -Lu', take_csv_entry(src).get_input_stream.read)
38
+ def download_filename
39
+ ::Time.now.strftime("%Y%m%d%H%M%S_#{SecureRandom.hex(3)}.zip")
40
+ end
41
41
 
42
+ def unpack(src)
42
43
  dest = src.sub('.zip', '.csv')
44
+ content = convert_encoding(take_csv_entry(src))
43
45
  ::File.binwrite(dest, content)
44
46
  dest
45
47
  end
@@ -48,9 +50,14 @@ module Kamome
48
50
  entry = ::Zip::File.open(src) do |zipfile|
49
51
  zipfile.select { |o| o.name =~ /.*\.csv/i }.first
50
52
  end
51
- raise ::Kamome::DownloadError, 'Not found csvfile in zipfile' if entry.nil?
53
+ raise 'Not found csvfile in zipfile' if entry.nil?
54
+
55
+ entry.get_input_stream.read
56
+ end
52
57
 
53
- entry
58
+ # convert encoding Shift_JIS to UTF-8 (and half width Katakana to full width Katakana)
59
+ def convert_encoding(content)
60
+ ::NKF.nkf('-w -Lu', content)
54
61
  end
55
62
  end
56
63
  end
data/lib/kamome/loader.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'kamome/downloader'
4
4
  require 'kamome/csv_handler'
5
+ require 'digest'
5
6
 
6
7
  module Kamome
7
8
  class Loader
@@ -9,13 +10,11 @@ module Kamome
9
10
  @config = config
10
11
  end
11
12
 
13
+ # @return [Array] csv digest
12
14
  def call(operation:, &block)
13
- csv_handler = build_csv_handler(operation)
14
-
15
- operation.urls.each do |url|
15
+ operation.urls.map do |url|
16
16
  csv_path = download(url)
17
- csv_handler.call(csv_path, &block)
18
- csv_path.delete
17
+ call_csv_handler(operation, csv_path, &block)
19
18
  end
20
19
  end
21
20
 
@@ -29,8 +28,11 @@ module Kamome
29
28
  @downloader ||= ::Kamome::Downloader.new(@config)
30
29
  end
31
30
 
32
- def build_csv_handler(operation)
33
- ::Kamome::CsvHandler.new(operation)
31
+ def call_csv_handler(operation, csv_path, &block)
32
+ ::Kamome::CsvHandler.new(operation).call(csv_path, &block)
33
+ ::Digest::SHA256.file(csv_path).to_s
34
+ ensure
35
+ csv_path.delete if @config.cleanup
34
36
  end
35
37
  end
36
38
  end
@@ -51,12 +51,18 @@ module Kamome
51
51
  # @return [Integer]
52
52
  attr_accessor :state
53
53
 
54
+ STATE_NOT_CHANGE = 0
55
+ STATE_UPDATE = 1
56
+ STATE_DELETE = 2
57
+
58
+ # @params [Hash] model attributes
54
59
  def initialize(params = {})
55
60
  params.each do |key, value|
56
61
  public_send("#{key}=", value)
57
62
  end
58
63
  end
59
64
 
65
+ # @return [Hash] model attributes
60
66
  def attributes
61
67
  {
62
68
  code: code,
@@ -71,12 +77,22 @@ module Kamome
71
77
  }
72
78
  end
73
79
 
74
- def changed?
75
- state == 1
80
+ # Returns true when state is 1 or 2
81
+ # @return [Boolean]
82
+ def change?
83
+ updated? || deleted?
84
+ end
85
+
86
+ # Returns true when state is 1
87
+ # @return [Boolean]
88
+ def update?
89
+ state == STATE_UPDATE
76
90
  end
77
91
 
92
+ # Returns true when state is 2
93
+ # @return [Boolean]
78
94
  def delete?
79
- state == 2
95
+ state == STATE_DELETE
80
96
  end
81
97
  end
82
98
  end
@@ -75,5 +75,9 @@ module Kamome
75
75
  def genre_jigyosho?
76
76
  genre == GENRE_JIGYOSHO
77
77
  end
78
+
79
+ def ==(other)
80
+ other.class == self.class && other.type == type && other.genre == genre && other.urls == urls
81
+ end
78
82
  end
79
83
  end
@@ -10,7 +10,7 @@ module Kamome
10
10
  value.sub(TOWN_KAN_SUB_RULE, '')
11
11
  end
12
12
 
13
- TOWN_SUB_RULE = /(((?!.+階)).+)|以下に掲載がない場合|.+の次に番地がくる場合|.+一円)$/.freeze
13
+ TOWN_SUB_RULE = /(((?!.+階)).+|以下に掲載がない場合|.+の次に番地がくる場合|.+一円|)$/.freeze
14
14
  def clean_town(value)
15
15
  value.sub(TOWN_SUB_RULE, '')
16
16
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kamome
4
- VERSION = '0.1.0'
4
+ VERSION = '0.8.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kamome
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kengos
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-30 00:00:00.000000000 Z
11
+ date: 2018-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -184,5 +184,5 @@ rubyforge_project:
184
184
  rubygems_version: 2.7.6
185
185
  signing_key:
186
186
  specification_version: 4
187
- summary: kamome-0.1.0
187
+ summary: kamome-0.8.0
188
188
  test_files: []