This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Merge pull request #47 from bcoe/master

Added Support for Shares
  • Loading branch information...
RISCfuture committed May 28, 2012
2 parents 06c2515 + 43d724c commit 702c0f99a25bb319b4a191f3dac2dbbd60427f9d
Showing with 59 additions and 1 deletion.
  1. +22 −0 lib/dropbox/api.rb
  2. +37 −1 spec/dropbox/api_spec.rb
@@ -371,6 +371,28 @@ def link(path, options={})
end
end
memoize :link
# Creates and returns a shareable link to files or folders.
#
# The path is assumed to be relative to the configured mode's root.
#
# Options:
#
# +mode+:: Temporarily changes the API mode. See the MODES array.
def shares(path, options={})
path = path.sub(/^\//, '')
rest = Dropbox.check_path(path).split('/')
begin
return JSON.parse( api_response(:post, 'shares', root(options), *rest).body ).symbolize_keys_recursively
rescue UnsuccessfulResponseError => error
return error.response['Location'] if error.response.kind_of?(Net::HTTPFound)
#TODO shouldn't be using rescue blocks for normal program flow
raise error
end
end
memoize :shares
# Returns a +Struct+ containing metadata on a given file or folder. The path
# is assumed to be relative to the configured mode's root.
@@ -11,7 +11,6 @@ def should_receive_api_method_with_arguments(object, method, api_method, argumen
object.should_receive(method).once do |url|
front = url.split('?').first
front.should eql("#{Dropbox::ALTERNATE_HOSTS[api_method] || Dropbox::HOST}/#{Dropbox::VERSION}/#{api_method}#{'/' + root if root}#{'/' + path if path}")
query_params = url_args(url)
query_params.each { |key, val| val.should eql(arguments[key.to_sym]) }
arguments.each { |key, _| query_params.should include(key.to_s) }
@@ -425,6 +424,43 @@ def response_acts_as(subclass)
@session.link(path)
end
end
describe "#shares" do
before :each do
@response.stub!(:code).and_return(304)
response_acts_as Net::HTTPFound
@response.stub!(:[]).and_return("new location")
end
it "should call the API method shares" do
should_receive_api_method_with_arguments @token_mock, :post, 'shares', {}, @response, 'some/file', 'sandbox'
@session.shares 'some/file'
end
it "should strip a leading slash" do
should_receive_api_method_with_arguments @token_mock, :post, 'shares', {}, @response, 'some/file', 'sandbox'
@session.shares '/some/file'
end
it "should rescue 304's and return the Location header" do
should_receive_api_method_with_arguments @token_mock, :post, 'shares', {}, @response, 'some/file', 'sandbox'
lambda { @session.shares('some/file').should eql("new location") }.should_not raise_error
end
it "should re-raise other errors unmodified" do
response_acts_as nil
@token_mock.stub!(:post).and_return(@response)
lambda { @session.shares('a') }.should raise_error(Dropbox::UnsuccessfulResponseError)
end
it "should check the path" do
path = "source/path"
Dropbox.should_receive(:check_path).once.with(path).and_return(path)
@token_mock.stub!(:post).and_return(@response)
@session.shares(path)
end
end
describe "#metadata" do
before :each do

0 comments on commit 702c0f9

Please sign in to comment.