Class: Refile::Backend::S3::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/refile/backend/s3.rb

Overview

Emulates an IO-object like interface on top of S3Object#read. To avoid memory allocations and unnecessary complexity, this treats the ‘length` parameter to read as a boolean flag instead. If given, it will read the file in chunks of undetermined size, if not given it will read the entire file.

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Reader

Returns a new instance of Reader.



13
14
15
16
# File 'lib/refile/backend/s3.rb', line 13

def initialize(object)
  @object = object
  @closed = false
end

Instance Method Details

#closeObject



43
44
45
# File 'lib/refile/backend/s3.rb', line 43

def close
  @closed = true
end

#eof?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/refile/backend/s3.rb', line 32

def eof?
  @peek ||= enumerator.next
  false
rescue StopIteration
  true
end

#read(length = nil, buffer = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/refile/backend/s3.rb', line 18

def read(length = nil, buffer = nil)
  result = if length
    raise "closed" if @closed

    @peek unless eof? # sets @peek
  else
    @object.read
  end
  buffer.replace(result) if buffer and result
  result
ensure
  @peek = nil
end

#sizeObject



39
40
41
# File 'lib/refile/backend/s3.rb', line 39

def size
  @object.content_length
end