shrine-google_drive_storage 0.2.1 → 0.3.1

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: 20ce8cde9408e42b0f6760dcc15037cf42f6791d
4
- data.tar.gz: deaa6b58b6a306508fddcff85fa2e27af192d481
3
+ metadata.gz: 2cd52ffd710d628f2989ff2a50afdab0cc0269c8
4
+ data.tar.gz: c8129903b375d7e7262a804e0a872754bd3bda57
5
5
  SHA512:
6
- metadata.gz: 4ac056acfc70909e7aba8497a64d5d5903cd34d895eb9e8106a0bad783fd97a5e1075ab396f515b8681ce7ff0d11bb03ee2579d311fdcb711fc19db77a77b554
7
- data.tar.gz: e0e1a102ac83ff4d9ea0a6def751001ba2505c36d2e3d9c9571c3ae531afa69cc46e5035bc99d7b5ae2ffa9aa07b35e5f004ffb4657bd504073e8d0ada1ce7a5
6
+ metadata.gz: d92d3866ecfde61011444da6676ab59c8a9ccc5c9deb8992c582e56c9717d1c048928fc43218638548913260f8d84b65ec50af793d1f6f0e70c665d2c49952ee
7
+ data.tar.gz: 1e1792b37951651db999ec5946488e0b7fcd52e536a3d333c838ba3c2d517aa1f9479989979b4a27a35484fe943f245fd959186b43821ae954bb323314fda58c
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- # Shrine::GoogleDriveStorage
1
+ # Shrine::Storage::GoogleDriveStorage
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/shrine/google_drive_storage`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- Provides [Google Drive Storage] storage for [Shrine].
3
+ Provides [Google Drive Storage] for [Shrine].
6
4
 
7
5
  ## Installation
8
6
 
@@ -32,17 +30,7 @@ Google Drive is a free service for file storage files. In order to use this stor
32
30
 
33
31
  4. Now you will have a Client ID, Client Secret, and Redirect URL. So, download the client_secret_XXXXX.json file and rename it to client_secret.json.
34
32
 
35
- 5. Run the authorization task:
36
- ```sh
37
- $ rake google_drive:authorize"[path/to/client_secret.json, 'application_name']"
38
- ```
39
- NOTE:
40
- - the `path/to/client_secret.json` path is the file downloaded from Google console (it will be overrided with the fresh token).
41
- - the `application_name` param is the name that you set for the application credentials on Google console.
42
-
43
- 6. The Rake task will give you an auth url. Simply go to that url (while signed in as the designated uploads owner), authorize the app, then enter code from url in the console. The rake task will override valid `client_secret.json` which you can use to connect with GoogleDrive from now on.
44
-
45
- 7. Create a folder in which the files will be uploaded; note the folder's ID.
33
+ 5. Create a google drive folder in which the files will be uploaded; note the folder's ID.
46
34
 
47
35
  ## Usage
48
36
 
@@ -81,7 +69,7 @@ Example of the overridden `path/to/client_secret.json` file:
81
69
  "refresh_token": "1/_sVZIgY5thPetbWDTTTasdDID5Rkvq6UEfYshaDs5dIKoUAKgjE9f"
82
70
  }
83
71
  ```
84
- It is good practice to not include the credentials directly in the JSON file. Instead you can set them in environment variables and embed them with ERB.
72
+ It is good practice to not include the credentials directly in the JSON file. Instead you can set them in environment variables and embed them with ERB. Alternatively, add the .json extention to your .gitignore file.
85
73
 
86
74
  ## Options
87
75
 
@@ -95,14 +83,30 @@ This is a hash containing any of the following options:
95
83
 
96
84
  The :path option should be a block which returns a path that the uploaded file should be saved to. The block yields the attachment style and is executed in the scope of the model instance.
97
85
 
86
+ ## .env file
87
+ Create an `.env` file with these values:
88
+
89
+ ```sh
90
+ # .env
91
+ APPLICATION_NAME = "..."
92
+ CREDENTIALS_PATH = File.join(Dir.home, '.credentials',
93
+ "application-name.yaml")
94
+ ```
95
+
96
+ The application name in the above cases is the project name you chose in the [Google Developers console](https://console.developers.google.com/project)
97
+
98
+
98
99
  ## Contributing
99
100
 
100
101
  Bug reports and pull requests are welcome on GitHub at https://github.com/verynear/shrine-google_drive_storage. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
101
102
 
102
103
  ## License
103
104
 
104
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
105
+ [MIT License](http://opensource.org/licenses/MIT).
105
106
 
106
107
  ## Code of Conduct
107
108
 
108
109
  Everyone interacting in the Shrine::GoogleDriveStorage project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/verynear/shrine-google_drive_storage/blob/master/CODE_OF_CONDUCT.md).
110
+
111
+ [Google Drive Storage]: https://drive.google.com/drive/
112
+ [Shrine]: https://github.com/janko-m/shrine
@@ -1,13 +1,7 @@
1
- require 'active_support/core_ext/hash/keys'
2
- require 'active_support/inflector/methods'
3
- require 'active_support/core_ext/object/blank'
4
- require 'yaml'
5
- require 'erb'
6
-
7
1
  require 'shrine'
8
2
  require 'google/apis/drive_v3'
9
3
  require 'googleauth'
10
- require 'shrine/google_drive/session'
4
+ require 'googleauth/stores/file_token_store'
11
5
 
12
6
  require 'fileutils'
13
7
 
@@ -15,28 +9,7 @@ class Shrine
15
9
  module Storage
16
10
  class GoogleDriveStorage
17
11
  attr_reader :prefix
18
-
19
- def extended(base)
20
- check_gem_is_installed
21
- base.instance_eval do
22
- @google_drive_client_secret_path = @options[:google_drive_client_secret_path]
23
- @google_drive_options = @options[:google_drive_options] || { application_name: 'test-app' }
24
- raise(ArgumentError, 'You must provide a valid google_drive_client_secret_path option') unless @google_drive_client_secret_path
25
- raise(ArgumentError, 'You must set the public_folder_id option') unless @google_drive_options[:public_folder_id]
26
- google_api_client # Force validations of credentials
27
- end
28
- end
29
-
30
- def check_gem_is_installed
31
- begin
32
- require 'google-api-client'
33
- rescue LoadError => e
34
- e.message << '(You may need to install the google-api-client gem)'
35
- raise e
36
- end unless defined?(Google)
37
- end
38
12
 
39
-
40
13
  def initialize(prefix: nil, google_drive_options: {})
41
14
  @prefix = prefix
42
15
  @google_drive_client_secret_path = @options[:google_drive_client_secret_path]
@@ -62,13 +35,13 @@ class Shrine
62
35
  end
63
36
 
64
37
  def google_api_client
65
- @google_api_client ||= begin
66
- # Initialize the client & Google+ API
67
- ::Shrine::GoogleDrive::Session.from_config(
68
- @google_drive_client_secret_path,
69
- application_name: @google_drive_options[:application_name]
70
- )
38
+ if !@google_api_client || @google_api_client.authorization.expired?
39
+ service = Google::Apis::DriveV3::DriveService.new
40
+ service.client_options.application_name = ENV['APPLICATION_NAME']
41
+ service.authorization = authorize
42
+ @google_api_client = service
71
43
  end
44
+ @google_api_client
72
45
  end
73
46
 
74
47
  alias_method :google_drive, :google_api_client
@@ -137,7 +110,7 @@ class Shrine
137
110
  ids.each_slice(100) do |ids|
138
111
  client.batch do |client|
139
112
  ids.each do |id|
140
- client.delete_object(id)
113
+ client.delete_file(id)
141
114
  end
142
115
  end
143
116
  end
@@ -207,7 +180,7 @@ class Shrine
207
180
  # If it finds a file, return id of a file or nil
208
181
  # @param name [ String ]
209
182
  # @return [ String ] or NilClass
210
- def search_for_title(name)
183
+ def search_for_file(name)
211
184
  raise 'You are trying to search a file with NO name' if name.nil? || name.empty?
212
185
  client = google_api_client
213
186
  result = client.list_files(page_size: 1,
@@ -221,6 +194,40 @@ class Shrine
221
194
  end
222
195
  end
223
196
 
197
+ private
198
+
199
+ # Ensure valid credentials, either by restoring from the saved credentials
200
+ # files or intitiating an OAuth2 authorization. If authorization is required,
201
+ # the user's default browser will be launched to approve the request.
202
+ #
203
+ # @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
204
+ def authorize
205
+ scope = Google::Apis::DriveV3::AUTH_DRIVE
206
+ client_secrets_path = @google_drive_client_secret_path
207
+ credentials_path = ENV['CREDENTIALS_PATH']
208
+ FileUtils.mkdir_p(File.dirname(credentials_path))
209
+
210
+ client_id = Google::Auth::ClientId.from_file(client_secrets_path)
211
+ token_store = Google::Auth::Stores::FileTokenStore.new(file: credentials_path)
212
+ authorizer = Google::Auth::UserAuthorizer.new(
213
+ client_id, scope, token_store)
214
+ user_id = 'default'
215
+ oob_uri = 'urn:ietf:wg:oauth:2.0:oob'
216
+ credentials = authorizer.get_credentials(user_id)
217
+ if credentials.nil?
218
+ url = authorizer.get_authorization_url(
219
+ base_url: oob_uri)
220
+ $stderr.print("\n1. Open this page:\n%s\n\n" % url)
221
+ $stderr.print('2. Enter the authorization code shown in the page: ')
222
+ code = $stdin.gets.chomp
223
+ credentials = authorizer.get_and_store_credentials_from_code(
224
+ user_id: user_id, code: code, base_url: oob_uri, scope: scope)
225
+ end
226
+ credentials
227
+ end
228
+
229
+
230
+
224
231
  end
225
232
  end
226
233
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine-google_drive_storage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Near
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-28 00:00:00.000000000 Z
11
+ date: 2017-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shrine
@@ -44,36 +44,8 @@ dependencies:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
46
  version: 0.13.0
47
- - !ruby/object:Gem::Dependency
48
- name: bundler
49
- requirement: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - "~>"
52
- - !ruby/object:Gem::Version
53
- version: '1.15'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: '1.15'
61
47
  - !ruby/object:Gem::Dependency
62
48
  name: rake
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '10.0'
68
- type: :development
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '10.0'
75
- - !ruby/object:Gem::Dependency
76
- name: rspec
77
49
  requirement: !ruby/object:Gem::Requirement
78
50
  requirements:
79
51
  - - ">="
@@ -87,7 +59,7 @@ dependencies:
87
59
  - !ruby/object:Gem::Version
88
60
  version: '0'
89
61
  - !ruby/object:Gem::Dependency
90
- name: dotenv
62
+ name: minitest
91
63
  requirement: !ruby/object:Gem::Requirement
92
64
  requirements:
93
65
  - - ">="
@@ -101,73 +73,29 @@ dependencies:
101
73
  - !ruby/object:Gem::Version
102
74
  version: '0'
103
75
  - !ruby/object:Gem::Dependency
104
- name: activerecord
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - "~>"
108
- - !ruby/object:Gem::Version
109
- version: '4.2'
110
- - - ">="
111
- - !ruby/object:Gem::Version
112
- version: 4.2.0
113
- type: :development
114
- prerelease: false
115
- version_requirements: !ruby/object:Gem::Requirement
116
- requirements:
117
- - - "~>"
118
- - !ruby/object:Gem::Version
119
- version: '4.2'
120
- - - ">="
121
- - !ruby/object:Gem::Version
122
- version: 4.2.0
123
- - !ruby/object:Gem::Dependency
124
- name: railties
76
+ name: dotenv
125
77
  requirement: !ruby/object:Gem::Requirement
126
78
  requirements:
127
- - - "~>"
128
- - !ruby/object:Gem::Version
129
- version: '4.2'
130
79
  - - ">="
131
80
  - !ruby/object:Gem::Version
132
- version: 4.2.0
81
+ version: '0'
133
82
  type: :development
134
83
  prerelease: false
135
84
  version_requirements: !ruby/object:Gem::Requirement
136
85
  requirements:
137
- - - "~>"
138
- - !ruby/object:Gem::Version
139
- version: '4.2'
140
86
  - - ">="
141
87
  - !ruby/object:Gem::Version
142
- version: 4.2.0
88
+ version: '0'
143
89
  description: Provides Google Drive Storage for Shrine.
144
90
  email:
145
91
  - scott.a.near@gmail.com
146
- executables:
147
- - console
148
- - setup
92
+ executables: []
149
93
  extensions: []
150
94
  extra_rdoc_files: []
151
95
  files:
152
- - ".gitignore"
153
- - ".travis.yml"
154
- - CODE_OF_CONDUCT.md
155
- - Gemfile
156
96
  - LICENSE.txt
157
97
  - README.md
158
- - Rakefile
159
- - bin/console
160
- - bin/setup
161
- - lib/shrine-google-drive-storage.rb
162
- - lib/shrine/google_drive.rb
163
- - lib/shrine/google_drive/config.rb
164
- - lib/shrine/google_drive/railtie.rb
165
- - lib/shrine/google_drive/rake.rb
166
- - lib/shrine/google_drive/session.rb
167
- - lib/shrine/google_drive/tasks.rake
168
98
  - lib/shrine/storage/google_drive_storage.rb
169
- - lib/shrine/version.rb
170
- - shrine-google_drive_storage.gemspec
171
99
  homepage: https://github.com/verynear/shrine-google_drive_storage
172
100
  licenses:
173
101
  - MIT
data/.gitignore DELETED
@@ -1,13 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
10
- .config
11
-
12
- # rspec failure tracking
13
- .rspec_status
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.4.0
5
- # uncomment and edit the following line if your project needs to run something other than `rake`:
6
- script: bundle exec rspec spec
data/CODE_OF_CONDUCT.md DELETED
@@ -1,74 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
28
- * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- * Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at scott.near@elevateenergy.org. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [http://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: http://contributor-covenant.org
74
- [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- # Specify your gem's dependencies in shrine-google_drive_storage.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,6 +0,0 @@
1
- #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
3
-
4
- Bundler.setup
5
-
6
- load "shrine/google_drive/tasks.rake"
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "alegra"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,44 +0,0 @@
1
- # Author: Mateusz Czerwinski <mtczerwinski@gmail.com>
2
- # The license of this source is "New BSD Licence"
3
- require 'json'
4
- require 'googleauth'
5
- require 'fileutils'
6
-
7
- module Shrine
8
- module GoogleDrive
9
- # @api private
10
- class Config
11
- FIELDS = %w(client_id client_secret scope refresh_token).freeze
12
- attr_accessor(*FIELDS)
13
-
14
- def initialize(config_path)
15
- @config_path = config_path
16
- if ::File.exist?(config_path)
17
- JSON.parse(::File.read(config_path)).each do |key, value|
18
- instance_variable_set("@#{key}", value) if FIELDS.include?(key)
19
- end
20
- if !client_id
21
- auth_client = Google::Auth::ClientId.from_file(config_path)
22
- @client_id = auth_client.id
23
- @client_secret = auth_client.secret
24
- end
25
- end
26
- end
27
-
28
- def save
29
- ::File.open(@config_path, 'w', 0600) { |f| f.write(to_json) }
30
- end
31
-
32
- private
33
-
34
- def to_json
35
- hash = {}
36
- FIELDS.each do |field|
37
- value = __send__(field)
38
- hash[field] = value if value
39
- end
40
- JSON.pretty_generate(hash)
41
- end
42
- end
43
- end
44
- end
@@ -1,10 +0,0 @@
1
- require 'active_support/core_ext/module'
2
- module Shrine
3
- module GoogleDrive
4
- class Railtie < Rails::Railtie
5
- rake_tasks do
6
- load "shrine/google_drive/tasks.rake"
7
- end
8
- end
9
- end
10
- end
@@ -1,38 +0,0 @@
1
- require 'google/apis/drive_v3'
2
- require 'googleauth'
3
- require 'shrine/google_drive/session'
4
-
5
- module Shrine
6
- module GoogleDrive
7
- module Rake
8
- extend self
9
- ##
10
- # Ensure valid credentials, either by restoring from the saved credentials
11
- # files or intitiating an OAuth2 authorization. If authorization is required,
12
- # the user's default browser will be launched to approve the request.
13
- #
14
- # @param client_secret_path [ String ] with the location of the JSON file downloaded from Google console
15
- # @param application_name [ String ] given in the Google console > credentials > OAuth 2.0 client IDs section
16
- # @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
17
- def authorize(client_secret_path, application_name)
18
- client = Shrine::GoogleDrive::Session.from_config(client_secret_path, application_name: application_name)
19
- # # List the 10 most recently modified files.
20
- # response = client.list_files(page_size: 10, fields: 'nextPageToken, files(id, name)')
21
- # puts 'Files:'
22
- # puts 'No files found' if response.files.empty?
23
- # response.files.each do |file|
24
- # puts "#{file.name} (#{file.id})"
25
- # end
26
- if client
27
- puts "\nAuthorization completed.\n\n"
28
- puts "The credentials were saved into #{ client_secret_path}.\n"
29
- puts "You can use these credentials as follows: \n"
30
- puts "Shrine::GoogleDrive::Session.from_config(#{client_secret_path}, application_name: #{application_name})"
31
- puts "\n"
32
- else
33
- raise 'There where something wrong in the initialization of the google client (Google::Apis::DriveV3::DriveService)'
34
- end
35
- end
36
- end
37
- end
38
- end
@@ -1,108 +0,0 @@
1
- # Author: Hiroshi Ichikawa <http://gimite.net/>
2
- # The license of this source is "New BSD Licence"
3
- require 'google/apis/drive_v3'
4
- require 'googleauth'
5
- require 'shrine/google_drive/config'
6
- require 'fileutils'
7
-
8
- module Shrine
9
- module GoogleDrive
10
- # A session for Google Drive operations.
11
- #
12
- # Use from_credentials, from_access_token, from_service_account_key or from_config
13
- # class method to construct a Shrine::GoogleDrive::Session object.
14
- class Session
15
-
16
- DEFAULT_SCOPE = Google::Apis::DriveV3::AUTH_DRIVE
17
-
18
- # Returns Google::Apis::DriveV3::DriveService constructed from a config JSON file at +config+.
19
- #
20
- # +config+ is the path to the config file.
21
- #
22
- # This will prompt the credential via command line for the first time and save it to
23
- # +config+ for later usages.
24
- #
25
- # See https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md for a usage example.
26
- #
27
- # You can also provide a config object that must respond to:
28
- # client_id
29
- # client_secret
30
- # refesh_token
31
- # refresh_token=
32
- # scope
33
- # scope=
34
- # save
35
- class << self
36
- def from_config(config_path, options = {})
37
- validate_options(options)
38
- config = get_cofiguration(config_path, options)
39
- credentials = Google::Auth::UserRefreshCredentials.new(
40
- client_id: config.client_id,
41
- client_secret: config.client_secret,
42
- scope: config.scope,
43
- redirect_uri: 'urn:ietf:wg:oauth:2.0:oob'
44
- )
45
- if config.refresh_token
46
- credentials.refresh_token = config.refresh_token
47
- credentials.fetch_access_token!
48
- else
49
- $stderr.print("\n1. Open this page:\n%s\n\n" % credentials.authorization_uri)
50
- $stderr.print('2. Enter the authorization code shown in the page: ')
51
- credentials.code = $stdin.gets.chomp
52
- credentials.fetch_access_token!
53
- config.refresh_token = credentials.refresh_token
54
- end
55
- config.save
56
- init_drive_service(options[:application_name], credentials)
57
- end
58
-
59
- # @param config_path [ String ]
60
- # @param options [ Hash ]
61
- # @return [ Shrine::GoogleDrive::Config ]
62
- def get_cofiguration(config_path, options)
63
- if config_path.is_a?(String)
64
- config = Shrine::GoogleDrive::Config.new(config_path)
65
- else
66
- raise(ArgumentError, 'You must set a valid config_path path')
67
- end
68
- config.scope ||= DEFAULT_SCOPE
69
- config_from_options(config, options)
70
- end
71
-
72
- # @param options [ Hash ]
73
- # @param config [ Shrine::GoogleDrive::Config ]
74
- # @return [ Shrine::GoogleDrive::Config ]
75
- def config_from_options(config, options)
76
- if options[:client_id] && options[:client_secret]
77
- config.client_id = options[:client_id]
78
- config.client_secret = options[:client_secret]
79
- end
80
- config
81
- end
82
-
83
- # @param options [ Hash ]
84
- def validate_options(options)
85
- raise(ArgumentError, 'You must specify the application_name option') unless options[:application_name]
86
- raise(ArgumentError, 'client_id and client_secret must be both specified or both omitted') if invalid_client_options?(options)
87
- end
88
-
89
- # @param options [ Hash ]
90
- # @return [ Boolean ]
91
- def invalid_client_options?(options)
92
- (options[:client_id] && !options[:client_secret]) || (!options[:client_id] && options[:client_secret])
93
- end
94
-
95
- # @param application_name [ String ]
96
- # @param credentials [ Google::Auth::UserRefreshCredentials ]
97
- # @return [ Google::Apis::DriveV3::DriveService ]
98
- def init_drive_service(application_name, credentials)
99
- # Initialize the API
100
- client = Google::Apis::DriveV3::DriveService.new
101
- client.client_options.application_name = application_name
102
- client.authorization = credentials
103
- client
104
- end
105
- end
106
- end
107
- end
108
- end
@@ -1,10 +0,0 @@
1
- require "shrine/google_drive/rake"
2
-
3
- namespace :google_drive do
4
- desc "Authorize Google Drive account: "
5
- task :authorize, [:client_secret_path, :application_name] do |_t, args|
6
- client_secret_path = args[:client_secret_path]
7
- application_name = args[:application_name]
8
- Shrine::GoogleDrive::Rake.authorize(client_secret_path, application_name)
9
- end
10
- end
@@ -1,2 +0,0 @@
1
- require "shrine/storage/google_drive_storage"
2
- require "shrine/google_drive/railtie" if defined?(Rails)
@@ -1,5 +0,0 @@
1
- module Shrine
2
- module GoogleDriveStorage
3
- VERSION = "0.2.1"
4
- end
5
- end
@@ -1,4 +0,0 @@
1
- require 'active_support/core_ext/module'
2
- require 'shrine'
3
- require 'shrine/google_drive' if defined?(Rails)
4
- require 'shrine/storage/google_drive_storage'
@@ -1,43 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "shrine/version"
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "shrine-google_drive_storage"
8
- spec.version = Shrine::GoogleDriveStorage::VERSION
9
- spec.authors = ["Scott Near"]
10
- spec.email = ["scott.a.near@gmail.com"]
11
-
12
- spec.summary = "Provides Google Drive Storage for Shrine."
13
- spec.description = "Provides Google Drive Storage for Shrine."
14
- spec.homepage = "https://github.com/verynear/shrine-google_drive_storage"
15
- spec.license = "MIT"
16
-
17
- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
- # to allow pushing to a single host or delete this section to allow pushing to any host.
19
- # if spec.respond_to?(:metadata)
20
- # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
- # else
22
- # raise "RubyGems 2.0 or newer is required to protect against " \
23
- # "public gem pushes."
24
- # end
25
-
26
- spec.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
27
-
28
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
29
- f.match(%r{^(test|spec|features)/})
30
- end
31
-
32
- spec.require_paths = ["lib"]
33
-
34
- spec.add_dependency 'shrine', '~> 2.6', '>= 2.6.1'
35
- spec.add_dependency 'google-api-client', '~> 0.13.0'
36
-
37
- spec.add_development_dependency "bundler", "~> 1.15"
38
- spec.add_development_dependency "rake", "~> 10.0"
39
- spec.add_development_dependency "rspec"
40
- spec.add_development_dependency "dotenv"
41
- spec.add_development_dependency "activerecord", "~> 4.2", ">= 4.2.0"
42
- spec.add_development_dependency "railties", "~> 4.2", ">= 4.2.0"
43
- end