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.



15
16
17
18
# File 'lib/refile/backend/s3.rb', line 15

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

Instance Method Details

#closeObject



47
48
49
# File 'lib/refile/backend/s3.rb', line 47

def close
  @closed = true
end

#eof?Boolean

Returns:

  • (Boolean)


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

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

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/refile/backend/s3.rb', line 20

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

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

#sizeObject



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

def size
  @object.content_length
end