Class: ImageSize::URIReader::RangeReader
- Inherits:
-
Object
- Object
- ImageSize::URIReader::RangeReader
- Includes:
- HTTPChunkyReader
- Defined in:
- lib/image_size/uri_reader.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#byte_size ⇒ Object
readonly
Returns the value of attribute byte_size.
Instance Method Summary collapse
- #chunk(i) ⇒ Object
-
#initialize(http, request_uri, chunk0, byte_size) ⇒ RangeReader
constructor
A new instance of RangeReader.
Methods included from HTTPChunkyReader
#chunk_range_header, #chunk_start
Methods included from ChunkyReader
Methods included from Reader
#fetch, open_with_uri, #stream, #unpack, #unpack1
Constructor Details
#initialize(http, request_uri, chunk0, byte_size) ⇒ RangeReader
Returns a new instance of RangeReader.
58 59 60 61 62 63 64 |
# File 'lib/image_size/uri_reader.rb', line 58 def initialize(http, request_uri, chunk0, byte_size) @http = http @request_uri = request_uri @chunks = { 0 => chunk0 } @byte_size = byte_size @last_chunk = nil end |
Instance Attribute Details
#byte_size ⇒ Object (readonly)
Returns the value of attribute byte_size.
56 57 58 |
# File 'lib/image_size/uri_reader.rb', line 56 def byte_size @byte_size end |
Instance Method Details
#chunk(i) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/image_size/uri_reader.rb', line 66 def chunk(i) return if @byte_size && chunk_start(i) >= @byte_size return if @last_chunk && i > @last_chunk unless @chunks.key?(i) response = @http.get(@request_uri, chunk_range_header(i)) case response when Net::HTTPPartialContent body = response.body @chunks[i] = body @last_chunk = i if body.length < chunk_size when Net::HTTPRequestedRangeNotSatisfiable @chunks[i] = nil @last_chunk = i if !@last_chunk || @last_chunk > i else fail "Unexpected response: #{response}" end end @chunks[i] end |