lib/dropbox/api.rb in dropbox-1.1.2 vs lib/dropbox/api.rb in dropbox-1.2.0

- old
+ new

@@ -86,11 +86,11 @@ rest = Dropbox.check_path(path).split('/') rest << { :ssl => @ssl } api_body :get, 'files', root(options), *rest #TODO streaming, range queries end - + # Downloads a minimized thumbnail for a file. Pass the path to the file, # optionally the size of the thumbnail you want, and any additional options. # See https://www.dropbox.com/developers/docs#thumbnails for a list of valid # size specifiers. # @@ -103,10 +103,12 @@ # file that does not exist, you will not receive a 404, but instead just get # +nil+. # # Options: # + # +format+:: The image format (see the API documentation for supported + # formats). # +mode+:: Temporarily changes the API mode. See the MODES array. # # Examples: # # Get the thumbnail for an image (default thunmbnail size): @@ -114,22 +116,17 @@ # session.thumbnail('my/image.jpg') # # Get the thumbnail for an image in the +medium+ size: # # session.thumbnail('my/image.jpg', 'medium') - - def thumbnail(*args) - options = args.extract_options! - path = args.shift - size = args.shift - raise ArgumentError, "thumbnail takes a path, an optional size, and optional options" unless path.kind_of?(String) and (size.kind_of?(String) or size.nil?) and args.empty? - + + def thumbnail(path, options={})\ path = path.sub(/^\//, '') rest = Dropbox.check_path(path).split('/') rest << { :ssl => @ssl } - rest.last[:size] = size if size - + rest.last.merge! options + begin api_body :get, 'thumbnails', root(options), *rest rescue Dropbox::UnsuccessfulResponseError => e raise unless e.response.code.to_i == 404 return nil @@ -139,35 +136,45 @@ # Uploads a file to a path relative to the configured mode's root. The # +remote_path+ parameter is taken to be the path portion _only_; the name # of the remote file will be identical to that of the local file. You can # provide any of the following for the first parameter: # - # * a +File+ object, in which case the name of the local file is used, or - # * a path to a file, in which case that file's name is used. + # * a +File+ object (in which case the name of the local file is used), + # * a path to a file (in which case that file's name is used), or + # * a +StringIO+ (in which case the <tt>:as</tt> option must be specified. # # Options: # # +mode+:: Temporarily changes the API mode. See the MODES array. + # +as+:: Specify a custom name for the uploaded file (required when + # uploading from a +StringIO+ stream). # # Examples: # # session.upload 'music.pdf', '/' # upload a file by path to the root directory # session.upload 'music.pdf, 'music/' # upload a file by path to the music folder # session.upload File.new('music.pdf'), '/' # same as the first example + # session.upload open('http://www.example.com/index.html'), :as => 'example.html' # upload from a StringIO stream (requires open-uri) def upload(local_file, remote_path, options={}) if local_file.kind_of?(File) or local_file.kind_of?(Tempfile) then file = local_file name = local_file.respond_to?(:original_filename) ? local_file.original_filename : File.basename(local_file.path) local_path = local_file.path elsif local_file.kind_of?(String) then file = File.new(local_file) name = File.basename(local_file) local_path = local_file + elsif local_file.kind_of?(StringIO) then + raise(ArgumentError, "Must specify the :as option when uploading from StringIO") unless options[:as] + file = local_file + local_path = options[:as] else - raise ArgumentError, "local_file must be a File or file path" + raise ArgumentError, "local_file must be a File, StringIO, or file path" end + + name = options.delete(:as).to_s if options[:as] remote_path = remote_path.sub(/^\//, '') remote_path = Dropbox.check_path(remote_path).split('/') remote_path << { :ssl => @ssl } @@ -186,12 +193,13 @@ file, 'application/octet-stream', name, local_path)) request['authorization'] = oauth_signature.join(', ') - - response = Net::HTTP.start(uri.host, uri.port) { |http| http.request(request) } + + proxy = URI.parse(@proxy || "") + response = Net::HTTP::Proxy(proxy.host, proxy.port).new(uri.host, uri.port).request(request) if response.kind_of?(Net::HTTPSuccess) then begin return JSON.parse(response.body).symbolize_keys_recursively.to_struct_recursively rescue JSON::ParserError raise ParseError.new(uri.to_s, response) @@ -262,11 +270,11 @@ # Options: # # +mode+:: Temporarily changes the API mode. See the MODES array. # # TODO The API documentation says this method returns 404 if the path does not exist, but it actually returns 5xx. - + def delete(path, options={}) path = path.sub(/^\//, '') path.sub! /\/$/, '' begin api_response(:post, 'fileops', 'delete', :path => Dropbox.check_path(path), :root => root(options), :ssl => @ssl) @@ -386,11 +394,11 @@ args << Hash.new args.last[:file_limit] = options[:limit] if options[:limit] #args.last[:hash] = options[:hash] if options[:hash] args.last[:list] = !(options[:suppress_list].to_bool) args.last[:ssl] = @ssl - + begin parse_metadata(get(*args)).to_struct_recursively rescue UnsuccessfulResponseError => error raise TooManyEntriesError.new(path) if error.response.kind_of?(Net::HTTPNotAcceptable) raise FileNotFoundError.new(path) if error.response.kind_of?(Net::HTTPNotFound) @@ -417,11 +425,11 @@ def list(path, options={}) metadata(path, options.merge(:suppress_list => false)).contents end alias :ls :list - + def event_metadata(target_events, options={}) # :nodoc: get 'event_metadata', :ssl => @ssl, :root => root(options), :target_events => target_events end def event_content(entry, options={}) # :nodoc: @@ -556,20 +564,20 @@ # Raised when the number of files within a directory exceeds a specified # limit. class TooManyEntriesError < FileError; end - + # Raised when the event_metadata method returns an error. - + class PingbackError < StandardError # The HTTP error code returned by the event_metadata method. attr_reader :code - + def initialize(code) # :nodoc @code = code end - + def to_s # :nodoc: "#{self.class.to_s} code #{@code}" end end end