Skip to content
This repository was archived by the owner on May 4, 2018. It is now read-only.

Commit 702c0f9

Browse files
committed
Merge pull request #47 from bcoe/master
Added Support for Shares
2 parents 06c2515 + 43d724c commit 702c0f9

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

lib/dropbox/api.rb

+22
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,28 @@ def link(path, options={})
371371
end
372372
end
373373
memoize :link
374+
375+
# Creates and returns a shareable link to files or folders.
376+
#
377+
# The path is assumed to be relative to the configured mode's root.
378+
#
379+
# Options:
380+
#
381+
# +mode+:: Temporarily changes the API mode. See the MODES array.
382+
383+
def shares(path, options={})
384+
path = path.sub(/^\//, '')
385+
rest = Dropbox.check_path(path).split('/')
386+
387+
begin
388+
return JSON.parse( api_response(:post, 'shares', root(options), *rest).body ).symbolize_keys_recursively
389+
rescue UnsuccessfulResponseError => error
390+
return error.response['Location'] if error.response.kind_of?(Net::HTTPFound)
391+
#TODO shouldn't be using rescue blocks for normal program flow
392+
raise error
393+
end
394+
end
395+
memoize :shares
374396

375397
# Returns a +Struct+ containing metadata on a given file or folder. The path
376398
# is assumed to be relative to the configured mode's root.

spec/dropbox/api_spec.rb

+37-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ def should_receive_api_method_with_arguments(object, method, api_method, argumen
1111
object.should_receive(method).once do |url|
1212
front = url.split('?').first
1313
front.should eql("#{Dropbox::ALTERNATE_HOSTS[api_method] || Dropbox::HOST}/#{Dropbox::VERSION}/#{api_method}#{'/' + root if root}#{'/' + path if path}")
14-
1514
query_params = url_args(url)
1615
query_params.each { |key, val| val.should eql(arguments[key.to_sym]) }
1716
arguments.each { |key, _| query_params.should include(key.to_s) }
@@ -425,6 +424,43 @@ def response_acts_as(subclass)
425424
@session.link(path)
426425
end
427426
end
427+
428+
describe "#shares" do
429+
before :each do
430+
@response.stub!(:code).and_return(304)
431+
response_acts_as Net::HTTPFound
432+
@response.stub!(:[]).and_return("new location")
433+
end
434+
435+
it "should call the API method shares" do
436+
should_receive_api_method_with_arguments @token_mock, :post, 'shares', {}, @response, 'some/file', 'sandbox'
437+
@session.shares 'some/file'
438+
end
439+
440+
it "should strip a leading slash" do
441+
should_receive_api_method_with_arguments @token_mock, :post, 'shares', {}, @response, 'some/file', 'sandbox'
442+
@session.shares '/some/file'
443+
end
444+
445+
it "should rescue 304's and return the Location header" do
446+
should_receive_api_method_with_arguments @token_mock, :post, 'shares', {}, @response, 'some/file', 'sandbox'
447+
lambda { @session.shares('some/file').should eql("new location") }.should_not raise_error
448+
end
449+
450+
it "should re-raise other errors unmodified" do
451+
response_acts_as nil
452+
@token_mock.stub!(:post).and_return(@response)
453+
lambda { @session.shares('a') }.should raise_error(Dropbox::UnsuccessfulResponseError)
454+
end
455+
456+
it "should check the path" do
457+
path = "source/path"
458+
Dropbox.should_receive(:check_path).once.with(path).and_return(path)
459+
@token_mock.stub!(:post).and_return(@response)
460+
461+
@session.shares(path)
462+
end
463+
end
428464

429465
describe "#metadata" do
430466
before :each do

0 commit comments

Comments
 (0)