dbox 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.
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'dbox'
8
+
9
+ class Test::Unit::TestCase
10
+ end
data/test/test_dbox.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestDbox < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Dropbox (Evenflow, Inc.), http://getdropbox.com/
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,15 @@
1
+ Getting started with the Dropbox ruby library:
2
+ 1. Make sure you have rake and rubygems installed on your system.
3
+ 1b. Some of the gems below may need to be compiled locally, so make sure you
4
+ have any necessary source code around as well.
5
+ e.g. 'apt-get install ruby1.x-dev libxml2-dev libxslt1-dev' on ubuntu
6
+ 2. Install oauth from rubygems
7
+ 3. Install multipart-post from rubygems
8
+ 4. Install json from rubygems
9
+ 5. Install mechanize from rubygems
10
+ 6. Install shoulda from rubygems
11
+ 7. Copy config/testing.json.example to config/testing.json
12
+ 8. In config/testing.json enter your application's consumer key, secret key,
13
+ and your test user email and password.
14
+ 9. Run 'rake test_units' to make sure all the tests pass.
15
+ 10. Start developing your Dropbox API application!
@@ -0,0 +1,41 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/gempackagetask'
4
+ require 'rubygems'
5
+
6
+ manifest = File.readlines('manifest').map! { |x| x.chomp! }
7
+
8
+ spec = Gem::Specification.new do |s|
9
+ s.name = %q{dropbox}
10
+ s.version = '1.0'
11
+
12
+ s.authors = ["Dropbox, Inc."]
13
+ s.date = Time.now.utc.strftime('%Y-%m-%d')
14
+ s.description = "Dropbox REST API Client Library"
15
+ s.email = %q{support@dropbox.com}
16
+ s.executables = %w()
17
+ s.extensions = %w()
18
+
19
+ s.files = manifest
20
+ s.homepage = %q{http://developers.dropbox.com/}
21
+
22
+ summary = %q{Dropbox REST API Client Library}
23
+ s.require_paths = %w(lib)
24
+ s.summary = summary
25
+ end
26
+
27
+ task :default => [:test_units]
28
+
29
+ desc "Run basic tests"
30
+ Rake::TestTask.new("test_units") { |t|
31
+ t.pattern = 'test/*_test.rb'
32
+ t.verbose = true
33
+ t.warning = true
34
+ }
35
+
36
+ Rake::GemPackageTask.new(spec) do |pkg|
37
+ pkg.need_zip = true
38
+ pkg.need_tar = true
39
+ end
40
+
41
+
@@ -0,0 +1,16 @@
1
+ {
2
+ "server": "api.dropbox.com",
3
+ "content_server": "api-content.dropbox.com",
4
+ "port": 80,
5
+ "request_token_url": "http://api.dropbox.com/0/oauth/request_token",
6
+ "access_token_url": "http://api.dropbox.com/0/oauth/access_token",
7
+ "authorization_url": "http://www.dropbox.com/0/oauth/authorize",
8
+ "consumer_key": "YOUR CONSUMER KEY",
9
+ "consumer_secret": "YOUR CONSUMER SECRET",
10
+ "verifier": "",
11
+ "testing_user": "USER TO TEST WITH",
12
+ "testing_password": "PASSWORD",
13
+ "root": "dropbox"
14
+ }
15
+
16
+
@@ -0,0 +1,226 @@
1
+ require 'rubygems'
2
+ require 'oauth'
3
+ require 'json'
4
+ require 'uri'
5
+ require 'net/http/post/multipart'
6
+
7
+ class Authenticator
8
+
9
+ def initialize(config, key=nil, secret=nil, is_request=false, server=nil)
10
+ @consumer_key = config['consumer_key']
11
+ @consumer_secret = config['consumer_secret']
12
+ @config = config
13
+ @oauth_conf = {
14
+ :site => "http://" + (server || config["server"]),
15
+ :scheme => :header,
16
+ :http_method => :post,
17
+ :request_token_url => config["request_token_url"],
18
+ :access_token_url => config["access_token_url"],
19
+ :authorize_url => config["authorization_url"],
20
+ }
21
+
22
+ @consumer = OAuth::Consumer.new @consumer_key, @consumer_secret, @oauth_conf
23
+
24
+ @access_token = nil
25
+ @request_token = nil
26
+
27
+ if key and secret
28
+ if is_request then
29
+ @request_token = OAuth::RequestToken.new(@consumer, key, secret)
30
+ else
31
+ @access_token = OAuth::AccessToken.new(@consumer, key, secret)
32
+ end
33
+ end
34
+ end
35
+
36
+ attr_reader :config
37
+
38
+ def Authenticator.load_config(path)
39
+ open(path) do |f|
40
+ return JSON.parse(f.read())
41
+ end
42
+ end
43
+
44
+ def get_request_token(*args)
45
+ @request_token = @consumer.get_request_token(*args)
46
+ return @request_token.authorize_url
47
+ end
48
+
49
+ def get_access_token
50
+ if @access_token == nil
51
+ @access_token = @request_token.get_access_token
52
+ end
53
+
54
+ return @access_token
55
+ end
56
+
57
+ def authorized?
58
+ !!@access_token
59
+ end
60
+
61
+ def token
62
+ return (@access_token || @request_token).token
63
+ end
64
+
65
+ def secret
66
+ return (@access_token || @request_token).secret
67
+ end
68
+
69
+ def sign(request, request_options = {})
70
+ return @consumer.sign!(request, @access_token || @request_token, request_options)
71
+ end
72
+
73
+ def clone(host)
74
+ return Authenticator.new(@config, token(), secret(), !authorized?, host)
75
+ end
76
+ end
77
+
78
+ # maybe subclass or monkey patch trusted into the oauth stuff
79
+
80
+
81
+ API_VERSION = 0
82
+
83
+
84
+ class DropboxError < RuntimeError
85
+ end
86
+
87
+ class DropboxClient
88
+
89
+ def initialize(api_host, content_host, port, auth)
90
+ @api_host = api_host
91
+ @content_host = content_host
92
+ @port = port.to_i
93
+ @auth = auth
94
+ @token = auth.get_access_token
95
+ end
96
+
97
+ def parse_response(response, callback=nil)
98
+ if response.kind_of?(Net::HTTPServerError)
99
+ raise DropboxError.new("Invalid response #{response}\n#{response.body}")
100
+ elsif not response.kind_of?(Net::HTTPSuccess)
101
+ return response
102
+ end
103
+
104
+ if callback
105
+ return response.body
106
+ else
107
+ begin
108
+ return JSON.parse(response.body)
109
+ rescue JSON::ParserError
110
+ return response.body
111
+ end
112
+ end
113
+ end
114
+
115
+
116
+ def account_info(status_in_response=false, callback=nil)
117
+ response = @token.get build_url(@api_host, @port, "/account/info")
118
+ return parse_response(response, callback)
119
+ end
120
+
121
+ def put_file(root, to_path, name, file_obj)
122
+ path = "/files/#{root}#{to_path}"
123
+ oauth_params = {"file" => name}
124
+ auth = @auth.clone(@content_host)
125
+
126
+ url = URI.parse(build_url(@content_host, @port, path))
127
+
128
+ oauth_fake_req = Net::HTTP::Post.new(url.path)
129
+ oauth_fake_req.set_form_data({ "file" => name })
130
+ auth.sign(oauth_fake_req)
131
+
132
+ oauth_sig = oauth_fake_req.to_hash['authorization']
133
+
134
+ req = Net::HTTP::Post::Multipart.new(url.path, {
135
+ "file" => UploadIO.new(file_obj, "application/octet-stream", name),
136
+ })
137
+ req['authorization'] = oauth_sig.join(", ")
138
+
139
+ res = Net::HTTP.start(url.host, url.port) do |http|
140
+ return parse_response(http.request(req))
141
+ end
142
+ end
143
+
144
+ def get_file(root, from_path)
145
+ path = "/files/#{root}#{from_path}"
146
+ response = @token.get(build_url(@content_host, @port, path))
147
+ return parse_response(response, callback=true)
148
+ end
149
+
150
+ def file_copy(root, from_path, to_path, callback=nil)
151
+ params = {
152
+ "root" => root,
153
+ "from_path" => from_path,
154
+ "to_path" => to_path,
155
+ "callback" => callback
156
+ }
157
+ response = @token.post(build_url(@api_host, @port, "/fileops/copy"), params)
158
+ return parse_response(response, callback)
159
+ end
160
+
161
+ def file_create_folder(root, path, callback=nil)
162
+ params = {
163
+ "root" => root,
164
+ "path" => path,
165
+ "callback" => callback
166
+ }
167
+ response = @token.post(build_url(@api_host, @port, "/fileops/create_folder"), params)
168
+
169
+ return parse_response(response, callback)
170
+ end
171
+
172
+ def file_delete(root, path, callback=nil)
173
+ params = {
174
+ "root" => root,
175
+ "path" => path,
176
+ "callback" => callback
177
+ }
178
+ response = @token.post(build_url(@api_host, @port, "/fileops/delete"), params)
179
+ return parse_response(response, callback)
180
+ end
181
+
182
+ def file_move(root, from_path, to_path, callback=nil)
183
+ params = {
184
+ "root" => root,
185
+ "from_path" => from_path,
186
+ "to_path" => to_path,
187
+ "callback" => callback
188
+ }
189
+ response = @token.post(build_url(@api_host, @port, "/fileops/move"), params)
190
+ return parse_response(response, callback)
191
+ end
192
+
193
+ def metadata(root, path, file_limit=10000, hash=nil, list=true, status_in_response=false, callback=nil)
194
+ params = {
195
+ "file_limit" => file_limit.to_s,
196
+ "list" => list ? "true" : "false",
197
+ "status_in_response" => status_in_response ? "true" : "false"
198
+ }
199
+ params["hash"] = hash if hash
200
+ params["callback"] = callback if callback
201
+
202
+ response = @token.get build_url(@api_host, @port, "/metadata/#{root}#{path}", params=params)
203
+ return parse_response(response, callback)
204
+ end
205
+
206
+ def links(root, path)
207
+ full_path = "/links/#{root}#{path}"
208
+ return build_url(@api_host, @port, full_path)
209
+ end
210
+
211
+ def build_url(host, port, url, params=nil)
212
+ port = port == 80 ? nil : port
213
+ versioned_url = "/#{API_VERSION}#{url}"
214
+
215
+ target = URI::Generic.new("http", nil, host, port, nil, versioned_url, nil, nil, nil)
216
+
217
+ if params
218
+ target.query = params.collect {|k,v| URI.escape(k) + "=" + URI.escape(v) }.join("&")
219
+ end
220
+
221
+ return target.to_s
222
+ end
223
+
224
+
225
+ end
226
+
@@ -0,0 +1,9 @@
1
+ F LICENSE eee4b82f8ec32762095d12613d633f24ac242ebe
2
+ F README da39a3ee5e6b4b0d3255bfef95601890afd80709
3
+ F Rakefile 30ac5c2002b9bccb9064e956fefa88f687e6d719
4
+ F config/testing.json.example 4f833212f80566278dfe78c216f05f16db92ce4d
5
+ F example/test_pingback.rb 082e0b515333e3c495e1b3cbce179a4e7c69bd6b
6
+ F lib/dropbox.rb d7d8960fb725f046b83f17fe54f64f603fbb2cf6
7
+ F test/authenticator_test.rb b476e8fbbaa73cf6d1db227c549e43860e85f60b
8
+ F test/client_test.rb aa34080cc5dcb369d39f0fa4c602be89100a535b
9
+ F test/util.rb 0d49786886f9aee3b2f60ba1f56e7f861b3480b4
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'lib/dropbox'
3
+ require 'test/unit'
4
+ require 'shoulda'
5
+ require './test/util'
6
+
7
+ CONF = Authenticator.load_config("config/testing.json")
8
+
9
+ class AuthenticatorTest < Test::Unit::TestCase
10
+ context "Authenticator" do
11
+ should "load json config" do
12
+ assert CONF
13
+ auth = Authenticator.new(CONF)
14
+ end
15
+
16
+ should "get request token" do
17
+ auth = Authenticator.new(CONF)
18
+ authorize_url = auth.get_request_token
19
+ assert authorize_url
20
+ end
21
+
22
+ should "get access token" do
23
+ auth = Authenticator.new(CONF)
24
+ authorize_url = auth.get_request_token
25
+ assert authorize_url
26
+
27
+ login_and_authorize(authorize_url, CONF)
28
+
29
+ access_token = auth.get_access_token
30
+ assert access_token
31
+
32
+ assert access_token.token
33
+ assert access_token.secret
34
+
35
+ CONF['access_token_key'] = access_token.token
36
+ CONF['access_token_secret'] = access_token.secret
37
+
38
+ response = access_token.get "http://" + CONF['server'] + "/0/oauth/echo"
39
+ assert response
40
+ assert response.code == "200"
41
+ end
42
+
43
+ should "reuse an existing token" do
44
+ auth = Authenticator.new(CONF, CONF['access_token_key'], CONF['access_token_secret'])
45
+ access_token = auth.get_access_token
46
+
47
+ response = access_token.get "http://" + CONF['server'] + "/0/oauth/echo"
48
+ assert response
49
+ assert response.code == "200"
50
+ end
51
+ end
52
+ end
53
+
@@ -0,0 +1,100 @@
1
+ require 'rubygems'
2
+ require 'lib/dropbox'
3
+ require 'test/unit'
4
+ require 'shoulda'
5
+ require './test/util'
6
+ require 'pp'
7
+
8
+ CONF = Authenticator.load_config("config/testing.json") unless defined?(CONF)
9
+ AUTH = Authenticator.new(CONF) unless defined?(AUTH)
10
+ login_and_authorize(AUTH.get_request_token, CONF)
11
+ ACCESS_TOKEN = AUTH.get_access_token
12
+
13
+
14
+ class DropboxClientTest < Test::Unit::TestCase
15
+
16
+ context "DropboxClient" do
17
+ setup do
18
+ assert ACCESS_TOKEN
19
+ assert AUTH
20
+ assert CONF
21
+
22
+ begin
23
+ client = DropboxClient.new(CONF['server'], CONF['content_server'], CONF['port'], AUTH)
24
+ client.file_delete(CONF['root'], "/tests")
25
+ rescue
26
+ # ignored
27
+ end
28
+ end
29
+
30
+ should "be able to access account info" do
31
+ client = DropboxClient.new(CONF['server'], CONF['content_server'], CONF['port'], AUTH)
32
+ info = client.account_info
33
+ assert info
34
+ assert info["country"]
35
+ assert info["uid"]
36
+ end
37
+
38
+ should "build full urls" do
39
+ client = DropboxClient.new(CONF['server'], CONF['content_server'], CONF['port'], AUTH)
40
+ url = client.build_url(CONF['server'], CONF['port'], "/account/info")
41
+ assert_equal url, "http://" + CONF['server'] + "/0/account/info"
42
+
43
+ url = client.build_url(CONF['server'], CONF['port'], "/account/info")
44
+ assert_equal url, "http://" + CONF['server'] + "/0/account/info"
45
+
46
+ url = client.build_url(CONF['server'], CONF['port'], "/account/info", params={"one" => "1", "two" => "2"})
47
+ assert_equal url, "http://" + CONF['server'] + "/0/account/info?two=2&one=1"
48
+
49
+ url = client.build_url(CONF['server'], CONF['port'], "/account/info", params={"one" => "1", "two" => "2"})
50
+ assert_equal url, "http://" + CONF['server'] + "/0/account/info?two=2&one=1"
51
+ end
52
+
53
+
54
+ should "create links" do
55
+ client = DropboxClient.new(CONF['server'], CONF['content_server'] ,CONF['port'], AUTH)
56
+ assert_equal "http://" + CONF['server'] + "/0/links/" + CONF['root'] + "/to/the/file", client.links(CONF['root'], "/to/the/file")
57
+ end
58
+
59
+ should "get metadata" do
60
+ client = DropboxClient.new(CONF['server'], CONF['content_server'] ,CONF['port'], AUTH)
61
+ results = client.metadata(CONF['root'], "/")
62
+ assert results
63
+ assert results["hash"]
64
+ end
65
+
66
+ should "be able to perform file ops on folders" do
67
+ client = DropboxClient.new(CONF['server'], CONF['content_server'], CONF['port'], AUTH)
68
+ results = client.file_create_folder(CONF['root'], "/tests/that")
69
+ assert results
70
+ assert results["bytes"]
71
+
72
+ assert client.file_copy(CONF['root'], "/tests/that", "/tests/those")
73
+ assert client.metadata(CONF['root'], "/tests/those")
74
+ assert client.file_delete(CONF['root'], "/tests/those")
75
+
76
+ results = client.file_move(CONF['root'], "/tests/that", "/tests/those")
77
+ assert results
78
+
79
+ assert client.metadata(CONF['root'], "/tests/those")
80
+ assert client.file_delete(CONF['root'], "/tests/those")
81
+
82
+ end
83
+
84
+ should "be able to get files" do
85
+ client = DropboxClient.new(CONF['server'], CONF['content_server'], CONF['port'], AUTH)
86
+ results = client.get_file(CONF['root'], "/client_tests.py")
87
+ assert results
88
+ end
89
+
90
+ should "be able to put files" do
91
+ client = DropboxClient.new(CONF['server'], CONF['content_server'], CONF['port'], AUTH)
92
+ results = client.put_file(CONF['root'], "/", "LICENSE", open("LICENSE"))
93
+ assert results
94
+ assert client.file_delete(CONF['root'], "/LICENSE")
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+