Class: Dbox::DB::DropboxBlob

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/dbox/db.rb

Direct Known Subclasses

DropboxDir, DropboxFile

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

included, #log

Constructor Details

#initialize(db, res) ⇒ DropboxBlob

Returns a new instance of DropboxBlob.



165
166
167
168
169
# File 'lib/dbox/db.rb', line 165

def initialize(db, res)
  @db = db
  @path = @db.remote_to_relative_path(res["path"])
  update_modification_info(res)
end

Instance Attribute Details

#modified_atObject (readonly)

Returns the value of attribute modified_at.



163
164
165
# File 'lib/dbox/db.rb', line 163

def modified_at
  @modified_at
end

#pathObject (readonly)

Returns the value of attribute path.



163
164
165
# File 'lib/dbox/db.rb', line 163

def path
  @path
end

#revisionObject (readonly)

Returns the value of attribute revision.



163
164
165
# File 'lib/dbox/db.rb', line 163

def revision
  @revision
end

Instance Method Details

#apiObject



282
283
284
# File 'lib/dbox/db.rb', line 282

def api
  @db.api
end

#create(direction) ⇒ Object



204
205
206
207
208
209
210
211
# File 'lib/dbox/db.rb', line 204

def create(direction)
  case direction
  when :down
    create_local
  when :up
    create_remote
  end
end

#create_localObject

Raises:

  • (RuntimeError)


231
# File 'lib/dbox/db.rb', line 231

def create_local; raise RuntimeError, "Not implemented"; end

#create_remoteObject

Raises:

  • (RuntimeError)


235
# File 'lib/dbox/db.rb', line 235

def create_remote; raise RuntimeError, "Not implemented"; end

#delete(direction) ⇒ Object



222
223
224
225
226
227
228
229
# File 'lib/dbox/db.rb', line 222

def delete(direction)
  case direction
  when :down
    delete_local
  when :up
    delete_remote
  end
end

#delete_localObject

Raises:

  • (RuntimeError)


232
# File 'lib/dbox/db.rb', line 232

def delete_local; raise RuntimeError, "Not implemented"; end

#delete_remoteObject

Raises:

  • (RuntimeError)


236
# File 'lib/dbox/db.rb', line 236

def delete_remote; raise RuntimeError, "Not implemented"; end

#dir?Boolean

Returns:

  • (Boolean)

Raises:

  • (RuntimeError)


200
201
202
# File 'lib/dbox/db.rb', line 200

def dir?
  raise RuntimeError, "Not implemented"
end

#force_metadata_update_from_serverObject

this downloads the metadata about this blob from the server and overwrites the metadata & timestamp IMPORTANT: should only be called if you are CERTAIN the file is up to date



271
272
273
274
275
# File 'lib/dbox/db.rb', line 271

def 
  res = api.(remote_path)
  update_modification_info(res)
  update_file_timestamp
end

#local_pathObject



192
193
194
# File 'lib/dbox/db.rb', line 192

def local_path
  @db.relative_to_local_path(@path)
end

#modified?(res) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
242
243
# File 'lib/dbox/db.rb', line 239

def modified?(res)
  out = !(@revision == res["revision"] && time_to_s(@modified_at) == time_to_s(res["modified"]))
  log.debug "#{path} modified? => #{out}"
  out
end

#parse_time(t) ⇒ Object



255
256
257
258
259
260
261
262
# File 'lib/dbox/db.rb', line 255

def parse_time(t)
  case t
  when Time
    t
  when String
    Time.parse(t)
  end
end

#remote_pathObject



196
197
198
# File 'lib/dbox/db.rb', line 196

def remote_path
  @db.relative_to_remote_path(@path)
end

#saving_parent_timestamp(&proc) ⇒ Object



277
278
279
280
# File 'lib/dbox/db.rb', line 277

def saving_parent_timestamp(&proc)
  parent = File.dirname(local_path)
  DB.saving_timestamp(parent, &proc)
end

#smart_new(res) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/dbox/db.rb', line 184

def smart_new(res)
  if res["is_dir"]
    DropboxDir.new(@db, res)
  else
    DropboxFile.new(@db, res)
  end
end

#time_to_s(t) ⇒ Object



245
246
247
248
249
250
251
252
253
# File 'lib/dbox/db.rb', line 245

def time_to_s(t)
  case t
  when Time
    # matches dropbox time format
    t.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
  when String
    t
  end
end

#update(direction) ⇒ Object



213
214
215
216
217
218
219
220
# File 'lib/dbox/db.rb', line 213

def update(direction)
  case direction
  when :down
    update_local
  when :up
    update_remote
  end
end

#update_file_timestampObject



264
265
266
# File 'lib/dbox/db.rb', line 264

def update_file_timestamp
  File.utime(Time.now, @modified_at, local_path)
end

#update_localObject

Raises:

  • (RuntimeError)


233
# File 'lib/dbox/db.rb', line 233

def update_local; raise RuntimeError, "Not implemented"; end

#update_modification_info(res) ⇒ Object

Raises:



171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/dbox/db.rb', line 171

def update_modification_info(res)
  raise(BadPath, "Bad path (#{remote_path} != #{res["path"]})") unless remote_path == res["path"]
  raise(RuntimeError, "Mode on #{@path} changed between file and dir -- not supported yet") unless dir? == res["is_dir"]
  last_modified_at = @modified_at
  @modified_at = parse_time(res["modified"])
  if res["revision"]
    @revision = res["revision"]
  else
    @revision = -1 if @modified_at != last_modified_at
  end
  log.debug "updated modification info on #{path.inspect}: r#{@revision}, #{@modified_at}"
end

#update_remoteObject

Raises:

  • (RuntimeError)


237
# File 'lib/dbox/db.rb', line 237

def update_remote; raise RuntimeError, "Not implemented"; end