Class: ImageSize::URIReader::RangeReader

Inherits:
Object
  • Object
show all
Includes:
HTTPChunkyReader
Defined in:
lib/image_size/uri_reader.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HTTPChunkyReader

#chunk_range_header, #chunk_start

Methods included from ChunkyReader

#[], #chunk_size

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_sizeObject (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