Class: Dropbox::Entry

Inherits:
Object show all
Defined in:
lib/dropbox/entry.rb

Overview

A façade over a Dropbox::Session that allows the programmer to interact with Dropbox files in an object-oriented manner. The Dropbox::Entry instance is created by calling the Dropbox::API#entry method:

file = session.file('remote/file.pdf')
dir = session.directory('remote/dir') # these calls are actually identical

Note that no network calls are made; this merely creates a façade that will delegate future calls to the session:

file.move('new/path') # identical to calling session.move('remote/file.pdf', 'new/path')

The internal path is updated as the file is moved and renamed:

file = session.file('first_name.txt')
file.rename('second_name.txt')
file.rename('third_name.txt') # works as the internal path is updated with the first rename

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, path) ⇒ Entry

:nodoc:


29
30
31
32
# File 'lib/dropbox/entry.rb', line 29

def initialize(session, path) # :nodoc:
  @session = session
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

The remote path of the file.


27
28
29
# File 'lib/dropbox/entry.rb', line 27

def path
  @path
end

Class Method Details

.create_from_metadata(session, metadata) ⇒ Object


34
35
36
37
38
39
# File 'lib/dropbox/entry.rb', line 34

def self.(session, )
  entry = new(session, .path)
  entry. = 

  entry
end

Instance Method Details

#copy(dest, options = {}) ⇒ Object Also known as: cp

Delegates to Dropbox::API#copy.


120
121
122
# File 'lib/dropbox/entry.rb', line 120

def copy(dest, options={})
  @session.copy path, dest, options
end

#delete(options = {}) ⇒ Object Also known as: rm

Delegates to Dropbox::API#delete.


127
128
129
# File 'lib/dropbox/entry.rb', line 127

def delete(options={})
  @session.delete path, options
end

#directory?Boolean

Returns:

  • (Boolean)

171
172
173
# File 'lib/dropbox/entry.rb', line 171

def directory?
  .directory?
end

#download(options = {}) ⇒ Object Also known as: body

Delegates to Dropbox::API#download.


134
135
136
# File 'lib/dropbox/entry.rb', line 134

def download(options={})
  @session.download path, options
end

#file(options = {}) ⇒ Tempfile

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :force (Boolean)

    create new file instead of using cached one

Returns:

  • (Tempfile)

156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/dropbox/entry.rb', line 156

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

#inspectObject

:nodoc:


175
176
177
# File 'lib/dropbox/entry.rb', line 175

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

Delegates to Dropbox::API#link.


147
148
149
# File 'lib/dropbox/entry.rb', line 147

def link(options={})
  @session.link path, options
end

#list(options = {}) ⇒ Array<Dropbox::Entry> Also known as: ls

use Dropbox::API#list

Returns:


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dropbox/entry.rb', line 84

def list(options={})
  # load metadata first
  (options)
  throw :not_a_directory unless directory?

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

  contents.map do |struct|
    self.class.(@session, struct)
  end
end

#metadata(options = {}) ⇒ Object Also known as: info

Delegates to Dropbox::Entry#update_metadata. Use caching.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :ignore_cache (Boolean)

    Normally, subsequent calls to this method will use cached results. To download the full metadata set this to true.

  • :force (Boolean)

    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.


47
48
49
50
51
52
# File 'lib/dropbox/entry.rb', line 47

def (options={})
  @cached_metadata = nil if options.delete(:ignore_cache) or options[:force]
  return @cached_metadata if @cached_metadata

  (options)
end

#metadata=(new_metadata) ⇒ Object Also known as: info=

Parameters:

  • new_metadata (Struct)

Raises:

  • (ArgumentError)

71
72
73
74
75
76
# File 'lib/dropbox/entry.rb', line 71

def metadata=()
  raise ArgumentError, "#{.inspect} does not respond to #path" unless .respond_to?(:path)

  @path = .path
  @cached_metadata = 
end

#move(dest, options = {}) ⇒ Object Also known as: mv

Delegates to Dropbox::API#move.


103
104
105
106
107
# File 'lib/dropbox/entry.rb', line 103

def move(dest, options={})
  result = @session.move(path, dest, options)
  @path = result.path.gsub(/^\//, '')
  return result
end

#rename(name, options = {}) ⇒ Object

Delegates to Dropbox::API#rename.


112
113
114
115
116
# File 'lib/dropbox/entry.rb', line 112

def rename(name, options={})
  result = @session.rename(path, name, options)
  @path = result.path.gsub(/^\//, '')
  return result
end

#thumbnail(*args) ⇒ Object

Delegates to Dropbox::API#thumbnail.


141
142
143
# File 'lib/dropbox/entry.rb', line 141

def thumbnail(*args)
  @session.thumbnail path, *args
end

#update_metadata(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :force (Boolean)

    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.


61
62
63
64
65
66
67
68
# File 'lib/dropbox/entry.rb', line 61

def (options={})
  @previous_metadata ||= @cached_metadata

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

  @cached_metadata = @previous_metadata
end