tumugi-plugin-google_drive 0.3.0 → 0.4.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
  SHA1:
3
- metadata.gz: 6299d3432c8ea64f32980e96034eedd84ba7ccd9
4
- data.tar.gz: 24be0abafa651a00fb408bb5efcd791ddedc4028
3
+ metadata.gz: 4bfeef127b61e1749d6d732d9dc67825a37d1143
4
+ data.tar.gz: 9691c71410622d8e0fc1c331fbcdd63ed67538c8
5
5
  SHA512:
6
- metadata.gz: 4efc5aa4b66a9c81fb01b200290dacb44165e8a2b4390881678f10c8263b846de3733f82af1171cc39420c7a517cd3edfdd69c17ea3eb995784131176cdb517a
7
- data.tar.gz: 02d61db00ff93d161899a39f5e53bf99fcc85561289b6e538976aa73d4b16f1ba8bedb68a81bd3c7a79723b1310fb9269fc41d96a9391dcf967f3cdf5e5c0eeb
6
+ metadata.gz: 22938993b7ba94e3382e11294d4780895cad57b6b393b8c8e8ca2337e64a0723097352ae65e30db2b276b9c45eff4c28c0d80d5f42419aacb02eb1051714b1e0
7
+ data.tar.gz: 18c0701d6077d0736d1450bcb39af8856e08d17028db709fdcde77a649fcb633c2b19c2e09ab978fb982fce17f962ae2246a907fa9c91253beb38a39df59050e
@@ -1,5 +1,18 @@
1
1
  # Change Log
2
2
 
3
+ ## [v0.4.0](https://github.com/tumugi/tumugi-plugin-google_drive/tree/v0.4.0) (2016-08-29)
4
+ [Full Changelog](https://github.com/tumugi/tumugi-plugin-google_drive/compare/v0.3.0...v0.4.0)
5
+
6
+ **Implemented enhancements:**
7
+
8
+ - Support convert a file to the corresponding Google Docs format [\#16](https://github.com/tumugi/tumugi-plugin-google_drive/issues/16)
9
+ - Add method to get sharable link for Google Drive [\#13](https://github.com/tumugi/tumugi-plugin-google_drive/issues/13)
10
+
11
+ **Merged pull requests:**
12
+
13
+ - Support file type convert when import/export. [\#17](https://github.com/tumugi/tumugi-plugin-google_drive/pull/17) ([hakobera](https://github.com/hakobera))
14
+ - Add url method to GoogleDriveFileTarget and GoogleDriveFolderTarget [\#15](https://github.com/tumugi/tumugi-plugin-google_drive/pull/15) ([hakobera](https://github.com/hakobera))
15
+
3
16
  ## [v0.3.0](https://github.com/tumugi/tumugi-plugin-google_drive/tree/v0.3.0) (2016-08-02)
4
17
  [Full Changelog](https://github.com/tumugi/tumugi-plugin-google_drive/compare/v0.2.0...v0.3.0)
5
18
 
@@ -7,6 +20,10 @@
7
20
 
8
21
  - \[Breaking Change\] Support match by name [\#12](https://github.com/tumugi/tumugi-plugin-google_drive/pull/12) ([hakobera](https://github.com/hakobera))
9
22
 
23
+ **Merged pull requests:**
24
+
25
+ - Prepare release for 0.3.0 [\#14](https://github.com/tumugi/tumugi-plugin-google_drive/pull/14) ([hakobera](https://github.com/hakobera))
26
+
10
27
  ## [v0.2.0](https://github.com/tumugi/tumugi-plugin-google_drive/tree/v0.2.0) (2016-07-18)
11
28
  [Full Changelog](https://github.com/tumugi/tumugi-plugin-google_drive/compare/v0.1.0...v0.2.0)
12
29
 
data/README.md CHANGED
@@ -22,15 +22,18 @@ And then execute `bundle install`
22
22
 
23
23
  #### Parameters
24
24
 
25
- | name | type | required? | default | description |
26
- |---------|------------------------|-----------|---------|---------------------|
27
- | name | string | required | | Filename |
28
- | file_id | string | optional | | File ID |
29
- | parents | string or string array | optional | | Parent folder ID(s) |
25
+ | name | type | required? | default | description |
26
+ |-----------|------------------------|-----------|---------|---------------------|
27
+ | name | string | required | | Filename |
28
+ | file_id | string | optional | | File ID |
29
+ | parents | string or string array | optional | | Parent folder ID(s) |
30
+ | mime_type | string | optional | | MIME type of file |
31
+
32
+ `mime_type` is specifiy default mime_type value for `GoogleDriveFileTarget#open(mode="r", mime_type: @mime_type, &block)` method.
30
33
 
31
34
  #### Examples
32
35
 
33
- Create Google Drive file in folder named `xyz`, which content is "done"
36
+ ##### Create Google Drive file in folder named `xyz`, which content is "done"
34
37
 
35
38
  ```rb
36
39
  task :task1 do
@@ -38,8 +41,8 @@ task :task1 do
38
41
 
39
42
  output do
40
43
  target(:google_drive_file,
41
- name: "test_#{day.strftime('%Y%m%d')}.txt",
42
- parents: "xyz")
44
+ name: "test_#{day.strftime('%Y%m%d')}.txt",
45
+ parents: "xyz")
43
46
  end
44
47
 
45
48
  run do
@@ -49,6 +52,27 @@ task :task1 do
49
52
  end
50
53
  ```
51
54
 
55
+ ##### Upload CSV file and convert it to Google Sheets file
56
+
57
+ ```rb
58
+ task :task1 do
59
+ param :day, type: :time, auto_bind: true, required: true
60
+
61
+ output do
62
+ target(:google_drive_file,
63
+ name: "test_#{day.strftime('%Y%m%d')}.csv",
64
+ mime_type: "application/vnd.google-apps.spreadsheet")
65
+ end
66
+
67
+ run do
68
+ log "task1#run"
69
+ output.open("w") {|f| f.puts("header1,header2"); f.puts("value1,value2") }
70
+ # You can also specify mime_type by argument of open method.
71
+ # output.open("w", mime_type: "application/vnd.google-apps.spreadsheet") { ... }
72
+ end
73
+ end
74
+ ```
75
+
52
76
  ### Tumugi::Plugin::GoogleDriveFolderTarget
53
77
 
54
78
  `GoogleDriveFolderTarget` represents folder on Googl Drive.
@@ -6,8 +6,8 @@ task :task1 do
6
6
 
7
7
  output do
8
8
  target(:google_drive_file,
9
- name: "test_#{day.strftime('%Y%m%d%H%M')}_#{seed}.txt",
10
- parents: input.folder_id)
9
+ name: "test_#{day.strftime('%Y%m%d%H%M')}_#{seed}.txt",
10
+ parents: input.folder_id)
11
11
  end
12
12
 
13
13
  run do
@@ -6,15 +6,24 @@ module Tumugi
6
6
  class AtomicFile < Tumugi::AtomicFile
7
7
  attr_reader :id
8
8
 
9
- def initialize(path, fs, file_id: nil, parents: nil)
9
+ def initialize(path, fs, file_id: nil, parents: nil, mime_type: nil)
10
10
  super(path)
11
11
  @fs = fs
12
12
  @parents = parents
13
- @id = (file_id.nil? ? @fs.generate_file_id : file_id)
13
+ @id = file_id.nil? ? fs.generate_file_id : file_id
14
+ @mime_type = mime_type
15
+
16
+ # https://developers.google.com/drive/v3/web/manage-uploads#uploading_using_a_pregenerated_id
17
+ # Pregenerated IDs are not supported for native Google Document creation,
18
+ # or uploads where conversion to native Google Document format is requested.
19
+ if fs.google_drive_document?(mime_type)
20
+ @id = nil
21
+ end
14
22
  end
15
23
 
16
24
  def move_to_final_destination(temp_file)
17
- @fs.upload(temp_file, path, file_id: @id, parents: @parents)
25
+ file = @fs.upload(temp_file, path, file_id: @id, parents: @parents, mime_type: @mime_type)
26
+ @id = file.id unless @id
18
27
  end
19
28
  end
20
29
  end
@@ -1,4 +1,5 @@
1
1
  require 'google/apis/drive_v3'
2
+
2
3
  require 'tumugi/error'
3
4
 
4
5
  Tumugi::Config.register_section('google_drive', :project_id, :client_email, :private_key, :private_key_file)
@@ -7,7 +8,6 @@ module Tumugi
7
8
  module Plugin
8
9
  module GoogleDrive
9
10
  class FileSystem
10
-
11
11
  MIME_TYPE_FOLDER = 'application/vnd.google-apps.folder'
12
12
 
13
13
  def initialize(config)
@@ -15,7 +15,7 @@ module Tumugi
15
15
  end
16
16
 
17
17
  def exist?(file_id)
18
- client.get_file(file_id, options: request_options)
18
+ get_file_metadata(file_id, fields: 'id')
19
19
  true
20
20
  rescue => e
21
21
  return false if e.respond_to?(:status_code) && e.status_code == 404
@@ -39,7 +39,7 @@ module Tumugi
39
39
  parents: parents,
40
40
  id: folder_id
41
41
  })
42
- file = client.create_file(file_metadata, options: request_options)
42
+ file = client.create_file(file_metadata, fields: "*", options: request_options)
43
43
  wait_until { exist?(file.id) }
44
44
  file
45
45
  rescue
@@ -47,7 +47,7 @@ module Tumugi
47
47
  end
48
48
 
49
49
  def directory?(file_id)
50
- file = client.get_file(file_id, options: request_options)
50
+ file = get_file_metadata(file_id)
51
51
  file.mime_type == MIME_TYPE_FOLDER
52
52
  rescue
53
53
  process_error($!)
@@ -72,14 +72,15 @@ module Tumugi
72
72
  process_error($!)
73
73
  end
74
74
 
75
- def upload(media, name, content_type: nil, file_id: nil, parents: nil)
75
+ def upload(media, name, content_type: nil, file_id: nil, parents: nil, mime_type: nil)
76
76
  parents = [parents] if parents.is_a?(String)
77
77
  file_metadata = Google::Apis::DriveV3::File.new({
78
78
  id: file_id,
79
79
  name: name,
80
- parents: parents
80
+ parents: parents,
81
+ mime_type: mime_type
81
82
  })
82
- file = client.create_file(file_metadata, upload_source: media, content_type: content_type, options: request_options)
83
+ file = client.create_file(file_metadata, fields: "*", upload_source: media, content_type: content_type, options: request_options)
83
84
  wait_until { exist?(file.id) }
84
85
  file
85
86
  rescue
@@ -91,11 +92,29 @@ module Tumugi
91
92
  upload(media, name, content_type: content_type, file_id: file_id, parents: parents)
92
93
  end
93
94
 
94
- def download(file_id, download_path: nil, mode: 'r', &block)
95
+ def download(file_id, download_path: nil, mode: 'r', mime_type: nil, &block)
96
+ if !exist?(file_id)
97
+ raise Tumugi::FileSystemError.new("#{file_id} does not exist")
98
+ end
99
+
95
100
  if download_path.nil?
96
101
  download_path = Tempfile.new('tumugi_google_drive_file_system').path
97
102
  end
98
- client.get_file(file_id, download_dest: download_path, options: request_options)
103
+
104
+ file = get_file_metadata(file_id)
105
+ if google_drive_document?(file.mime_type)
106
+ # If file is Google Drive Document, use export_file
107
+ # https://developers.google.com/drive/v3/web/manage-downloads#downloading_google_documents
108
+ if mime_type.nil?
109
+ raise Tumugi::FileSystemError.new("mime_type is required because file #{file_id} is Google Drive Document")
110
+ end
111
+ client.export_file(file_id, mime_type, download_dest: download_path, options: request_options)
112
+ else
113
+ # If file is not Google Drive Document, use get_file
114
+ # https://developers.google.com/drive/v3/web/manage-downloads#downloading_a_file
115
+ client.get_file(file_id, download_dest: download_path, options: request_options)
116
+ end
117
+
99
118
  wait_until { File.exist?(download_path) }
100
119
 
101
120
  if block_given?
@@ -103,6 +122,7 @@ module Tumugi
103
122
  else
104
123
  File.open(download_path, mode)
105
124
  end
125
+ file
106
126
  rescue
107
127
  process_error($!)
108
128
  end
@@ -121,6 +141,14 @@ module Tumugi
121
141
  process_error($!)
122
142
  end
123
143
 
144
+ def get_file_metadata(file_id, fields: "*")
145
+ client.get_file(file_id, fields: fields, options: request_options)
146
+ end
147
+
148
+ def google_drive_document?(mime_type)
149
+ !!(mime_type && mime_type.start_with?("application/vnd.google-apps."))
150
+ end
151
+
124
152
  private
125
153
 
126
154
  def save_config(config)
@@ -173,16 +201,21 @@ module Tumugi
173
201
  begin
174
202
  if err.body.nil?
175
203
  reason = err.status_code.to_s
176
- errors = "HTTP Status: #{err.status_code}\nHeaders: #{err.header.inspect}"
204
+ errors = "HTTP Status: #{err.status_code}, Headers: #{err.header.inspect}"
177
205
  else
178
206
  jobj = JSON.parse(err.body)
179
207
  error = jobj["error"]
180
- reason = error["errors"].map{|e| e["reason"]}.join(",")
181
- errors = error["errors"].map{|e| e["message"] }.join("\n")
208
+ errors = "HTTP Status: #{err.status_code}, Headers: #{err.header.inspect}, Body: "
209
+ if error["errors"]
210
+ reason = error["errors"].map{|e| e["reason"]}.join(",")
211
+ errors += error["errors"].map{|e| e["message"] }.join(". ")
212
+ else
213
+ errors += error["message"]
214
+ end
182
215
  end
183
216
  rescue JSON::ParserError
184
217
  reason = err.status_code.to_s
185
- errors = "HTTP Status: #{err.status_code}\nHeaders: #{err.header.inspect}\nBody:\n#{err.body}"
218
+ errors = "HTTP Status: #{err.status_code}, Headers: #{err.header.inspect}, Body: #{err.body}"
186
219
  end
187
220
  raise Tumugi::FileSystemError.new(errors, reason)
188
221
  else
@@ -9,29 +9,31 @@ module Tumugi
9
9
  class GoogleDriveFileTarget < Tumugi::Plugin::FileSystemTarget
10
10
  Tumugi::Plugin.register_target('google_drive_file', self)
11
11
 
12
- attr_reader :file_id, :name, :parents
12
+ attr_reader :file_id, :name, :parents, :mime_type
13
13
 
14
- def initialize(file_id: nil, name:, parents: nil, fs: nil)
14
+ def initialize(file_id: nil, name:, parents: nil, mime_type: nil, fs: nil)
15
15
  @fs = fs unless fs.nil?
16
16
  @file_id = file_id
17
17
  @name = name
18
18
  @parents = parents
19
- super(file_id)
19
+ @mime_type = mime_type
20
+
21
+ super(@file_id)
20
22
  end
21
23
 
22
24
  def fs
23
25
  @fs ||= Tumugi::Plugin::GoogleDrive::FileSystem.new(Tumugi.config.section('google_drive'))
24
26
  end
25
27
 
26
- def open(mode="r", &block)
28
+ def open(mode="r", mime_type: @mime_type, &block)
27
29
  if mode.include? 'r'
28
30
  if file_id.nil?
29
31
  file = find_by_name(name)
30
32
  @file_id = file.id unless file.nil?
31
33
  end
32
- fs.download(file_id, mode: mode, &block)
34
+ fs.download(file_id, mode: mode, mime_type: mime_type, &block)
33
35
  elsif mode.include? 'w'
34
- file = Tumugi::Plugin::GoogleDrive::AtomicFile.new(name, fs, file_id: file_id, parents: @parents)
36
+ file = Tumugi::Plugin::GoogleDrive::AtomicFile.new(name, fs, file_id: file_id, parents: @parents, mime_type: mime_type)
35
37
  file.open(&block)
36
38
  @file_id = file.id
37
39
  else
@@ -53,6 +55,11 @@ module Tumugi
53
55
  s
54
56
  end
55
57
 
58
+ def url
59
+ file = fs.get_file_metadata(file_id)
60
+ file.web_view_link
61
+ end
62
+
56
63
  private
57
64
 
58
65
  def find_by_name(n)
@@ -24,7 +24,7 @@ module Tumugi
24
24
  if folder_id
25
25
  fs.exist?(folder_id)
26
26
  else
27
- !!find_by_name(name)
27
+ !find_by_name(name).nil?
28
28
  end
29
29
  end
30
30
 
@@ -40,6 +40,12 @@ module Tumugi
40
40
  s
41
41
  end
42
42
 
43
+ def url
44
+ return nil if folder_id.nil?
45
+ folder = fs.get_file_metadata(folder_id)
46
+ folder.web_view_link
47
+ end
48
+
43
49
  private
44
50
 
45
51
  def find_by_name(n)
@@ -51,7 +57,7 @@ module Tumugi
51
57
  if parents
52
58
  query += " and ("
53
59
  query += "#{ps.map{|p| "'#{p}' in parents"}.join(" or ")}"
54
- query += ") and mime_type = '#{Tumugi::Plugin::GoogleDrive::FileSystem::MIME_TYPE_FOLDER}'"
60
+ query += ") and mime_type = '#{Tumugi::Plugin::GoogleDrive::MimeTypes::DRIVE_FOLDER}'"
55
61
  end
56
62
  files = fs.list_files(query: query, page_size: 2).files
57
63
  if files.size == 0
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "tumugi-plugin-google_drive"
7
- spec.version = "0.3.0"
7
+ spec.version = "0.4.0"
8
8
  spec.authors = ["Kazuyuki Honda"]
9
9
  spec.email = ["hakobera@gmail.com"]
10
10
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tumugi-plugin-google_drive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kazuyuki Honda
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-02 00:00:00.000000000 Z
11
+ date: 2016-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: tumugi
@@ -136,7 +136,7 @@ files:
136
136
  - README.md
137
137
  - Rakefile
138
138
  - examples/example.rb
139
- - examples/tumgui_config_example.rb
139
+ - examples/tumugi_config_example.rb
140
140
  - lib/tumugi/plugin/google_drive/atomic_file.rb
141
141
  - lib/tumugi/plugin/google_drive/file_system.rb
142
142
  - lib/tumugi/plugin/target/google_drive_file.rb