Class: Dropbox::Revision

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

Overview

A file or folder at a point in time as referenced by a Dropbox::Event pingback event. Instances start out as “shells” only storing enough information to uniquely identify a file/folder belonging to a user at a certain revision.

Instances of this class only appear in a Dropbox::Event object. To load the metadata for a revision, use the Dropbox::Event#load_metadata method. To load the content of the file at this revision, use the load_content method on this class.

Once the metadata has been loaded, you can access it directly:

revision.size #=> 2962

The mtime attribute will be a Time instance. The mtime and size attributes will be nil (not -1) if the file was deleted. All other attributes are as defined in developers.getdropbox.com/base.html#event-metadata

If the metadata could not be read for whatever reason, the HTTP error code will be stored in the error attribute.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uid, nid, jid) ⇒ Revision

:nodoc:



40
41
42
43
44
# File 'lib/dropbox/revision.rb', line 40

def initialize(uid, nid, jid) # :nodoc:
  @user_id = uid
  @namespace_id = nid
  @journal_id = jid
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Allows you to access metadata attributes directly:

revision.size #=> 10526

A NoMethodError will be raised if the metadata has not yet been loaded for this revision, so be sure to call metadata_loaded? beforehand.



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/dropbox/revision.rb', line 135

def method_missing(meth, *args)
  if args.empty? then
    if  and .include?(meth) then
      return [meth]
    else
      super
    end
  else
    super
  end
end

Instance Attribute Details

#errorObject (readonly)

The HTTP error code received when trying to load metadata, or nil if no error has yet been received.



38
39
40
# File 'lib/dropbox/revision.rb', line 38

def error
  @error
end

#journal_idObject (readonly)

The journal ID of the file (Dropbox internal).



35
36
37
# File 'lib/dropbox/revision.rb', line 35

def journal_id
  @journal_id
end

#namespace_idObject (readonly)

The namespace ID of the file (Dropbox internal).



33
34
35
# File 'lib/dropbox/revision.rb', line 33

def namespace_id
  @namespace_id
end

#user_idObject (readonly)

The ID of the Dropbox user that owns this file.



31
32
33
# File 'lib/dropbox/revision.rb', line 31

def user_id
  @user_id
end

Instance Method Details

#contentObject

Returns the contents of the file as a string. Returns nil for directories. You must call load first to retrieve the content from the network.

Raises:



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

def content
  raise NotLoadedError.new(:content) unless content_loaded?
  @content
end

#content_loaded?Boolean

Returns true if the content for this revision has been previously loaded and is cached in this object.

Returns:

  • (Boolean)


70
71
72
# File 'lib/dropbox/revision.rb', line 70

def content_loaded?
  @content.to_bool
end

#deleted?Boolean

Returns true if this change represents the file being deleted.

Returns:

  • (Boolean)

Raises:



111
112
113
114
# File 'lib/dropbox/revision.rb', line 111

def deleted?
  raise NotLoadedError.new(:metadata) unless 
  self.mtime.nil? and self.size.nil?
end

#directory?Boolean

Sugar for the is_dir attribute.

Returns:

  • (Boolean)

Raises:



90
91
92
93
# File 'lib/dropbox/revision.rb', line 90

def directory?
  raise NotLoadedError.new(:metadata) unless 
  self.is_dir
end

#error?Boolean

Returns true if an error occurred when trying to load metadata.

Returns:

  • (Boolean)


105
106
107
# File 'lib/dropbox/revision.rb', line 105

def error?
  error.to_bool
end

#identifierObject

The unique identifier string used by some Dropbox event API methods.



48
49
50
# File 'lib/dropbox/revision.rb', line 48

def identifier
  "#{user_id}:#{namespace_id}:#{journal_id}"
end

#inspectObject

:nodoc:



124
125
126
# File 'lib/dropbox/revision.rb', line 124

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

#latest?Boolean

Sugar for the latest attribute.

Returns:

  • (Boolean)

Raises:



83
84
85
86
# File 'lib/dropbox/revision.rb', line 83

def latest?
  raise NotLoadedError.new(:metadata) unless 
  self.latest
end

#load(session, options = {}) ⇒ Object

Uses the given Dropbox::Session to load the content and metadata for a file at a specific revision.

Options:

mode

Temporarily changes the API mode. See the Dropbox::API::MODES array.



60
61
62
63
64
65
# File 'lib/dropbox/revision.rb', line 60

def load(session, options={})
  @content,  = session.event_content(identifier, options)
  .symbolize_keys!

  
end

#metadata_for_latest_revision(session, options = {}) ⇒ Object

Loads the metadata for the latest revision of the entry and returns it as as Struct object. Uses the given session and calls Dropbox::API.metadata.

If the metadata for this object has not yet been loaded, raises an error. Options are passed to Dropbox::API.metadata.

Raises:



154
155
156
157
# File 'lib/dropbox/revision.rb', line 154

def (session, options={})
  raise NotLoadedError.new(:metadata) unless 
  session. self.path, options
end

#metadata_loaded?Boolean

Returns true if the metadata for this revision has been previously loaded and is cached in this object.

Returns:

  • (Boolean)


77
78
79
# File 'lib/dropbox/revision.rb', line 77

def 
  .to_bool
end

#modifiedObject

Synonym for the mtime attribute, for “duck” compatibility with the Dropbox metadata API.

Raises:



98
99
100
101
# File 'lib/dropbox/revision.rb', line 98

def modified
  raise NotLoadedError.new(:metadata) unless 
  self.mtime
end

#process_metadata(metadata) ⇒ Object

:nodoc:



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/dropbox/revision.rb', line 159

def () # :nodoc:
  if [:error] then
    @error = [:error] unless 
    return
  end

  @error = nil
   = Hash.new
  .each { |key, value| [key.to_sym] = value }

  
end