Class: Down::ChunkedIO

Inherits:
Object
  • Object
show all
Defined in:
lib/down.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ChunkedIO

Returns a new instance of ChunkedIO.



140
141
142
143
144
145
# File 'lib/down.rb', line 140

def initialize(options)
  @size     = options.fetch(:size)
  @chunks   = options.fetch(:chunks)
  @on_close = options.fetch(:on_close, ->{})
  @tempfile = Tempfile.new("down", binmode: true)
end

Instance Attribute Details

#tempfileObject (readonly)

Returns the value of attribute tempfile.



138
139
140
# File 'lib/down.rb', line 138

def tempfile
  @tempfile
end

Instance Method Details

#closeObject



169
170
171
172
# File 'lib/down.rb', line 169

def close
  terminate_download
  @tempfile.close!
end

#each_chunkObject



156
157
158
159
# File 'lib/down.rb', line 156

def each_chunk
  return enum_for(__method__) if !block_given?
  yield download_chunk until download_finished?
end

#eof?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/down.rb', line 161

def eof?
  @tempfile.eof? && download_finished?
end

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



151
152
153
154
# File 'lib/down.rb', line 151

def read(length = nil, outbuf = nil)
  download_chunk until enough_downloaded?(length) || download_finished?
  @tempfile.read(length, outbuf)
end

#rewindObject



165
166
167
# File 'lib/down.rb', line 165

def rewind
  @tempfile.rewind
end

#sizeObject



147
148
149
# File 'lib/down.rb', line 147

def size
  @size
end