Class: IntervalResponse::Abstract
- Inherits:
-
Object
- Object
- IntervalResponse::Abstract
- Defined in:
- lib/interval_response/abstract.rb
Overview
Base class for all response types, primarily for ease of documentation
Instance Method Summary collapse
-
#content_length ⇒ Integer
Returns the exact number of bytes that the response is.
-
#each {|Object, Range| ... } ⇒ Object
Yields every segment and the range within that segment to be returned to the client.
-
#etag ⇒ Object
Returns the ETag of the interval sequence.
-
#headers ⇒ Hash
Returns headers for the HTTP response.
-
#initialize(interval_sequence) ⇒ Abstract
constructor
A new instance of Abstract.
-
#multiple_ranges? ⇒ Boolean
Tells whether this response is responding with multiple ranges.
-
#satisfied_with_first_interval? ⇒ Boolean
Tells whether this entire requested range can be satisfied with the first available segment within the given Sequence.
-
#status_code ⇒ Integer
Returns the HTTP status code of the response.
- #to_rack_response_triplet(headers: nil, chunk_size: IntervalResponse::RackBodyWrapper::CHUNK_SIZE) ⇒ Object
Constructor Details
#initialize(interval_sequence) ⇒ Abstract
Returns a new instance of Abstract.
33 34 35 |
# File 'lib/interval_response/abstract.rb', line 33 def initialize(interval_sequence) @interval_sequence = interval_sequence end |
Instance Method Details
#content_length ⇒ Integer
Returns the exact number of bytes that the response is. If the response is a range it will be the length of the range. If the response is a multipart byte range response it will be the content length of the ranges plus the content length of all the envelopes.
63 64 65 |
# File 'lib/interval_response/abstract.rb', line 63 def content_length 0 end |
#each {|Object, Range| ... } ⇒ Object
Yields every segment and the range within that segment to be returned to the client. For multipart responses the envelopes of the parts will be returned as segments as well
46 47 48 |
# File 'lib/interval_response/abstract.rb', line 46 def each # No-op end |
#etag ⇒ Object
Returns the ETag of the interval sequence
38 39 40 |
# File 'lib/interval_response/abstract.rb', line 38 def etag @interval_sequence.etag end |
#headers ⇒ Hash
Returns headers for the HTTP response
69 70 71 72 73 74 75 76 |
# File 'lib/interval_response/abstract.rb', line 69 def headers { 'Accept-Ranges' => 'bytes', 'Content-Length' => '0', 'Content-Type' => 'binary/octet-stream', 'ETag' => etag, } end |
#multiple_ranges? ⇒ Boolean
Tells whether this response is responding with multiple ranges. If you want to simulate S3 for example, it might be relevant to deny a response from being served if it does respond with multiple ranges - IntervalResponse supports these responses just fine, but S3 doesn’t.
10 11 12 |
# File 'lib/interval_response/abstract.rb', line 10 def multiple_ranges? false end |
#satisfied_with_first_interval? ⇒ Boolean
Tells whether this entire requested range can be satisfied with the first available segment within the given Sequence. If it is, then you can redirect to the URL of the first segment instead of streaming the response through - which can be cheaper for your application server. Note that you can redirect to the resource of the first interval only, because otherwise your ‘Range` header will no longer match. Suppose you have a stitched resource consisting of two segments:
[bytes 0..456]
[bytes 457..890]
and your client requests ‘Range: bytes=0-33`. You can redirect the client to the location of the first interval, and the `Range:` header will be retransmitted to that location and will be satisfied. However, imagine you are requesting the `Range: bytes=510-512` - you could redirect just to the second interval, but the `Range` header is not going to be adjusted by the client, and you are not going to receive the correct slice of the resource. That’s why you can only redirect to the first interval only.
28 29 30 |
# File 'lib/interval_response/abstract.rb', line 28 def satisfied_with_first_interval? false end |
#status_code ⇒ Integer
Returns the HTTP status code of the response
53 54 55 |
# File 'lib/interval_response/abstract.rb', line 53 def status_code 200 end |
#to_rack_response_triplet(headers: nil, chunk_size: IntervalResponse::RackBodyWrapper::CHUNK_SIZE) ⇒ Object
3 4 5 |
# File 'lib/interval_response/abstract.rb', line 3 def to_rack_response_triplet(headers: nil, chunk_size: IntervalResponse::RackBodyWrapper::CHUNK_SIZE) [status_code, headers.to_h.merge(self.headers), IntervalResponse::RackBodyWrapper.new(self, chunk_size: chunk_size)] end |