google_drive-persistent_session 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +2 -0
- data/google_drive-persistent_session.gemspec +24 -0
- data/lib/google_drive/persistent_session.rb +120 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0b67e3482df9ee92791d942800255769a7f94218
|
4
|
+
data.tar.gz: c5d45932dbcd371b20dabd8ba57a7ec1e40072e6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ec7b5e0d73864b5f05b3e147dc303f81596c1d32f241b9e3ef3f53cde3ce181c0abbf6e356ce390ff1741ad546cafcbef311da6b4cd06cb0ce61003045643cb0
|
7
|
+
data.tar.gz: e512231aaf892076cc1f449ffd5191b096c192c394a22d367719d326f94679ccbfc044e193ec5ff366c8afad03362ff3247014aae0c5e688c84d9e993afbabaf
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Genki Sugawara
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# GoogleDrive::PersistentSession
|
2
|
+
|
3
|
+
Persist credential for [google-drive-ruby](https://github.com/gimite/google-drive-ruby).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'google_drive-persistent_session'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install google_drive-persistent_session
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'google_drive/persistent_session'
|
25
|
+
|
26
|
+
#GoogleDrive::PersistentSession.credential_store_file = '~/.google_drive-oauth2.json'
|
27
|
+
|
28
|
+
session = GoogleDrive::PersistentSession.login
|
29
|
+
|
30
|
+
session.files.each do |file|
|
31
|
+
puts file.title
|
32
|
+
end
|
33
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'google_drive-persistent_session'
|
7
|
+
spec.version = '0.1.0'
|
8
|
+
spec.authors = ['Genki Sugawara']
|
9
|
+
spec.email = ['sgwr_dts@yahoo.co.jp']
|
10
|
+
spec.summary = %q{Persist credential for google-drive-ruby.}
|
11
|
+
spec.description = %q{Persist credential for google-drive-ruby.}
|
12
|
+
spec.homepage = 'https://github.com/winebarrel/google_drive-persistent_session'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_dependency 'google_drive', '>= 1.0.0'
|
21
|
+
spec.add_dependency 'highline'
|
22
|
+
spec.add_development_dependency 'bundler'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'forwardable'
|
3
|
+
require 'google/api_client'
|
4
|
+
require 'google/api_client/auth/file_storage'
|
5
|
+
require 'google_drive'
|
6
|
+
require 'highline'
|
7
|
+
|
8
|
+
module GoogleDrive
|
9
|
+
class PersistentSession
|
10
|
+
extend Forwardable
|
11
|
+
|
12
|
+
def_delegator :@highline, :ask
|
13
|
+
|
14
|
+
THREAD_KEY = "#{self.to_s}::THREAD_KEY"
|
15
|
+
|
16
|
+
DEFAULE_CREDENTIAL_STORE_FILE = '~/.google_drive-oauth2.json'
|
17
|
+
@credential_store_file = ::File.expand_path(DEFAULE_CREDENTIAL_STORE_FILE)
|
18
|
+
|
19
|
+
class << self
|
20
|
+
attr_accessor :credential_store_file
|
21
|
+
|
22
|
+
def login
|
23
|
+
self.new
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize
|
28
|
+
@highline = HighLine.new
|
29
|
+
ensure_login
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def ensure_login
|
35
|
+
unless is_credential_stored?
|
36
|
+
create_credential_store_file
|
37
|
+
end
|
38
|
+
|
39
|
+
credential = file_storage.load_credentials
|
40
|
+
|
41
|
+
if !credential.access_token
|
42
|
+
fetch_access_token(credential)
|
43
|
+
elsif expired?(credential)
|
44
|
+
refresh(credential)
|
45
|
+
end
|
46
|
+
|
47
|
+
unless session
|
48
|
+
login_with_oauth(credential.access_token)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def is_credential_stored?
|
53
|
+
::File.exist?(self.class.credential_store_file)
|
54
|
+
end
|
55
|
+
|
56
|
+
def expired?(credential)
|
57
|
+
credential.issued_at + credential.expires_in <= Time.new
|
58
|
+
end
|
59
|
+
|
60
|
+
def create_credential_store_file
|
61
|
+
client_id = ask('Enter CLIENT ID: ')
|
62
|
+
client_secret = ask('Enter CLIENT SECRET: ') {|q| q.echo = false }
|
63
|
+
puts # line break
|
64
|
+
|
65
|
+
credential = Signet::OAuth2::Client.new(
|
66
|
+
:client_id => client_id,
|
67
|
+
:client_secret => client_secret,
|
68
|
+
:refresh_token => ''
|
69
|
+
)
|
70
|
+
|
71
|
+
file_storage.write_credentials(credential)
|
72
|
+
FileUtils.chmod(0600, credential_store_file)
|
73
|
+
end
|
74
|
+
|
75
|
+
def fetch_access_token(credential)
|
76
|
+
credential.scope = %w(
|
77
|
+
https://www.googleapis.com/auth/drive
|
78
|
+
https://spreadsheets.google.com/feeds/
|
79
|
+
).join(' ')
|
80
|
+
|
81
|
+
credential.redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
|
82
|
+
credential.grant_type = 'authorization_code'
|
83
|
+
|
84
|
+
message = "1. Open this page:\n%s\n\n" % credential.authorization_uri
|
85
|
+
message << "2. Enter the authorization code shown in the page: "
|
86
|
+
credential.code = ask(message)
|
87
|
+
|
88
|
+
credential.fetch_access_token!
|
89
|
+
file_storage.write_credentials(credential)
|
90
|
+
login_with_oauth(credential.access_token)
|
91
|
+
end
|
92
|
+
|
93
|
+
def refresh(credential)
|
94
|
+
credential.refresh!
|
95
|
+
file_storage.write_credentials(credential)
|
96
|
+
login_with_oauth(credential.access_token)
|
97
|
+
end
|
98
|
+
|
99
|
+
def session
|
100
|
+
Thread.current[THREAD_KEY]
|
101
|
+
end
|
102
|
+
|
103
|
+
def login_with_oauth(access_token)
|
104
|
+
Thread.current[THREAD_KEY] = GoogleDrive.login_with_oauth(access_token)
|
105
|
+
end
|
106
|
+
|
107
|
+
def file_storage
|
108
|
+
@file_storage ||= Google::APIClient::FileStorage.new(credential_store_file)
|
109
|
+
end
|
110
|
+
|
111
|
+
def credential_store_file
|
112
|
+
self.class.credential_store_file
|
113
|
+
end
|
114
|
+
|
115
|
+
def method_missing(method_name, *args , &block)
|
116
|
+
ensure_login
|
117
|
+
session.send(method_name, *args, &block)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_drive-persistent_session
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genki Sugawara
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: google_drive
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: highline
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Persist credential for google-drive-ruby.
|
70
|
+
email:
|
71
|
+
- sgwr_dts@yahoo.co.jp
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- google_drive-persistent_session.gemspec
|
82
|
+
- lib/google_drive/persistent_session.rb
|
83
|
+
homepage: https://github.com/winebarrel/google_drive-persistent_session
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.0.14
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Persist credential for google-drive-ruby.
|
107
|
+
test_files: []
|