drive_v3 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile ADDED
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ desc 'Run the same tasks that the CI build will run'
4
+ if RUBY_PLATFORM == 'java'
5
+ task default: %w[spec rubocop bundle:audit build]
6
+ else
7
+ task default: %w[spec rubocop yard bundle:audit build]
8
+ end
9
+
10
+ # Bundler Audit
11
+
12
+ require 'bundler/audit/task'
13
+ Bundler::Audit::Task.new
14
+
15
+ # Bundler Gem Build
16
+
17
+ require 'bundler'
18
+ require 'bundler/gem_tasks'
19
+
20
+ begin
21
+ Bundler.setup(:default, :development)
22
+ rescue Bundler::BundlerError => e
23
+ warn e.message
24
+ warn 'Run `bundle install` to install missing gems'
25
+ exit e.status_code
26
+ end
27
+
28
+ CLEAN << 'pkg'
29
+ CLEAN << 'Gemfile.lock'
30
+
31
+ # RSpec
32
+
33
+ require 'rspec/core/rake_task'
34
+
35
+ RSpec::Core::RakeTask.new do
36
+ if RUBY_PLATFORM == 'java'
37
+ ENV['JAVA_OPTS'] = '-Djdk.io.File.enableADS=true'
38
+ ENV['JRUBY_OPTS'] = '--debug'
39
+ ENV['NOCOV'] = 'TRUE'
40
+ end
41
+ end
42
+
43
+ CLEAN << 'coverage'
44
+ CLEAN << '.rspec_status'
45
+ CLEAN << 'rspec-report.xml'
46
+
47
+ # Rubocop
48
+
49
+ require 'rubocop/rake_task'
50
+
51
+ RuboCop::RakeTask.new do |t|
52
+ t.options = %w[
53
+ --display-cop-names
54
+ --display-style-guide
55
+ --extra-details
56
+ --format progress
57
+ --format json --out rubocop-report.json
58
+ ]
59
+ end
60
+
61
+ CLEAN << 'rubocop-report.json'
62
+
63
+ unless RUBY_PLATFORM == 'java'
64
+ # yard:build
65
+
66
+ require 'yard'
67
+
68
+ YARD::Rake::YardocTask.new('yard:build') do |t|
69
+ t.files = %w[lib/**/*.rb]
70
+ t.stats_options = ['--list-undoc']
71
+ end
72
+
73
+ CLEAN << '.yardoc'
74
+ CLEAN << 'doc'
75
+
76
+ # yard:audit
77
+
78
+ desc 'Run yardstick to show missing YARD doc elements'
79
+ task :'yard:audit' do
80
+ sh "yardstick 'lib/**/*.rb'"
81
+ end
82
+
83
+ # yard:coverage
84
+
85
+ require 'yardstick/rake/verify'
86
+
87
+ Yardstick::Rake::Verify.new(:'yard:coverage') do |verify|
88
+ verify.threshold = 100
89
+ verify.require_exact_threshold = false
90
+ end
91
+
92
+ task yard: %i[yard:build yard:audit yard:coverage]
93
+
94
+ # github-pages:publish
95
+
96
+ require 'github_pages_rake_tasks'
97
+ GithubPagesRakeTasks::PublishTask.new do |task|
98
+ # task.doc_dir = 'documentation'
99
+ task.verbose = true
100
+ end
101
+ end
@@ -0,0 +1,300 @@
1
+ # Google Drive Examples
2
+
3
+ Annotated examples written in Ruby.
4
+
5
+ Checked (✅︎) topics are completed. Topics without a check still need to be added.
6
+
7
+ * [Getting started](#getting-started)
8
+ * [✅︎ Creating a Google API service account](#︎-creating-a-google-api-service-account)
9
+ * [✅︎ Creating a DriveService instance](#︎-creating-a-driveservice-instance)
10
+ * [✅︎ Batching DriveService requests](#︎-batching-driveservice-requests)
11
+ * [Files and folders](#files-and-folders)
12
+ * [✅︎ Search for files](#︎-search-for-files)
13
+ * [✅︎ Get file](#︎-get-file)
14
+ * [✅︎ Create an empty data file](#︎-create-an-empty-data-file)
15
+ * [✅︎ Create a folder](#︎-create-a-folder)
16
+ * [✅︎ Create a spreadsheet, document, or presentation](#︎-create-a-spreadsheet-document-or-presentation)
17
+ * [✅︎ Upload file data](#︎-upload-file-data)
18
+ * [✅︎ Download file data](#︎-download-file-data)
19
+ * [✅︎ Export file](#︎-export-file)
20
+ * [✅︎ Send file to trash](#︎-send-file-to-trash)
21
+ * [✅︎ Recover file from trash](#︎-recover-file-from-trash)
22
+ * [✅︎ Delete a file](#︎-delete-a-file)
23
+ * [Share files, folders, and drives](#share-files-folders-and-drives)
24
+ * [✅︎ Create permission](#︎-create-permission)
25
+ * [✅︎ List permissions](#︎-list-permissions)
26
+ * [✅︎ Update permission](#︎-update-permission)
27
+ * [✅︎ Delete permission](#︎-delete-permission)
28
+ * [Shortcuts](#shortcuts)
29
+ * [Create a shortcut to a file](#create-a-shortcut-to-a-file)
30
+ * [Search for shortcuts](#search-for-shortcuts)
31
+ * [Other](#other)
32
+ * [File revisions](#file-revisions)
33
+ * [Store application-specific data](#store-application-specific-data)
34
+ * [Manage file metadata](#manage-file-metadata)
35
+ * [Manage comments and replies](#manage-comments-and-replies)
36
+ * [Add custom file properties](#add-custom-file-properties)
37
+ * [Create a shortcut to a Drive file](#create-a-shortcut-to-a-drive-file)
38
+ * [Create a shortcut to app content](#create-a-shortcut-to-app-content)
39
+ * [Protect file content from modification](#protect-file-content-from-modification)
40
+ * [Access link-shared files using resource keys](#access-link-shared-files-using-resource-keys)
41
+ * [Handle changes](#handle-changes)
42
+ * [Identify which change log to track](#identify-which-change-log-to-track)
43
+ * [Track changes for users and shared drives](#track-changes-for-users-and-shared-drives)
44
+ * [Retrieve changes](#retrieve-changes)
45
+ * [Receive notifications for resource changes](#receive-notifications-for-resource-changes)
46
+ * [Manage labels](#manage-labels)
47
+ * [Manage labels](#manage-labels-1)
48
+ * [Set label field](#set-label-field)
49
+ * [Unset label field](#unset-label-field)
50
+ * [Remove label](#remove-label)
51
+ * [List labels from file](#list-labels-from-file)
52
+ * [Return specific labels from a file](#return-specific-labels-from-a-file)
53
+ * [Search by label or field](#search-by-label-or-field)
54
+
55
+ ## Getting started
56
+
57
+ ### ✅︎ Creating a Google API service account
58
+
59
+ See the [Getting started](https://github.com/main-branch/drive_v3#getting-started)
60
+ section in the project README.md
61
+
62
+ ### ✅︎ Creating a DriveService instance
63
+
64
+ See [Obtaining an authenticated DriveService](https://github.com/main-branch/drive_v3#obtaining-an-authenticated-driveservice)
65
+ section in the project README.md
66
+
67
+ ### ✅︎ Batching DriveService requests
68
+
69
+ Each HTTP connection that your client makes results in a certain amount of overhead.
70
+ The Google Drive API supports batching, to allow your client to put several API
71
+ calls into a single HTTP request.
72
+
73
+ See [examples/drive_service_batch](https://github.com/main-branch/drive_v3/blob/main/examples/drive_service_batch)
74
+ for an example of how to batch Drive API.
75
+
76
+ You're limited to 100 calls in a single batch request. If you need to make more
77
+ calls than that, use multiple batch requests.
78
+
79
+ ## Files and folders
80
+
81
+ Google Drive organizes files in collections, describes files by types, and provides
82
+ specific attributes for each file to facilitate file manipulation.
83
+
84
+ The Google Drive API represents files stored on Drive as a File resource.
85
+
86
+ See [File & folders overview](https://developers.google.com/drive/api/guides/about-files)
87
+ in the Google Drive API documentation for more information.
88
+
89
+ Folders are treated as a type of file. For more details about folders, see
90
+ [File types](https://developers.google.com/drive/api/guides/about-files#types).
91
+
92
+ ### ✅︎ Search for files
93
+
94
+ [examples/file_search](https://github.com/main-branch/drive_v3/blob/main/examples/file_search)
95
+ shows how to list all the files for the authenticated user.
96
+
97
+ This example illustrates how to:
98
+ 1. Use a search query to filter results with specific examples for:
99
+ * Omitting files that are in the trash
100
+ * Only returning files in a specific folder
101
+ 2. Specify which data to return about each file
102
+ 3. How to retrieve the name of the containing folder for each file
103
+ 4. How to retrieve pagenated results
104
+
105
+ ### ✅︎ Get file
106
+
107
+ [examples/file_get](https://github.com/main-branch/drive_v3/blob/main/examples/file_get)
108
+ shows how to get a
109
+ [File](https://github.com/googleapis/google-api-ruby-client/blob/main/generated/google-apis-drive_v3/lib/google/apis/drive_v3/classes.rb)
110
+ from the drive and controlling which fields are returned.
111
+
112
+ ### ✅︎ Create an empty data file
113
+
114
+ To create a file that contains no metadata or content, use the `drive_service.create_file`
115
+ method with no parameters. The file is given a kind of drive.file, an id, a name of
116
+ "Untitled," and a mimeType of application/octet-stream.
117
+
118
+ Use the `:parents` parameter to give the id of the folder that should contain the
119
+ file. Omitting this parameter or passing an empty array will place the new file
120
+ in the user's `My Drive` root folder.
121
+
122
+ [examples/file_create](https://github.com/main-branch/drive_v3/blob/main/examples/file_create)
123
+ shows how to create an empty data file and write data to it.
124
+
125
+ ### ✅︎ Create a folder
126
+
127
+ [examples/file_create_folder](https://github.com/main-branch/drive_v3/blob/main/examples/file_create_folder)
128
+ shows how to create a folder by setting the appropriate mime-type.
129
+
130
+ See [Create and populate folders](https://developers.google.com/drive/api/guides/folder#create-folder)
131
+ for more information.
132
+
133
+ ### ✅︎ Create a spreadsheet, document, or presentation
134
+
135
+ [examples/file_create_spreadsheet](https://github.com/main-branch/drive_v3/blob/main/examples/file_create_spreadsheet)
136
+ shows how to create a blank spreadsheet by setting the appropriate mime-type.
137
+
138
+ See [Create a spreadsheet](https://developers.google.com/sheets/api/guides/create#work_with_folders)
139
+ for more information.
140
+
141
+ Use the following mime-types for Google apps:
142
+ * Spreadsheet: **application/vnd.google-apps.spreadsheet**
143
+ * Document: **application/vnd.google-apps.document**
144
+ * Presentation: **application/vnd.google-apps.presentation**
145
+
146
+ See [Google Workspace & Google Drive supported MIME types](https://developers.google.com/drive/api/guides/mime-types)
147
+ for a comprehensive list of MIME types supported by Google Drive.
148
+
149
+ ### ✅︎ Upload file data
150
+
151
+ Use `drive_service.update_file` to upload file content.
152
+
153
+ The Drive API does not allow for partial modifications or appending data directly on
154
+ the server. To achieve this, the entire file must first be downloaded from the
155
+ server. After downloading, any alterations or additions are made locally.
156
+ Once these modifications are complete, the updated file must be uploaded in its
157
+ entirety back to the server, effectively replacing the original file.
158
+
159
+ This approach ensures data integrity, but it might not be bandwidth-efficient,
160
+ especially for large files.
161
+
162
+ [examples/file_upload_content](https://github.com/main-branch/drive_v3/blob/main/examples/file_upload_content)
163
+ shows how to overwrite a file's content.
164
+
165
+ ### ✅︎ Download file data
166
+
167
+ Use `drive_service.get_file` to download file content.
168
+
169
+ [examples/file_download_content](https://github.com/main-branch/drive_v3/blob/main/examples/file_download_content)
170
+ shows how to download a file's content.
171
+
172
+ ### ✅︎ Export file
173
+
174
+ To export Google Workspace document, use the `files.export` method with
175
+ the ID of the file to export and the correct MIME type. Exported content is limited
176
+ to 10 MB.
177
+
178
+ See [Export MIME types for Google Workspace documents](https://developers.google.com/drive/api/guides/ref-export-formats)
179
+ for a list of possible MIME types for each document type.
180
+
181
+ See [examples/file_export_spreasheet](https://github.com/main-branch/drive_v3/blob/main/examples/file_export_spreadsheet)
182
+ for an example of how to export a spreadsheet to a PDF.
183
+
184
+ ### ✅︎ Send file to trash
185
+
186
+ To send a file to the trash, use `drive_service.update_file` to set the `trashed`
187
+ file attribute to `true`.
188
+
189
+ [examples/file_send_to_trash](https://github.com/main-branch/drive_v3/blob/main/examples/file_send_to_trash)
190
+ shows how to send a file to the trash.
191
+
192
+ ### ✅︎ Recover file from trash
193
+
194
+ To recover a file from the trash, use `drive_service.update_file` to set the `trashed`
195
+ file attribute to `false`.
196
+
197
+ [examples/file_recover_from_trash](https://github.com/main-branch/drive_v3/blob/main/examples/file_recover_from_trash)
198
+ shows how to recover a file from the trash.
199
+
200
+ ### ✅︎ Delete a file
201
+
202
+ Use `drive_service.delete_file` to delete a file without sending it to the trash.
203
+
204
+ [examples/file_delete](https://github.com/main-branch/drive_v3/blob/main/examples/file_delete)
205
+ shows how to delete a file.
206
+
207
+ ## Share files, folders, and drives
208
+
209
+ Every Google Drive file, folder, and shared drive have associated
210
+ [permission](https://developers.google.com/drive/api/v3/reference/permissions)
211
+ resources. Each resource identifies the permission for a specific `type` (user, group,
212
+ domain, anyone) and `role`, such as "commenter" or "reader." For example, a file might
213
+ have a permission granting a specific user (`type=user`) read-only access (`role=reader`)
214
+ while another permission grants members of a specific group (`type=group`) the ability
215
+ to add comments to a file (`role=commenter`).
216
+
217
+ For a complete list of roles and the operations permitted by each, refer to
218
+ [Roles & permissions-(https://developers.google.com/drive/api/guides/ref-roles).
219
+
220
+ ### ✅︎ Create permission
221
+
222
+ Use `drive_service.create_permission` to create a permission on a file.
223
+
224
+ [examples/permission_create](https://github.com/main-branch/drive_v3/blob/main/examples/permission_create)
225
+ shows how to create a permission for a file.
226
+
227
+ ### ✅︎ List permissions
228
+
229
+ Use `drive_service.list_permissions` to list the permissions on a file.
230
+
231
+ [examples/permission_list](https://github.com/main-branch/drive_v3/blob/main/examples/permission_list)
232
+ shows how to list the permissions for a file.
233
+
234
+ ### ✅︎ Update permission
235
+
236
+ Use `drive_service.update_permission` to list the permissions on a file.
237
+
238
+ [examples/permission_update](https://github.com/main-branch/drive_v3/blob/main/examples/permission_update)
239
+ shows how to update a permissions for a file.
240
+
241
+ ### ✅︎ Delete permission
242
+
243
+ Use `drive_service.delete_permission` to delete a permission for a file.
244
+
245
+ [examples/permission_delete](https://github.com/main-branch/drive_v3/blob/main/examples/permission_delete)
246
+ shows how to delete a permissions for a file.
247
+
248
+ ## Shortcuts
249
+
250
+ https://developers.google.com/drive/api/guides/shortcuts
251
+
252
+ ### Create a shortcut to a file
253
+
254
+ ### Search for shortcuts
255
+
256
+ ## Other
257
+
258
+ ### File revisions
259
+
260
+ ### Store application-specific data
261
+
262
+ ### Manage file metadata
263
+
264
+ ### Manage comments and replies
265
+
266
+ ### Add custom file properties
267
+
268
+ ### Create a shortcut to a Drive file
269
+
270
+ ### Create a shortcut to app content
271
+
272
+ ### Protect file content from modification
273
+
274
+ ### Access link-shared files using resource keys
275
+
276
+ ## Handle changes
277
+
278
+ ### Identify which change log to track
279
+
280
+ ### Track changes for users and shared drives
281
+
282
+ ### Retrieve changes
283
+
284
+ ### Receive notifications for resource changes
285
+
286
+ ## Manage labels
287
+
288
+ ### Manage labels
289
+
290
+ ### Set label field
291
+
292
+ ### Unset label field
293
+
294
+ ### Remove label
295
+
296
+ ### List labels from file
297
+
298
+ ### Return specific labels from a file
299
+
300
+ ### Search by label or field
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # `batch` allows you to batch multiple requests into a single HTTP request
5
+ #
6
+ # See [Batching Requests](https://developers.google.com/drive/api/v3/batch)
7
+ #
8
+ # ```Ruby
9
+ # drive_service.batch do |service|
10
+ # service.some_method(...)
11
+ # service.some_other_method(...)
12
+ # end
13
+ # ```
14
+ #
15
+ # The `batch` method yields a service object that can be used to make requests.
16
+ #
17
+ # Requests made within the block are sent in a single network request to the server.
18
+ # These requests are not performed within a transaction. If one request fails,
19
+ # subsequent requests will still be executed.
20
+ #
21
+ # The `batch` method returns after all requests have been completed.
22
+ #
23
+ # The return value should be ignored.
24
+ #
25
+ # The service methods calls within the block return nil. To collect the results
26
+ # of the requests, you must pass a block to the service method as follows:
27
+ #
28
+ # ```Ruby
29
+ # results = [].tap do |results|
30
+ # drive_service.batch do |service|
31
+ # service.some_method(...) do |res, err|
32
+ # results << (res || err)
33
+ # end
34
+ # end
35
+ #
36
+
37
+ require 'drive_v3'
38
+
39
+ @spreadsheet_ids = %w[
40
+ 1MNb_59W87lj75-9HqrEQoFBdrQiNl96rDlRy87sDIjs
41
+ 1AG9PC6iGXRmezc5lcF9MX-tR4rR2lvEC6zZZ0AOnqLk
42
+ 1bQlb77ID8_AL1F0hEiJVfit4YUpy3Nw-qLckwpWLCcI
43
+ BOGUS_ID
44
+ ]
45
+
46
+ drive_service = DriveV3.drive_service
47
+
48
+ permission = Google::Apis::DriveV3::Permission.new(
49
+ type: 'user',
50
+ email_address: 'jcouball@gmail.com',
51
+ role: 'writer'
52
+ )
53
+
54
+ begin
55
+ results = [].tap do |request_results|
56
+ drive_service.batch do |service|
57
+ @spreadsheet_ids.each do |spreadsheet_id|
58
+ # In the batch block, `create_permission` returns nil instead of the result
59
+ #
60
+ # Collect the result (or error if there is one) in the block passed to
61
+ # `create_permission`
62
+ service.create_permission(spreadsheet_id, permission) do |res, err|
63
+ request_results << (res || err.message)
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ puts "Permission created: #{results.pretty_inspect}"
70
+ rescue StandardError => e
71
+ puts "An error occurred: #{e.message}"
72
+ end
@@ -0,0 +1,66 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Create a data file
5
+ #
6
+ # When creating a data file, upload_source should be an IO object that
7
+ # responds to `read` and `size` (e.g. StringIO) or a string that names
8
+ # an existing file to upload.
9
+
10
+ require 'drive_v3'
11
+ require 'json'
12
+
13
+ drive_service = DriveV3.drive_service
14
+
15
+ # If name is not specified as part of a create request, the file name is named
16
+ # 'Untitled'.
17
+ name = 'My data file'
18
+
19
+ # If parents not specified as part of a create request or is an empty array, the
20
+ # file is placed directly in the user's My Drive folder
21
+ #
22
+ # parents = ['id1', 'id2']
23
+ parents = ['1V6RS_7_YgJmLDH-BDw3dpD8Np60Oi9ET']
24
+
25
+ # The MIME type of the file. Drive will attempt to automatically detect an
26
+ # appropriate value from uploaded content if no value is provided. The value
27
+ # cannot be changed unless a new revision is uploaded.
28
+ #
29
+ # The default MIME type is 'text/plain'
30
+ #
31
+ # mime_type = nil
32
+ mime_type = 'application/vnd.google-apps.spreadsheet'
33
+
34
+ file_metadata = { name:, parents:, mime_type: }
35
+
36
+ # If fields is not specified, the following default fields are returned:
37
+ # id, name, kind, and mime_type. '*' can by used to return all fields.
38
+ # See https://developers.google.com/drive/api/v3/reference/files#resource
39
+ #
40
+ fields = '*'
41
+
42
+ # When creating the data file, upload_source should be an IO object that
43
+ # responds to `read` and `size` (e.g. StringIO) or a string that names
44
+ # an existing file to upload.
45
+ #
46
+ # If upload_source is not specified, the file will be created as an empty
47
+ # data file.
48
+ #
49
+ # upload_source = StringIO.new("This is my letter to the World\nThat never wrote to Me")
50
+ upload_source = StringIO.new("1,2,3\n4,5,6\n7,8,9")
51
+
52
+ # Content type indicates the MIME type of the upload_source. If not specified,
53
+ # the content type will be determined by calling MIME::Types.of with the
54
+ # upload_source's filename.
55
+ #
56
+ # content_type = 'text/plain'
57
+ # CSV content type
58
+ # content_type = nil
59
+ content_type = 'text/csv'
60
+
61
+ begin
62
+ file = drive_service.create_file(file_metadata, fields:, upload_source:, content_type:)
63
+ puts JSON.pretty_generate(file.to_h)
64
+ rescue StandardError => e
65
+ puts "An error occurred: #{e.message}"
66
+ end
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Create a folder
5
+ #
6
+ # When creating a Google Drive folder, you create a file with a MIME type of
7
+ # 'application/vnd.google-apps.folder' and a name. You can also specify the
8
+ # folder's parent folder(s) and other metadata.
9
+
10
+ require 'drive_v3'
11
+ require 'json'
12
+
13
+ drive_service = DriveV3.drive_service
14
+
15
+ # If name is not specified as part of a create request, the file name is named
16
+ # 'Untitled'.
17
+ name = 'My New Folder'
18
+
19
+ # If parents not specified as part of a create request or is an empty array, the
20
+ # folder is placed directly in the user's My Drive folder
21
+ #
22
+ # parents = ['id1', 'id2']
23
+ parents = []
24
+
25
+ # The MIME type of the file. Drive will attempt to automatically detect an
26
+ # appropriate value from uploaded content if no value is provided. The value
27
+ # cannot be changed unless a new revision is uploaded.
28
+ #
29
+ # A folder MIME type is 'application/vnd.google-apps.folder'
30
+ #
31
+ mime_type = 'application/vnd.google-apps.folder'
32
+
33
+ # If fields is not specified, the following default fields are returned:
34
+ # id, name, kind, and mime_type. '*' can by used to return all fields.
35
+ # See https://developers.google.com/drive/api/v3/reference/files#resource
36
+ #
37
+ fields = '*'
38
+
39
+ file_metadata = { name:, parents:, mime_type: }
40
+
41
+ begin
42
+ file = drive_service.create_file(file_metadata, fields:)
43
+ puts JSON.pretty_generate(file.to_h)
44
+ rescue StandardError => e
45
+ puts "An error occurred: #{e.message}"
46
+ end
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Create a Google Sheets spreadsheet in Google Drive
5
+ #
6
+ # When creating a spreadsheet, you create a file with a MIME type of
7
+ # 'application/vnd.google-apps.spreadsheet' and a name. You can also specify the
8
+ # spreadsheet's parent folder and other metadata.
9
+ #
10
+ # The file_create method allows loading an Excel spreadheet or a sheet of data
11
+ # from CSV or TSV data. Specify the data to load using `upload_source` and
12
+ # `content_type` parameters. If `upload_source` is not specified, the file will
13
+ # be created as an empty spreadsheet.
14
+
15
+ require 'drive_v3'
16
+ require 'json'
17
+
18
+ drive_service = DriveV3.drive_service
19
+
20
+ # If name is not specified as part of a create request, the file name is named
21
+ # 'Untitled'.
22
+ name = 'My spreadsheet file'
23
+
24
+ # If parents not specified as part of a create request or is an empty array, the
25
+ # file is placed directly in the user's My Drive folder
26
+ #
27
+ # parents = ['id1', 'id2']
28
+ parents = []
29
+
30
+ # The MIME type of the file. Drive will attempt to automatically detect an
31
+ # appropriate value from uploaded content if no value is provided. The value
32
+ # cannot be changed unless a new revision is uploaded.
33
+ #
34
+ # The default MIME type is 'text/plain'
35
+ #
36
+ # mime_type = nil
37
+ mime_type = 'application/vnd.google-apps.spreadsheet'
38
+
39
+ file_metadata = { name:, parents:, mime_type: }
40
+
41
+ # If fields is not specified, the following default fields are returned:
42
+ # id, name, kind, and mime_type. '*' can by used to return all fields.
43
+ # See https://developers.google.com/drive/api/v3/reference/files#resource
44
+ #
45
+ fields = '*'
46
+
47
+ # When creating the data file, upload_source should be an IO object that
48
+ # responds to `read` and `size` (e.g. StringIO) or a string that names
49
+ # an existing file to upload.
50
+ #
51
+ # If upload_source is not specified, the file will be created as an empty
52
+ # data file.
53
+ #
54
+ upload_source = StringIO.new("1,2,3\n4,5,6\n7,8,9")
55
+
56
+ # Content type indicates the MIME type of `upload_source`. If not specified,
57
+ # the content type will be determined by calling MIME::Types.of with the
58
+ # upload_source's filename.
59
+ #
60
+ # content_type = 'text/plain'
61
+ # CSV content type
62
+ # content_type = nil
63
+ content_type = 'text/csv'
64
+
65
+ begin
66
+ file = drive_service.create_file(file_metadata, fields:, upload_source:, content_type:)
67
+ puts JSON.pretty_generate(file.to_h)
68
+ rescue StandardError => e
69
+ puts "An error occurred: #{e.message}"
70
+ end
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Permanently delete a file without moving it to the trash
5
+ #
6
+ # Moving a file to the trash is done by updating the file's trashed attribute to
7
+ # true. The process involves using the update method on the file you wish to move
8
+ # to the trash.
9
+
10
+ require 'drive_v3'
11
+
12
+ drive_service = DriveV3.drive_service
13
+
14
+ file_id = ARGV[0] || ENV.fetch('FILE_ID', nil)
15
+ raise 'Missing file_id' unless file_id
16
+
17
+ begin
18
+ print 'Delete file...'
19
+ # Does not return anything
20
+ drive_service.delete_file(file_id)
21
+ puts 'Success'
22
+ rescue StandardError => e
23
+ puts "An error occurred: #{e.message}"
24
+ end