Class: IntervalResponse::Abstract

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

Overview

Base class for all response types, primarily for ease of documentation

Direct Known Subclasses

Empty, Full, Invalid, Multi, Single

Instance Method Summary collapse

Constructor Details

#initialize(interval_sequence) ⇒ Abstract

Returns a new instance of Abstract.

Parameters:



33
34
35
# File 'lib/interval_response/abstract.rb', line 33

def initialize(interval_sequence)
  @interval_sequence = interval_sequence
end

Instance Method Details

#content_lengthInteger

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.

Returns:

  • (Integer)


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

Yields:

  • (Object, Range)


46
47
48
# File 'lib/interval_response/abstract.rb', line 46

def each
  # No-op
end

#etagObject

Returns the ETag of the interval sequence



38
39
40
# File 'lib/interval_response/abstract.rb', line 38

def etag
  @interval_sequence.etag
end

#headersHash

Returns headers for the HTTP response

Returns:

  • (Hash)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


28
29
30
# File 'lib/interval_response/abstract.rb', line 28

def satisfied_with_first_interval?
  false
end

#status_codeInteger

Returns the HTTP status code of the response

Returns:

  • (Integer)


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