Class: Datapimp::Clients::Google
- Inherits:
-
Object
- Object
- Datapimp::Clients::Google
show all
- Includes:
- Singleton
- Defined in:
- lib/datapimp/clients/google.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/datapimp/clients/google.rb', line 74
def method_missing meth, *args, &block
if api.respond_to?(meth)
return api.send(meth, *args, &block)
end
super
end
|
Class Method Details
.client(options = {}) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/datapimp/clients/google.rb', line 14
def self.client(options={})
unless defined?(::GoogleDrive)
require 'google_drive'
require 'google/api_client'
require 'google_drive/session'
end
@client ||= begin
instance.with_options(options)
end
end
|
.method_missing(meth, *args, &block) ⇒ Object
6
7
8
9
10
11
12
|
# File 'lib/datapimp/clients/google.rb', line 6
def self.method_missing(meth, *args, &block)
if client.respond_to?(meth)
return client.send(meth, *args, &block)
end
super
end
|
Instance Method Details
#api ⇒ Object
67
68
69
70
71
72
|
# File 'lib/datapimp/clients/google.rb', line 67
def api
@api ||= begin
refresh_access_token!
GoogleDrive.login_with_oauth(Datapimp.config.google_access_token)
end
end
|
#auth_client ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/datapimp/clients/google.rb', line 114
def auth_client
return @auth_client if @auth_client
client = ::Google::APIClient.new(
:application_name => "google_drive Ruby library",
:application_version => "0.3.11"
)
client_id = "452925651630-egr1f18o96acjjvphpbbd1qlsevkho1d.apps.googleusercontent.com"
client_secret = "1U3-Krii5x1oLPrwD5zgn-ry"
@auth_client = auth = client.authorization
auth.client_id = client_id auth.client_secret = client_secret auth.scope =
"https://www.googleapis.com/auth/drive " +
"https://spreadsheets.google.com/feeds/ " +
"https://docs.google.com/feeds/ " +
"https://docs.googleusercontent.com/"
auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
auth
end
|
#authorize(token, secret) ⇒ Object
82
83
84
85
86
87
|
# File 'lib/datapimp/clients/google.rb', line 82
def authorize(token, secret)
@api = nil if @api
options[:token] = token
options[:secret] = secret
self
end
|
#browser_authorization_url ⇒ Object
30
31
32
|
# File 'lib/datapimp/clients/google.rb', line 30
def browser_authorization_url
auth_client.authorization_uri
end
|
#consume_auth_client_code(code) ⇒ Object
34
35
36
37
38
39
|
# File 'lib/datapimp/clients/google.rb', line 34
def consume_auth_client_code code
auth_client.code = code
auth_client.fetch_access_token!
Datapimp.config.set "google_refresh_token", auth_client.refresh_token
Datapimp.config.set "google_access_token", auth_client.access_token
end
|
#get_application_keys ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/datapimp/clients/google.rb', line 102
def get_application_keys
unless Datapimp.config.google_client_id.to_s.length > 0
google_client_id = ask("What is the Google Client ID?", String)
Datapimp.config.set "google_client_id", google_client_id
end
unless Datapimp.config.google_client_secret.to_s.length > 0
google_client_secret = ask("What is the Google Client Secret?", String)
Datapimp.config.set "google_client_secret", google_client_secret
end
end
|
#has_application_keys? ⇒ Boolean
98
99
100
|
# File 'lib/datapimp/clients/google.rb', line 98
def has_application_keys?
(Datapimp.config.google_client_id.to_s.length > 0 && Datapimp.config.google_client_secret.to_s.length > 0)
end
|
#has_refresh_token? ⇒ Boolean
143
144
145
|
# File 'lib/datapimp/clients/google.rb', line 143
def has_refresh_token?
refresh_token.length > 0
end
|
#options ⇒ Object
89
90
91
|
# File 'lib/datapimp/clients/google.rb', line 89
def options
@options ||= {}
end
|
#refresh_access_token! ⇒ Object
147
148
149
150
151
152
153
|
# File 'lib/datapimp/clients/google.rb', line 147
def refresh_access_token!
if has_refresh_token?
auth_client.refresh_token = refresh_token
auth_client.fetch_access_token!
Datapimp.config.set "google_access_token", auth_client.access_token
end
end
|
#refresh_token ⇒ Object
139
140
141
|
# File 'lib/datapimp/clients/google.rb', line 139
def refresh_token
Datapimp.config.google_refresh_token.to_s
end
|
#refreshable? ⇒ Boolean
26
27
28
|
# File 'lib/datapimp/clients/google.rb', line 26
def refreshable?
has_application_keys? && has_refresh_token?
end
|
#session ⇒ Object
63
64
65
|
# File 'lib/datapimp/clients/google.rb', line 63
def session
api
end
|
#setup(options = {}) ⇒ Object
Runs through an interactive session where we get the necessary tokens needed to integrate with google drive.
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/datapimp/clients/google.rb', line 43
def setup(options={})
get_application_keys unless has_application_keys?
if options[:client_id]
Datapimp.config.set "google_client_id", options[:client_id]
end
if options[:client_secret]
Datapimp.config.set "google_client_secret", options[:client_secret]
end
if has_refresh_token?
refresh_access_token!
elsif respond_to?(:ask)
Launchy.open(browser_authorization_url)
say("\n1. Open this page:\n%s\n\n" % auth_client.authorization_uri)
consume_auth_client_code ask("2. Enter the authorization code shown in the page: ", String)
end
end
|
#with_options(opts = {}) ⇒ Object
93
94
95
96
|
# File 'lib/datapimp/clients/google.rb', line 93
def with_options(opts={})
options.merge!(opts)
self
end
|