dropbox-api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,82 @@
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 = mock :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 = mock :code => 401, :body => '{ "a":1}'
20
+ lambda do
21
+ @connection.request { response }
22
+ end.should raise_error(Dropbox::API::Error::Unauthorized)
23
+ end
24
+
25
+ it "raises a Dropbox::API::Error::Forbidden when the response is a 403" do
26
+ response = mock :code => 403, :body => '{ "a":1}'
27
+ lambda do
28
+ @connection.request { response }
29
+ end.should raise_error(Dropbox::API::Error::Forbidden)
30
+ end
31
+
32
+ it "raises a Dropbox::API::Error::NotFound when the response is a 404" do
33
+ response = mock :code => 404, :body => '{ "a":1}'
34
+ lambda do
35
+ @connection.request { response }
36
+ end.should raise_error(Dropbox::API::Error::NotFound)
37
+ end
38
+
39
+ it "raises a Dropbox::API::Error when the response is a 3xx" do
40
+ response = mock :code => 301, :body => '{ "a":1}'
41
+ lambda do
42
+ @connection.request { response }
43
+ end.should raise_error(Dropbox::API::Error::Redirect)
44
+ end
45
+
46
+ it "raises a Dropbox::API::Error when the response is a 5xx" do
47
+ response = mock :code => 500, :body => '{ "a":1}'
48
+ lambda do
49
+ @connection.request { response }
50
+ end.should raise_error(Dropbox::API::Error)
51
+ end
52
+
53
+ it "raises a Dropbox::API::Error when the response is a 400" do
54
+ response = mock :code => 400, :body => '{ "error": "bad request" }'
55
+ lambda do
56
+ @connection.request { response }
57
+ end.should raise_error(Dropbox::API::Error)
58
+ end
59
+
60
+ it "raises a Dropbox::API::Error when the response is a 406" do
61
+ response = mock :code => 406, :body => '{ "error": "bad request" }'
62
+ lambda do
63
+ @connection.request { response }
64
+ end.should raise_error(Dropbox::API::Error)
65
+ end
66
+
67
+ it "returns the raw response if :raw => true is provided" do
68
+ response = mock :code => 200, :body => '{ "something": "more" }'
69
+ response = @connection.request(:raw => true) { response }
70
+ response.should == '{ "something": "more" }'
71
+ end
72
+
73
+ end
74
+
75
+ describe "#consumer" do
76
+
77
+ it "returns an appropriate consumer object" do
78
+ @connection.consumer(:main).should be_a(::OAuth::Consumer)
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+
3
+ describe Dropbox::API::Dir do
4
+
5
+ before do
6
+ @client = Dropbox::Spec.instance
7
+ @dirname = "test/spec-dir-test-#{Time.now.to_i}"
8
+ @dir = @client.mkdir @dirname
9
+ end
10
+
11
+ after do
12
+ # @dir.delete
13
+ end
14
+
15
+ describe "#copy" do
16
+
17
+ it "copies the dir properly" do
18
+ new_dirname = @dirname + "-copied"
19
+ @dir.copy new_dirname
20
+ @dir.path.should == new_dirname
21
+ end
22
+
23
+ end
24
+
25
+ describe "#move" do
26
+
27
+ it "moves the dir properly" do
28
+ new_dirname = @dirname + "-copied"
29
+ @dir.move new_dirname
30
+ @dir.path.should == new_dirname
31
+ end
32
+
33
+ end
34
+
35
+ describe "#delete" do
36
+
37
+ it "deletes the dir properly" do
38
+ @dir.delete
39
+ @dir.is_deleted.should == true
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,114 @@
1
+ require "spec_helper"
2
+
3
+ describe Dropbox::API::File do
4
+
5
+ before do
6
+ @client = Dropbox::Spec.instance
7
+ @filename = "test/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 "#delete" do
36
+
37
+ it "deletes the file properly" do
38
+ @file.delete
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']
89
+
90
+ end
91
+
92
+ end
93
+
94
+ describe "#direct_url" do
95
+
96
+ it "returns an Url object" do
97
+
98
+ result = @file.direct_url
99
+ result.should be_an_instance_of(Dropbox::API::Object)
100
+ result.keys.sort.should == ['expires', 'url']
101
+
102
+ end
103
+
104
+ end
105
+
106
+ describe "#download" do
107
+
108
+ it "should download the file" do
109
+ @file.download.should == 'spec file'
110
+ end
111
+
112
+ end
113
+
114
+ 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 = "test/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 == 72
24
+ jpeg.width.should == 72
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,14 @@
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
+ module Dropbox
11
+ Spec = Hashie::Mash.new
12
+ end
13
+
14
+ Dir.glob("#{File.dirname(__FILE__)}/support/*.rb").each { |f| require f }
@@ -0,0 +1,13 @@
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
+
8
+ Dropbox::Spec.token = config['token']
9
+ Dropbox::Spec.secret = config['secret']
10
+
11
+ Dropbox::Spec.namespace = Time.now.to_i
12
+ Dropbox::Spec.instance = Dropbox::API::Client.new(:token => Dropbox::Spec.token,
13
+ :secret => Dropbox::Spec.secret)
@@ -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
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dropbox-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marcin Bunsch
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: yajl-ruby
16
+ requirement: &70118868905540 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70118868905540
25
+ - !ruby/object:Gem::Dependency
26
+ name: oauth
27
+ requirement: &70118868903940 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70118868903940
36
+ - !ruby/object:Gem::Dependency
37
+ name: hashie
38
+ requirement: &70118868948200 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70118868948200
47
+ description: To deliver a more Rubyesque experience when using the DropBox API.
48
+ email:
49
+ - marcin@futuresimple.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - Gemfile
57
+ - LICENSE
58
+ - README.markdown
59
+ - Rakefile
60
+ - dropbox-api.gemspec
61
+ - lib/dropbox-api.rb
62
+ - lib/dropbox-api/client.rb
63
+ - lib/dropbox-api/client/files.rb
64
+ - lib/dropbox-api/client/raw.rb
65
+ - lib/dropbox-api/connection.rb
66
+ - lib/dropbox-api/connection/requests.rb
67
+ - lib/dropbox-api/objects/dir.rb
68
+ - lib/dropbox-api/objects/file.rb
69
+ - lib/dropbox-api/objects/fileops.rb
70
+ - lib/dropbox-api/objects/object.rb
71
+ - lib/dropbox-api/tasks.rb
72
+ - lib/dropbox-api/util/config.rb
73
+ - lib/dropbox-api/util/error.rb
74
+ - lib/dropbox-api/util/oauth.rb
75
+ - lib/dropbox-api/util/util.rb
76
+ - lib/dropbox-api/version.rb
77
+ - spec/connection.sample.yml
78
+ - spec/fixtures/dropbox.jpg
79
+ - spec/lib/dropbox-api/client_spec.rb
80
+ - spec/lib/dropbox-api/connection_spec.rb
81
+ - spec/lib/dropbox-api/dir_spec.rb
82
+ - spec/lib/dropbox-api/file_spec.rb
83
+ - spec/lib/dropbox-api/oauth_spec.rb
84
+ - spec/lib/dropbox-api/thumbnail_spec.rb
85
+ - spec/spec_helper.rb
86
+ - spec/support/config.rb
87
+ - spec/support/jpeg.rb
88
+ homepage: ''
89
+ licenses: []
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ segments:
101
+ - 0
102
+ hash: 1145444997854151183
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ segments:
110
+ - 0
111
+ hash: 1145444997854151183
112
+ requirements: []
113
+ rubyforge_project: dropbox-api
114
+ rubygems_version: 1.8.10
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: A Ruby client for the DropBox REST API.
118
+ test_files:
119
+ - spec/connection.sample.yml
120
+ - spec/fixtures/dropbox.jpg
121
+ - spec/lib/dropbox-api/client_spec.rb
122
+ - spec/lib/dropbox-api/connection_spec.rb
123
+ - spec/lib/dropbox-api/dir_spec.rb
124
+ - spec/lib/dropbox-api/file_spec.rb
125
+ - spec/lib/dropbox-api/oauth_spec.rb
126
+ - spec/lib/dropbox-api/thumbnail_spec.rb
127
+ - spec/spec_helper.rb
128
+ - spec/support/config.rb
129
+ - spec/support/jpeg.rb