Class: Deas::Runner::SendFileBody

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

Constant Summary collapse

CHUNK_SIZE =

this class borrows from the body range handling in Rack::File.

(8*1024).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, path_name) ⇒ SendFileBody

Returns a new instance of SendFileBody.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/deas/runner.rb', line 238

def initialize(env, path_name)
  @path_name = path_name

  file_size = @path_name.size? || Rack::Utils.bytesize(path_name.read)
  ranges = byte_ranges(env, file_size)
  if ranges.nil? || ranges.empty? || ranges.length > 1
    # No ranges or multiple ranges are not supported
    @range         = 0..file_size-1
    @content_range = nil
  else
    # single range
    @range         = ranges[0]
    @content_range = "bytes #{@range.begin}-#{@range.end}/#{file_size}"
  end

  @size = self.range_end - self.range_begin + 1
end

Instance Attribute Details

#content_rangeObject (readonly)

Returns the value of attribute content_range.



236
237
238
# File 'lib/deas/runner.rb', line 236

def content_range
  @content_range
end

#path_nameObject (readonly)

Returns the value of attribute path_name.



236
237
238
# File 'lib/deas/runner.rb', line 236

def path_name
  @path_name
end

#sizeObject (readonly)

Returns the value of attribute size.



236
237
238
# File 'lib/deas/runner.rb', line 236

def size
  @size
end

Instance Method Details

#==(other_body) ⇒ Object



283
284
285
286
287
# File 'lib/deas/runner.rb', line 283

def ==(other_body)
  self.path_name.to_s == other_body.path_name.to_s &&
  self.range_begin    == other_body.range_begin    &&
  self.range_end      == other_body.range_end
end

#eachObject



263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/deas/runner.rb', line 263

def each
  @path_name.open("rb") do |io|
    io.seek(@range.begin)
    remaining_len = self.size
    while remaining_len > 0
      part = io.read([CHUNK_SIZE, remaining_len].min)
      break if part.nil?

      remaining_len -= part.length
      yield part
    end
  end
end

#inspectObject



277
278
279
280
281
# File 'lib/deas/runner.rb', line 277

def inspect
  "#<#{self.class}:#{'0x0%x' % (self.object_id << 1)} " \
    "path=#{self.path_name} " \
    "range_begin=#{self.range_begin} range_end=#{self.range_end}>"
end

#partial?Boolean

Returns:

  • (Boolean)


256
257
258
# File 'lib/deas/runner.rb', line 256

def partial?
  !@content_range.nil?
end

#range_beginObject



260
# File 'lib/deas/runner.rb', line 260

def range_begin; @range.begin; end

#range_endObject



261
# File 'lib/deas/runner.rb', line 261

def range_end;   @range.end;   end