Skip to content

Instantly share code, notes, and snippets.

@rattlion
Created July 12, 2012 17:52
Show Gist options
  • Save rattlion/3099641 to your computer and use it in GitHub Desktop.
Save rattlion/3099641 to your computer and use it in GitHub Desktop.
# Create a new drop. Multiple bookmarks can be created at once by
# puts 'test'
# passing an array of bookmark options parameters.
#
# Requires authentication.
#
# @param [Symbol] kind type of drop (can be +:bookmark+, +:bookmarks+ or +:upload+)
# @overload self.create(:bookmark, opts = {})
# @param [Hash] opts options paramaters
# @option opts [String] :name Name of bookmark (only required for +:bookmark+ kind)
# @option opts [String] :redirect_url Redirect URL (only required for +:bookmark+ kind)
# @overload self.create(:bookmarks, bookmarks)
# @param [Array] bookmarks array of bookmark option parameters (containing +:name+ and +:redirect_url+)
# @overload self.create(:upload, opts = {})
# @param [Hash] opts options paramaters
# @option opts [String] :file Path to file (only required for +:upload+ kind)
# @option opts [Boolean] :private override the account default privacy setting
# @return [CloudApp::Drop]
def self.create(kind, opts = {})
case kind
when :bookmark
res = post "/items", {:body => {:item => opts}, :digest_auth => @@auth}
when :bookmarks
res = post "/items", {:body => {:items => opts}, :digest_auth => @@auth}
when :upload
r = get "/items/new", {:query => ({:item => {:private => opts[:private]}} if opts.has_key?(:private)), :digest_auth => @@auth}
return bad_response(r) unless r.ok?
res = post r['url'], Multipart.new(r['params'].merge!(:file => File.new(opts[:file]))).payload.merge!(:digest_auth => @@auth)
else
# TODO raise an error
return false
end
res.ok? ? (res.is_a?(Array) ? res.collect{|i| Drop.new(i)} : Drop.new(res)) : bad_response(res)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment