Class: Alipan::Drive

Inherits:
Common::Struct show all
Defined in:
lib/alipan/drive.rb

Instance Method Summary collapse

Methods inherited from Common::Struct

attrs

Constructor Details

#initialize(opts = {}, protocol = nil) ⇒ Drive

Returns a new instance of Drive.



8
9
10
11
# File 'lib/alipan/drive.rb', line 8

def initialize(opts = {}, protocol = nil)
  super(opts)
  @protocol = protocol
end

Instance Method Details

#delete_object(key) ⇒ Object



50
51
52
# File 'lib/alipan/drive.rb', line 50

def delete_object(key)
  @protocol.delete_object(resource_drive_id, key)
end

#get_object(key, opts = {}, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/alipan/drive.rb', line 17

def get_object(key, opts = {}, &block)
  obj = nil
  file = opts[:file]
  if file
    File.open(File.expand_path(file), 'wb') do |f|
      obj = @protocol.get_object(resource_drive_id, key, opts) do |chunk|
        f.write(chunk)
      end
    end
  elsif block
    obj = @protocol.get_object(resource_drive_id, key, opts, &block)
  else
    obj = @protocol.get_object(resource_drive_id, key, opts)
  end

  obj
end

#list_objects(opts = {}) ⇒ Object



13
14
15
# File 'lib/alipan/drive.rb', line 13

def list_objects(opts = {})
  Iterator::Objects.new(@protocol, resource_drive_id, 'root', opts).to_enum
end

#put_object(key, opts = {}, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/alipan/drive.rb', line 35

def put_object(key, opts = {}, &block)
  file = opts[:file]

  if file
    @protocol.put_object(resource_drive_id, key, opts) do |sw|
      File.open(File.expand_path(file), 'rb') do |f|
        sw << f.read(Protocol::STREAM_CHUNK_SIZE) until f.eof?
      end
    end
  else
    @protocol.put_object(resource_drive_id, key, opts, &block)
  end
end