drive_v3 0.2.0

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.
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json_schemer'
4
+
5
+ module DriveV3
6
+ # Creates a Google API credential with an access token
7
+ #
8
+ class CreateCredential
9
+ # Creates a Google API credential with an access token
10
+ #
11
+ # This wraps the boiler plate code into one function to make constructing a
12
+ # credential easy and less error prone.
13
+ #
14
+ # @example Constructing a credential from the contents of ~/.credential
15
+ # credential_source = File.read(File.join(Dir.home, '.credential'))
16
+ # scope = Google::Apis::DriveV3::AUTH_DRIVE
17
+ # credential = DriveV3::CreateCredential.call(credential_source, scope)
18
+ #
19
+ # @param [Google::Auth::*, String, IO, nil] credential_source may be one of four things:
20
+ # (1) a previously created credential that you want to reuse, (2) a credential read
21
+ # into a string, (3) an IO object with the credential ready to be read, or (4)
22
+ # if nill, the credential is read from ~/.google-api-credential.json
23
+ # @param scopes [Object, Array] one or more scopes to access.
24
+ # @param credential_factory [#make_creds] Used inject the credential_factory for tests
25
+ #
26
+ # @return [Object] a credential object with an access token
27
+ #
28
+ def self.call(
29
+ credential_source, scopes, credential_factory = Google::Auth::DefaultCredentials
30
+ )
31
+ return credential_source if credential?(credential_source)
32
+
33
+ credential_source ||= default_credential_source
34
+ options = make_creds_options(credential_source, scopes)
35
+ credential_factory.make_creds(options).tap(&:fetch_access_token)
36
+ end
37
+
38
+ private
39
+
40
+ # Reads credential source from `~/.google-api-credential.json`
41
+ #
42
+ # @return [String] the credential as a string
43
+ #
44
+ # @api private
45
+ #
46
+ private_class_method def self.default_credential_source
47
+ File.read(File.expand_path('~/.google-api-credential.json'))
48
+ end
49
+
50
+ # Constructs creds_options needed to create a credential
51
+ #
52
+ # @param [Google::Auth::*, String, #read] credential_source a credential (which
53
+ # is an object whose class is in the Google::Auth module), a String containing
54
+ # the credential, or a IO object with the credential ready to be read.
55
+ #
56
+ # @return [Hash] returns the cred_options
57
+ #
58
+ # @api private
59
+ #
60
+ private_class_method def self.make_creds_options(credential_source, scopes)
61
+ { json_key_io: to_io(credential_source), scope: scopes }
62
+ end
63
+
64
+ # Wraps a credential_source that is a string in a StringIO
65
+ #
66
+ # @param [Google::Auth::*, String, #read] credential_source a credential (which
67
+ # is an object whose class is in the Google::Auth module), a String containing
68
+ # the credential, or a IO object with the credential ready to be read.
69
+ #
70
+ # @return [IO, StringIO] returns a StringIO object is a String was passed in.
71
+ #
72
+ # @api private
73
+ #
74
+ private_class_method def self.to_io(credential_source)
75
+ credential_source.is_a?(String) ? StringIO.new(credential_source) : credential_source
76
+ end
77
+
78
+ # Determines if a credential_source is already a credential
79
+ #
80
+ # @param [Google::Auth::*, String, #read] credential_source a credential (which
81
+ # is an object whose class is in the Google::Auth module), a String containing
82
+ # the credential, or a IO object with the credential ready to be read.
83
+ #
84
+ # @return [Boolean] true if the credential source is an object whose class is in the
85
+ # Google::Auth module.
86
+ #
87
+ # @api private
88
+ #
89
+ private_class_method def self.credential?(credential_source)
90
+ credential_source.class.name.start_with?('Google::Auth::')
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DriveV3
4
+ # The version of this gem
5
+ VERSION = '0.2.0'
6
+ end
data/lib/drive_v3.rb ADDED
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'drive_v3/version'
4
+ require_relative 'drive_v3/create_credential'
5
+
6
+ require 'google/apis/drive_v3'
7
+ require 'json'
8
+ require 'logger'
9
+
10
+ # Unofficial helpers for the Google Drive V3 API
11
+ #
12
+ # @api public
13
+ #
14
+ module DriveV3
15
+ class << self
16
+ # Create a new Google::Apis::DriveV3::DriveService object
17
+ #
18
+ # Simplifies creating and configuring a the credential.
19
+ #
20
+ # @example using the credential in `~/.google-api-credential`
21
+ # DriveV3.drive_service
22
+ #
23
+ # @example using a credential passed in as a string
24
+ # credential_source = File.read(File.expand_path('~/.google-api-credential.json'))
25
+ # DriveV3.drive_service(credential_source:)
26
+ #
27
+ # @example using a credential passed in as an IO
28
+ # credential_source = File.open(File.expand_path('~/.google-api-credential.json'))
29
+ # DriveV3.drive_service(credential_source:)
30
+ #
31
+ # @param credential_source [nil, String, IO, Google::Auth::*] may
32
+ # be either an already constructed credential, the credential read into a String or
33
+ # an open file with the credential ready to be read. Passing `nil` will result
34
+ # in the credential being read from `~/.google-api-credential.json`.
35
+ #
36
+ # @param scopes [Object, Array] one or more scopes to access.
37
+ #
38
+ # @param credential_creator [#credential] Used to inject the credential creator for
39
+ # testing.
40
+ #
41
+ # @return a new DriveService instance
42
+ #
43
+ def drive_service(credential_source: nil, scopes: nil, credential_creator: DriveV3::CreateCredential)
44
+ credential_source ||= File.read(File.expand_path('~/.google-api-credential.json'))
45
+ scopes ||= [Google::Apis::DriveV3::AUTH_DRIVE]
46
+
47
+ Google::Apis::DriveV3::DriveService.new.tap do |service|
48
+ service.authorization = credential_creator.call(credential_source, scopes)
49
+ end
50
+ end
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,307 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drive_v3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - James Couball
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-12-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler-audit
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: create_github_release
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.48'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.48'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.22'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.22'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov-lcov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.8'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.8'
111
+ - !ruby/object:Gem::Dependency
112
+ name: redcarpet
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.6'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.6'
125
+ - !ruby/object:Gem::Dependency
126
+ name: yard
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.9'
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: 0.9.28
135
+ type: :development
136
+ prerelease: false
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - "~>"
140
+ - !ruby/object:Gem::Version
141
+ version: '0.9'
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: 0.9.28
145
+ - !ruby/object:Gem::Dependency
146
+ name: yardstick
147
+ requirement: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - "~>"
150
+ - !ruby/object:Gem::Version
151
+ version: '0.9'
152
+ type: :development
153
+ prerelease: false
154
+ version_requirements: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - "~>"
157
+ - !ruby/object:Gem::Version
158
+ version: '0.9'
159
+ - !ruby/object:Gem::Dependency
160
+ name: activesupport
161
+ requirement: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - "~>"
164
+ - !ruby/object:Gem::Version
165
+ version: '7.0'
166
+ type: :runtime
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ requirements:
170
+ - - "~>"
171
+ - !ruby/object:Gem::Version
172
+ version: '7.0'
173
+ - !ruby/object:Gem::Dependency
174
+ name: github_pages_rake_tasks
175
+ requirement: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - "~>"
178
+ - !ruby/object:Gem::Version
179
+ version: '0.1'
180
+ type: :development
181
+ prerelease: false
182
+ version_requirements: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - "~>"
185
+ - !ruby/object:Gem::Version
186
+ version: '0.1'
187
+ - !ruby/object:Gem::Dependency
188
+ name: google-apis-drive_v3
189
+ requirement: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - "~>"
192
+ - !ruby/object:Gem::Version
193
+ version: '0.26'
194
+ type: :runtime
195
+ prerelease: false
196
+ version_requirements: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - "~>"
199
+ - !ruby/object:Gem::Version
200
+ version: '0.26'
201
+ - !ruby/object:Gem::Dependency
202
+ name: googleauth
203
+ requirement: !ruby/object:Gem::Requirement
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ type: :runtime
209
+ prerelease: false
210
+ version_requirements: !ruby/object:Gem::Requirement
211
+ requirements:
212
+ - - ">="
213
+ - !ruby/object:Gem::Version
214
+ version: '0'
215
+ - !ruby/object:Gem::Dependency
216
+ name: json_schemer
217
+ requirement: !ruby/object:Gem::Requirement
218
+ requirements:
219
+ - - "~>"
220
+ - !ruby/object:Gem::Version
221
+ version: '2.0'
222
+ type: :runtime
223
+ prerelease: false
224
+ version_requirements: !ruby/object:Gem::Requirement
225
+ requirements:
226
+ - - "~>"
227
+ - !ruby/object:Gem::Version
228
+ version: '2.0'
229
+ - !ruby/object:Gem::Dependency
230
+ name: rltk
231
+ requirement: !ruby/object:Gem::Requirement
232
+ requirements:
233
+ - - "~>"
234
+ - !ruby/object:Gem::Version
235
+ version: '3.0'
236
+ type: :runtime
237
+ prerelease: false
238
+ version_requirements: !ruby/object:Gem::Requirement
239
+ requirements:
240
+ - - "~>"
241
+ - !ruby/object:Gem::Version
242
+ version: '3.0'
243
+ description: Unofficial helpers and extensions for the Google Drive V3 API
244
+ email:
245
+ - jcouball@yahoo.com
246
+ executables: []
247
+ extensions: []
248
+ extra_rdoc_files: []
249
+ files:
250
+ - ".markdownlint.yml"
251
+ - ".rspec"
252
+ - ".rubocop.yml"
253
+ - ".yardopts"
254
+ - CHANGELOG.md
255
+ - CODE_OF_CONDUCT.md
256
+ - LICENSE.txt
257
+ - README.md
258
+ - Rakefile
259
+ - examples/README.md
260
+ - examples/drive_service_batch
261
+ - examples/file_create
262
+ - examples/file_create_folder
263
+ - examples/file_create_spreadsheet
264
+ - examples/file_delete
265
+ - examples/file_download_content
266
+ - examples/file_export_spreadsheet
267
+ - examples/file_get
268
+ - examples/file_recover_from_trash
269
+ - examples/file_search
270
+ - examples/file_send_to_trash
271
+ - examples/file_upload_content
272
+ - examples/permission_create
273
+ - examples/permission_delete
274
+ - examples/permission_list
275
+ - examples/permission_update
276
+ - lib/drive_v3.rb
277
+ - lib/drive_v3/create_credential.rb
278
+ - lib/drive_v3/version.rb
279
+ homepage: https://github.com/main-branch/drive_v3
280
+ licenses:
281
+ - MIT
282
+ metadata:
283
+ allowed_push_host: https://rubygems.org
284
+ rubygems_mfa_required: 'true'
285
+ homepage_uri: https://github.com/main-branch/drive_v3
286
+ source_code_uri: https://github.com/main-branch/drive_v3
287
+ changelog_uri: https://rubydoc.info/gems/drive_v3/file/CHANGELOG.md
288
+ post_install_message:
289
+ rdoc_options: []
290
+ require_paths:
291
+ - lib
292
+ required_ruby_version: !ruby/object:Gem::Requirement
293
+ requirements:
294
+ - - ">="
295
+ - !ruby/object:Gem::Version
296
+ version: 3.1.0
297
+ required_rubygems_version: !ruby/object:Gem::Requirement
298
+ requirements:
299
+ - - ">="
300
+ - !ruby/object:Gem::Version
301
+ version: '0'
302
+ requirements: []
303
+ rubygems_version: 3.4.14
304
+ signing_key:
305
+ specification_version: 4
306
+ summary: Unofficial helpers and extensions for the Google Drive V3 API
307
+ test_files: []