Class: Datapimp::Clients::Dropbox
- Inherits:
-
Object
- Object
- Datapimp::Clients::Dropbox
show all
- Includes:
- Singleton
- Defined in:
- lib/datapimp/clients/dropbox.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
75
76
77
78
79
80
81
|
# File 'lib/datapimp/clients/dropbox.rb', line 75
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
|
# File 'lib/datapimp/clients/dropbox.rb', line 14
def self.client(options={})
require 'dropbox-api' unless defined?(::Dropbox::API)
@client ||= begin
::Dropbox::API::Config.app_key = options.fetch(:dropbox_app_key) { Datapimp.config.dropbox_app_key }
::Dropbox::API::Config.app_secret = options.fetch(:dropbox_app_secret) { Datapimp.config.dropbox_app_secret }
::Dropbox::API::Config.mode = options.fetch(:dropbox_app_type) { Datapimp.config.dropbox_app_type }
instance.with_options(options)
end
end
|
.method_missing(meth, *args, &block) ⇒ Object
6
7
8
9
10
11
12
|
# File 'lib/datapimp/clients/dropbox.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
29
30
31
32
33
34
35
|
# File 'lib/datapimp/clients/dropbox.rb', line 29
def api
@api ||= begin
token = options.fetch(:token) { Datapimp.config.dropbox_client_token }
secret = options.fetch(:secret) { Datapimp.config.dropbox_client_secret }
::Dropbox::API::Client.new(token: token , secret: secret)
end
end
|
#authorize(token, secret) ⇒ Object
83
84
85
86
87
88
|
# File 'lib/datapimp/clients/dropbox.rb', line 83
def authorize(token, secret)
@api = nil if @api
options[:token] = token
options[:secret] = secret
self
end
|
#browser_authorization_url ⇒ Object
122
123
124
|
# File 'lib/datapimp/clients/dropbox.rb', line 122
def browser_authorization_url
@browser_authorization_url ||= request_token.authorize_url
end
|
#consume_auth_client_code(code = nil) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/datapimp/clients/dropbox.rb', line 126
def consume_auth_client_code code=nil
if code.nil?
query = browser_authorization_url.split('?').last
params = CGI.parse(query)
code = params['oauth_token'].first
end
request_token.get_access_token(:oauth_verifier => code).tap do |access_token|
Datapimp.config.set 'dropbox_client_token', access_token.token
Datapimp.config.set 'dropbox_client_secret', access_token.secret
end
end
|
#create_site_folder(folder, allow_overwrite = false) ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/datapimp/clients/dropbox.rb', line 67
def create_site_folder(folder, allow_overwrite=false)
found = find(folder)
unless (!found || (found && !allow_overwrite))
api.mkdir("/#{folder}")
end
end
|
#dropbox_app_key ⇒ Object
103
104
105
|
# File 'lib/datapimp/clients/dropbox.rb', line 103
def dropbox_app_key
Datapimp.config.dropbox_app_key.to_s
end
|
#dropbox_app_secret ⇒ Object
107
108
109
|
# File 'lib/datapimp/clients/dropbox.rb', line 107
def dropbox_app_secret
Datapimp.config.dropbox_app_secret.to_s
end
|
#find(path) ⇒ Object
37
38
39
40
41
|
# File 'lib/datapimp/clients/dropbox.rb', line 37
def find(path)
api.find(path)
rescue ::Dropbox::API::Error::NotFound
nil
end
|
#interactive_setup(options = {}) ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
# File 'lib/datapimp/clients/dropbox.rb', line 139
def interactive_setup(options={})
if requires_setup?
if dropbox_app_key.length == 0
if answer = options[:dropbox_app_key] || ask("What is the dropbox app key?", String)
Datapimp.config.set("dropbox_app_key", answer)
end
if answer = options[:dropbox_app_secret] || ask("What is the dropbox app secret?", String)
Datapimp.config.set("dropbox_app_secret", answer)
end
end
end
raise 'Missing dropbox application values' if requires_setup?
::Dropbox::API::Config.app_key = Datapimp.config.dropbox_app_key
::Dropbox::API::Config.app_secret = Datapimp.config.dropbox_app_secret
auth_url = browser_authorization_url
puts "\nGo to this url and click 'Authorize' to get the token:"
puts auth_url
Launchy.open(auth_url)
print "\nOnce you authorize the app on Dropbox, press enter... "
STDIN.gets.chomp
access_token = consume_auth_client_code()
puts "\nAuthorization complete!:\n\n"
puts " Dropbox::API::Config.app_key = '#{Datapimp.config.dropbox_app_key}'"
puts " Dropbox::API::Config.app_secret = '#{Datapimp.config.dropbox_app_secret}'"
puts " Dropbox::API::Config.mode = '#{Datapimp.config.dropbox_app_type}'"
puts " client = Dropbox::API::Client.new(:token => '#{access_token.token}', :secret => '#{access_token.secret}')"
puts "\n"
end
|
#options ⇒ Object
90
91
92
|
# File 'lib/datapimp/clients/dropbox.rb', line 90
def options
@options ||= {}
end
|
#push_from(local_path, options = {}) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/datapimp/clients/dropbox.rb', line 43
def push_from(local_path, options={})
path_prefix = options.fetch(:prefix)
root = options[:root]
unless find(path_prefix)
api.mkdir(path_prefix)
end
uploader = lambda do |node|
next if node.to_s == ".DS_Store"
if node.directory?
Array(node.children).each(&uploader)
elsif node.file?
relative = node.relative_path_from(local_path)
target = "#{path_prefix}/#{relative}"
target = target.gsub(/^\//,'')
api.upload(target, node.read)
end
end
Pathname(local_path).children.each(&uploader)
end
|
#request_token ⇒ Object
115
116
117
118
119
120
|
# File 'lib/datapimp/clients/dropbox.rb', line 115
def request_token
@request_token ||= begin
consumer = ::Dropbox::API::OAuth.consumer(:authorize)
consumer.get_request_token
end
end
|
#requires_setup? ⇒ Boolean
99
100
101
|
# File 'lib/datapimp/clients/dropbox.rb', line 99
def requires_setup?
!(dropbox_app_key.length > 0 && dropbox_app_secret.length > 0)
end
|
#sandboxed_app? ⇒ Boolean
24
25
26
27
|
# File 'lib/datapimp/clients/dropbox.rb', line 24
def sandboxed_app?
entry = api.ls.first
entry && entry.root == "app_folder"
end
|
#setup(options = {}) ⇒ Object
111
112
113
|
# File 'lib/datapimp/clients/dropbox.rb', line 111
def setup(options={})
interactive_setup(options)
end
|
#with_options(opts = {}) ⇒ Object
94
95
96
97
|
# File 'lib/datapimp/clients/dropbox.rb', line 94
def with_options(opts={})
options.merge!(opts)
self
end
|