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

add new functionality to Entry class #46

Merged
merged 10 commits into from
Nov 17, 2011
95 changes: 83 additions & 12 deletions lib/dropbox/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,70 @@ def initialize(session, path) # :nodoc:
@path = path
end

# Delegates to Dropbox::API#metadata. Additional options:
#
# +force+:: Normally, subsequent calls to this method will use cached
# results if the file hasn't been changed. To download the full
# metadata even if the file has not been changed, set this to
# +true+.
def self.create_from_metadata(session, metadata)
entry = new(session, metadata.path)
entry.metadata = metadata

entry
end

# Delegates to Dropbox::Entry#update_metadata. Use caching.
#
# @param [Hash] options
# @option options [Boolean] :ignore_cache Normally, subsequent calls to this method will use cached
# results. To download the full metadata set this to +true+.
# @option (see Dropbox::Entry#update_metadata)
def metadata(options={})
@cached_metadata = nil if options.delete(:ignore_cache) or options[:force]
return @cached_metadata if @cached_metadata

update_metadata(options)
end
alias :info :metadata

# @param [Hash] options
# @option options [Boolean] :force Normally, subsequent calls to this method will use cached
# results if the file hasn't been changed. To download the full
# metadata even if the file has not been changed, set this to
# +true+.
#
def update_metadata(options={})
@previous_metadata ||= @cached_metadata

@previous_metadata = nil if options.delete(:force)
@previous_metadata = @session.metadata path, (@previous_metadata ? options.merge(:prior_response => @previous_metadata) : options)

@cached_metadata = @previous_metadata
end
alias :info :metadata

# Delegates to Dropbox::API#list

# @param [Struct] new_metadata
def metadata=(new_metadata)
raise ArgumentError, "#{new_metadata.inspect} does not respond to #path" unless new_metadata.respond_to?(:path)

@path = new_metadata.path
@cached_metadata = new_metadata
end
alias :info= :metadata=

#
# @return [Array<Dropbox::Entry>]
# @throw :not_a_directory
# use Dropbox::API#list

def list(options={})
@session.list path, options
# load metadata first
metadata(options)
throw :not_a_directory unless directory?

begin
contents = metadata.contents
rescue NoMethodError
contents = metadata(options.merge(:ignore_cache => true)).contents
end

contents.map do |struct|
self.class.create_from_metadata(@session, struct)
end
end
alias :ls :list

Expand Down Expand Up @@ -88,9 +135,9 @@ def download(options={})
@session.download path, options
end
alias :body :download

# Delegates to Dropbox::API#thumbnail.

def thumbnail(*args)
@session.thumbnail path, *args
end
Expand All @@ -101,6 +148,30 @@ def link(options={})
@session.link path, options
end

#
# @param [Hash] options
# @option options [Boolean] :force create new file instead of using cached one
# @return [Tempfile]
# @throw :not_a_file
def file(options={})
throw :not_a_file if directory?
return @cached_file if @cached_file && !options[:force]

ext = ::File.extname(path)
name = ::File.basename(path, ext)

file = Tempfile.new([name, ext], :encoding => "BINARY")
file.write download
file.rewind

@cached_file = file
end

# @return [Boolean]
def directory?
metadata.directory?
end

def inspect # :nodoc:
"#<#{self.class.to_s} #{path}>"
end
Expand Down
Loading