Class: IntervalResponse::Single

Inherits:
Object
  • Object
show all
Includes:
ToRackResponseTriplet
Defined in:
lib/interval_response/single.rb

Overview

Serves out a response that consists of one HTTP Range, which is always not the entire resource

Constant Summary

Constants included from ToRackResponseTriplet

ToRackResponseTriplet::CHUNK_SIZE

Instance Method Summary collapse

Methods included from ToRackResponseTriplet

#to_rack_response_triplet

Constructor Details

#initialize(interval_map, http_ranges) ⇒ Single

Returns a new instance of Single.



6
7
8
9
# File 'lib/interval_response/single.rb', line 6

def initialize(interval_map, http_ranges)
  @interval_map = interval_map
  @http_range = http_ranges.first
end

Instance Method Details

#content_lengthObject



22
23
24
# File 'lib/interval_response/single.rb', line 22

def content_length
  @http_range.end - @http_range.begin + 1
end

#eachObject

Serve the part of the interval map



12
13
14
15
16
# File 'lib/interval_response/single.rb', line 12

def each
  @interval_map.each_in_range(@http_range) do |segment, range_in_segment|
    yield(segment, range_in_segment)
  end
end

#headersObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/interval_response/single.rb', line 26

def headers
  c_range = ('bytes %d-%d/%d' % [@http_range.begin, @http_range.end, @interval_map.size])
  {
    'Accept-Ranges' => 'bytes',
    'Content-Length' => (@http_range.end - @http_range.begin + 1).to_s,
    'Content-Type' => 'binary/octet-stream',
    'Content-Range' => c_range,
    'ETag' => @interval_map.etag,
  }
end

#status_codeObject



18
19
20
# File 'lib/interval_response/single.rb', line 18

def status_code
  206
end