dropbox-api-alt 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/.rspec +1 -0
- data/CHANGELOG +8 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.markdown +369 -0
- data/Rakefile +4 -0
- data/dropbox-api-alt.gemspec +34 -0
- data/lib/dropbox-api-alt.rb +1 -0
- data/lib/dropbox-api.rb +22 -0
- data/lib/dropbox-api/client.rb +72 -0
- data/lib/dropbox-api/client/files.rb +77 -0
- data/lib/dropbox-api/client/raw.rb +51 -0
- data/lib/dropbox-api/connection.rb +34 -0
- data/lib/dropbox-api/connection/requests.rb +87 -0
- data/lib/dropbox-api/objects/delta.rb +11 -0
- data/lib/dropbox-api/objects/dir.rb +29 -0
- data/lib/dropbox-api/objects/file.rb +39 -0
- data/lib/dropbox-api/objects/fileops.rb +33 -0
- data/lib/dropbox-api/objects/object.rb +39 -0
- data/lib/dropbox-api/tasks.rb +48 -0
- data/lib/dropbox-api/util/config.rb +27 -0
- data/lib/dropbox-api/util/error.rb +20 -0
- data/lib/dropbox-api/util/oauth.rb +29 -0
- data/lib/dropbox-api/util/util.rb +27 -0
- data/lib/dropbox-api/version.rb +5 -0
- data/spec/connection.sample.yml +5 -0
- data/spec/fixtures/dropbox.jpg +0 -0
- data/spec/lib/dropbox-api/client_spec.rb +259 -0
- data/spec/lib/dropbox-api/connection_spec.rb +137 -0
- data/spec/lib/dropbox-api/dir_spec.rb +59 -0
- data/spec/lib/dropbox-api/file_spec.rb +126 -0
- data/spec/lib/dropbox-api/oauth_spec.rb +17 -0
- data/spec/lib/dropbox-api/thumbnail_spec.rb +29 -0
- data/spec/spec_helper.rb +27 -0
- data/spec/support/config.rb +15 -0
- data/spec/support/jpeg.rb +39 -0
- metadata +193 -0
@@ -0,0 +1,137 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Dropbox::API::Connection do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@connection = Dropbox::API::Connection.new(:token => Dropbox::Spec.token,
|
7
|
+
:secret => Dropbox::Spec.secret)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#request" do
|
11
|
+
|
12
|
+
it "returns a parsed response when the response is a 200" do
|
13
|
+
response = double :code => 200, :body => '{ "a":1}'
|
14
|
+
response = @connection.request { response }
|
15
|
+
response.should be_an_instance_of(Hash)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "raises a Dropbox::API::Error::Unauthorized when the response is a 401" do
|
19
|
+
response = double :code => 401, :body => '{ "a":1}'
|
20
|
+
lambda do
|
21
|
+
@connection.request { response }
|
22
|
+
end.should raise_error(Dropbox::API::Error::Unauthorized, '401 - Bad or expired token')
|
23
|
+
end
|
24
|
+
|
25
|
+
it "raises a Dropbox::API::Error::Forbidden when the response is a 403" do
|
26
|
+
response = double :code => 403, :body => '{ "a":1}'
|
27
|
+
lambda do
|
28
|
+
@connection.request { response }
|
29
|
+
end.should raise_error(Dropbox::API::Error::Forbidden, '403 - Bad OAuth request')
|
30
|
+
end
|
31
|
+
|
32
|
+
it "raises a Dropbox::API::Error::NotFound when the response is a 404" do
|
33
|
+
response = double :code => 404, :body => '{ "a":1}'
|
34
|
+
lambda do
|
35
|
+
@connection.request { response }
|
36
|
+
end.should raise_error(Dropbox::API::Error::NotFound, '404 - Not found')
|
37
|
+
end
|
38
|
+
|
39
|
+
it "raises a Dropbox::API::Error::WrongMethod when the response is a 405" do
|
40
|
+
response = double :code => 405, :body => '{ "error": "The requested method GET is not allowed for the URL /foo/." }'
|
41
|
+
lambda do
|
42
|
+
@connection.request { response }
|
43
|
+
end.should raise_error(Dropbox::API::Error::WrongMethod, '405 - Request method not expected - The requested method GET is not allowed for the URL /foo/.')
|
44
|
+
end
|
45
|
+
|
46
|
+
it "raises a Dropbox::API::Error when the response is a 3xx" do
|
47
|
+
response = double :code => 301, :body => '{ "a":1}'
|
48
|
+
lambda do
|
49
|
+
@connection.request { response }
|
50
|
+
end.should raise_error(Dropbox::API::Error::Redirect, '301 - Redirect Error')
|
51
|
+
end
|
52
|
+
|
53
|
+
it "raises a Dropbox::API::Error when the response is a 5xx" do
|
54
|
+
response = double :code => 500, :body => '{ "a":1}'
|
55
|
+
lambda do
|
56
|
+
@connection.request { response }
|
57
|
+
end.should raise_error(Dropbox::API::Error, '500 - Server error. Check http://status.dropbox.com/')
|
58
|
+
end
|
59
|
+
|
60
|
+
it "raises a Dropbox::API::Error when the response is a 400" do
|
61
|
+
response = double :code => 400, :body => '{ "error": "bad request foo" }'
|
62
|
+
lambda do
|
63
|
+
@connection.request { response }
|
64
|
+
end.should raise_error(Dropbox::API::Error::BadInput, '400 - Bad input parameter - bad request foo')
|
65
|
+
end
|
66
|
+
|
67
|
+
it "raises a Dropbox::API::Error when the response is a 406" do
|
68
|
+
response = double :code => 406, :body => '{ "error": "bad request bar" }'
|
69
|
+
lambda do
|
70
|
+
@connection.request { response }
|
71
|
+
end.should raise_error(Dropbox::API::Error, '406 - bad request bar')
|
72
|
+
end
|
73
|
+
|
74
|
+
it "raises a Dropbox::API::Error when the response is a 406" do
|
75
|
+
response = double :code => 429, :body => '{ "error": "rate limited" }'
|
76
|
+
lambda do
|
77
|
+
@connection.request { response }
|
78
|
+
end.should raise_error(Dropbox::API::Error::RateLimit, '429 - Rate Limiting in affect')
|
79
|
+
end
|
80
|
+
|
81
|
+
it "raises a Dropbox::API::Error when the response is a 503" do
|
82
|
+
response = double :code => 503, :body => '{ "error": "rate limited" }', :headers => '{ "Retry-After": "50" }'
|
83
|
+
lambda do
|
84
|
+
@connection.request { response }
|
85
|
+
end.should raise_error(Dropbox::API::Error, '503 - rate limited. Retry after: 50')
|
86
|
+
end
|
87
|
+
|
88
|
+
it "raises a Dropbox::API::Error when the response is a 503 Service Unavailable" do
|
89
|
+
message = 'we are down'
|
90
|
+
response = Net::HTTPServiceUnavailable.new('1.1', 503, message)
|
91
|
+
lambda do
|
92
|
+
@connection.request { response }
|
93
|
+
end.should raise_error(Dropbox::API::Error, "503 - #{message}")
|
94
|
+
end
|
95
|
+
|
96
|
+
it "raises a Dropbox::API::Error::StorageQuota when the response is a 507" do
|
97
|
+
response = double :code => 507, :body => '{ "error": "quote limit" }'
|
98
|
+
lambda do
|
99
|
+
@connection.request { response }
|
100
|
+
end.should raise_error(Dropbox::API::Error, '507 - Dropbox storage quota exceeded.')
|
101
|
+
end
|
102
|
+
|
103
|
+
|
104
|
+
it "returns the raw response if :raw => true is provided" do
|
105
|
+
response = double :code => 200, :body => '{ "something": "more" }'
|
106
|
+
response = @connection.request(:raw => true) { response }
|
107
|
+
response.should == '{ "something": "more" }'
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "#consumer" do
|
113
|
+
|
114
|
+
it "returns an appropriate consumer object" do
|
115
|
+
@connection.consumer(:main).should be_a(::OAuth::Consumer)
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
describe "errors" do
|
121
|
+
|
122
|
+
it "recovers error with rescue statement modifier" do
|
123
|
+
expect { raise Dropbox::API::Error rescue nil }.to_not raise_error
|
124
|
+
end
|
125
|
+
|
126
|
+
it "recovers any kind of errors with the generic error" do
|
127
|
+
expect do
|
128
|
+
begin
|
129
|
+
raise Dropbox::API::Error::Forbidden
|
130
|
+
rescue Dropbox::API::Error
|
131
|
+
end
|
132
|
+
end.to_not raise_error
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Dropbox::API::Dir do
|
4
|
+
|
5
|
+
after do
|
6
|
+
# @dir.delete
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#hash" do
|
10
|
+
|
11
|
+
let(:hash) { "dasdsaf32da" }
|
12
|
+
|
13
|
+
it "returns the hash from Dropbox" do
|
14
|
+
dir = Dropbox::API::Dir.new("hash" => hash)
|
15
|
+
dir.hash.should == hash
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
context "operations" do
|
21
|
+
|
22
|
+
before do
|
23
|
+
@client = Dropbox::Spec.instance
|
24
|
+
@dirname = "#{Dropbox::Spec.test_dir}/spec-dir-test-#{Time.now.to_i}"
|
25
|
+
@dir = @client.mkdir @dirname
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#copy" do
|
29
|
+
|
30
|
+
it "copies the dir properly" do
|
31
|
+
new_dirname = @dirname + "-copied"
|
32
|
+
@dir.copy new_dirname
|
33
|
+
@dir.path.should == new_dirname
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#move" do
|
39
|
+
|
40
|
+
it "moves the dir properly" do
|
41
|
+
new_dirname = @dirname + "-copied"
|
42
|
+
@dir.move new_dirname
|
43
|
+
@dir.path.should == new_dirname
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#destroy" do
|
49
|
+
|
50
|
+
it "destroys the dir properly" do
|
51
|
+
@dir.destroy
|
52
|
+
@dir.is_deleted.should == true
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Dropbox::API::File do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@client = Dropbox::Spec.instance
|
7
|
+
@filename = "#{Dropbox::Spec.test_dir}/spec-test-#{Time.now.to_i}.txt"
|
8
|
+
@file = @client.upload @filename, "spec file"
|
9
|
+
end
|
10
|
+
|
11
|
+
after do
|
12
|
+
# @file.delete
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#copy" do
|
16
|
+
|
17
|
+
it "copies the file properly" do
|
18
|
+
new_filename = @filename + ".copied"
|
19
|
+
@file.copy new_filename
|
20
|
+
@file.path.should == new_filename
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#move" do
|
26
|
+
|
27
|
+
it "moves the file properly" do
|
28
|
+
new_filename = @filename + ".copied"
|
29
|
+
@file.move new_filename
|
30
|
+
@file.path.should == new_filename
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#destroy" do
|
36
|
+
|
37
|
+
it "destroys the file properly" do
|
38
|
+
@file.destroy
|
39
|
+
@file.is_deleted.should == true
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#revisions" do
|
45
|
+
|
46
|
+
it "retrieves all revisions as an Array of File objects" do
|
47
|
+
@client.upload @file.path, "Updated content"
|
48
|
+
|
49
|
+
revisions = @file.revisions
|
50
|
+
revisions.size.should == 2
|
51
|
+
revisions.collect { |f| f.class }.should == [Dropbox::API::File, Dropbox::API::File]
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#restore" do
|
57
|
+
|
58
|
+
it "restores the file to a specific revision" do
|
59
|
+
old_rev = @file.rev
|
60
|
+
|
61
|
+
@client.upload @file.path, "Updated content"
|
62
|
+
|
63
|
+
file = @filename.split('/').last
|
64
|
+
|
65
|
+
found = @client.find(@file.path)
|
66
|
+
|
67
|
+
found.rev.should_not == old_rev
|
68
|
+
|
69
|
+
newer_rev = found.rev
|
70
|
+
|
71
|
+
@file.restore(old_rev)
|
72
|
+
|
73
|
+
found = @client.find(@file.path)
|
74
|
+
|
75
|
+
found.rev.should_not == old_rev
|
76
|
+
found.rev.should_not == newer_rev
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "#share_url" do
|
83
|
+
|
84
|
+
it "returns an Url object" do
|
85
|
+
|
86
|
+
result = @file.share_url
|
87
|
+
result.should be_an_instance_of(Dropbox::API::Object)
|
88
|
+
result.keys.sort.should == ['expires', 'url', 'visibility']
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "#copy_ref" do
|
95
|
+
|
96
|
+
it "returns a copy_ref object" do
|
97
|
+
|
98
|
+
result = @file.copy_ref
|
99
|
+
result.should be_an_instance_of(Dropbox::API::Object)
|
100
|
+
result.keys.sort.should == ['copy_ref', 'expires']
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "#direct_url" do
|
107
|
+
|
108
|
+
it "returns an Url object" do
|
109
|
+
|
110
|
+
result = @file.direct_url
|
111
|
+
result.should be_an_instance_of(Dropbox::API::Object)
|
112
|
+
result.keys.sort.should == ['expires', 'url']
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "#download" do
|
119
|
+
|
120
|
+
it "should download the file" do
|
121
|
+
@file.download.should == 'spec file'
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Dropbox::API::OAuth do
|
4
|
+
|
5
|
+
describe ".consumer" do
|
6
|
+
|
7
|
+
it "raises an error if config options are not provided" do
|
8
|
+
Dropbox::API::Config.stub(:app_key).and_return(nil)
|
9
|
+
lambda {
|
10
|
+
Dropbox::API::OAuth.consumer :main
|
11
|
+
}.should raise_error(Dropbox::API::Error::Config)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "fileutils"
|
3
|
+
|
4
|
+
describe Dropbox::API::File do
|
5
|
+
|
6
|
+
before do
|
7
|
+
@io = StringIO.new
|
8
|
+
@client = Dropbox::Spec.instance
|
9
|
+
@filename = "#{Dropbox::Spec.test_dir}/spec-test-#{Time.now.to_i}.jpg"
|
10
|
+
jpeg = File.read("spec/fixtures/dropbox.jpg")
|
11
|
+
@file = @client.upload @filename, jpeg
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#thumbnail" do
|
15
|
+
|
16
|
+
it "downloads a thumbnail" do
|
17
|
+
result = @file.thumbnail
|
18
|
+
|
19
|
+
@io << result
|
20
|
+
@io.rewind
|
21
|
+
|
22
|
+
jpeg = JPEG.new(@io)
|
23
|
+
jpeg.height.should == 64
|
24
|
+
jpeg.width.should == 64
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
$:.push File.expand_path("../../lib", __FILE__)
|
2
|
+
|
3
|
+
require 'simplecov'
|
4
|
+
SimpleCov.start do
|
5
|
+
add_filter 'spec'
|
6
|
+
end
|
7
|
+
require 'dropbox-api'
|
8
|
+
require 'rspec'
|
9
|
+
|
10
|
+
# If you wand to change the json, you can do it here
|
11
|
+
# I still believe yajl is the best :) - marcinbunsch
|
12
|
+
MultiJson.engine= :yajl
|
13
|
+
|
14
|
+
module Dropbox
|
15
|
+
Spec = Hashie::Mash.new
|
16
|
+
end
|
17
|
+
|
18
|
+
Dir.glob("#{File.dirname(__FILE__)}/support/*.rb").each { |f| require f }
|
19
|
+
|
20
|
+
# Clean up after specs, remove test-directory
|
21
|
+
RSpec.configure do |config|
|
22
|
+
config.after(:all) do
|
23
|
+
test_dir = Dropbox::Spec.instance.find(Dropbox::Spec.test_dir) rescue nil
|
24
|
+
test_dir.destroy if test_dir and !test_dir.is_deleted?
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "yaml"
|
2
|
+
|
3
|
+
config = YAML.load_file "spec/connection.yml"
|
4
|
+
|
5
|
+
Dropbox::API::Config.app_key = config['app_key']
|
6
|
+
Dropbox::API::Config.app_secret = config['app_secret']
|
7
|
+
Dropbox::API::Config.mode = config['mode']
|
8
|
+
|
9
|
+
Dropbox::Spec.token = config['token']
|
10
|
+
Dropbox::Spec.secret = config['secret']
|
11
|
+
|
12
|
+
Dropbox::Spec.namespace = Time.now.to_i
|
13
|
+
Dropbox::Spec.instance = Dropbox::API::Client.new(:token => Dropbox::Spec.token,
|
14
|
+
:secret => Dropbox::Spec.secret)
|
15
|
+
Dropbox::Spec.test_dir = "test-#{Time.now.to_i}"
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class JPEG
|
2
|
+
attr_reader :width, :height, :bits
|
3
|
+
|
4
|
+
def initialize(file)
|
5
|
+
examine(file)
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
def examine(io)
|
10
|
+
raise 'malformed JPEG' unless io.getbyte == 0xFF && io.getbyte == 0xD8 # SOI
|
11
|
+
|
12
|
+
class << io
|
13
|
+
def readint; (getbyte << 8) + getbyte; end
|
14
|
+
def readframe; read(readint - 2); end
|
15
|
+
def readsof; [readint, getbyte, readint, readint, getbyte]; end
|
16
|
+
def next
|
17
|
+
c = getbyte while c != 0xFF
|
18
|
+
c = getbyte while c == 0xFF
|
19
|
+
c
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
while marker = io.next
|
24
|
+
case marker
|
25
|
+
when 0xC0..0xC3, 0xC5..0xC7, 0xC9..0xCB, 0xCD..0xCF # SOF markers
|
26
|
+
length, @bits, @height, @width, components = io.readsof
|
27
|
+
raise 'malformed JPEG' unless length == 8 + components * 3
|
28
|
+
when 0xD9, 0xDA
|
29
|
+
break # EOI, SOS
|
30
|
+
when 0xFE
|
31
|
+
@comment = io.readframe # COM
|
32
|
+
when 0xE1
|
33
|
+
io.readframe # APP1, contains EXIF tag
|
34
|
+
else
|
35
|
+
io.readframe # ignore frame
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|