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
66
67
68
69
70
71
72
|
# File 'lib/datapimp/clients/google.rb', line 66
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
59
60
61
62
63
64
|
# File 'lib/datapimp/clients/google.rb', line 59
def api
@api ||= begin
refresh_access_token!
GoogleDrive.login_with_oauth(Datapimp.config.google_access_token)
end
end
|
#auth_client ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/datapimp/clients/google.rb', line 106
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
74
75
76
77
78
79
|
# File 'lib/datapimp/clients/google.rb', line 74
def authorize(token, secret)
@api = nil if @api
options[:token] = token
options[:secret] = secret
self
end
|
#get_application_keys ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/datapimp/clients/google.rb', line 94
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
90
91
92
|
# File 'lib/datapimp/clients/google.rb', line 90
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
135
136
137
|
# File 'lib/datapimp/clients/google.rb', line 135
def has_refresh_token?
refresh_token.length > 0
end
|
#options ⇒ Object
81
82
83
|
# File 'lib/datapimp/clients/google.rb', line 81
def options
@options ||= {}
end
|
#refresh_access_token! ⇒ Object
139
140
141
142
143
144
145
|
# File 'lib/datapimp/clients/google.rb', line 139
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
131
132
133
|
# File 'lib/datapimp/clients/google.rb', line 131
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
55
56
57
|
# File 'lib/datapimp/clients/google.rb', line 55
def session
api
end
|
#setup(options = {}) ⇒ Object
Runs through an interactive session where we get the necessary tokens needed to integrate with google drive.
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/datapimp/clients/google.rb', line 32
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(auth_client.authorization_uri)
say("\n1. Open this page:\n%s\n\n" % auth_client.authorization_uri)
auth_client.code = ask("2. Enter the authorization code shown in the page: ", String)
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
end
|
#with_options(opts = {}) ⇒ Object
85
86
87
88
|
# File 'lib/datapimp/clients/google.rb', line 85
def with_options(opts={})
options.merge!(opts)
self
end
|