Class: PinchResponse

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

Overview

This class only implements read_body The rest can be implemented when required

Instance Method Summary collapse

Constructor Details

#initialize(http_response) ⇒ PinchResponse

Prepare pinch response



8
9
10
11
12
# File 'lib/pinch_response.rb', line 8

def initialize(http_response)
  @http_response = http_response
  @zstream       = Zlib::Inflate.new(-Zlib::MAX_WBITS)
  @first_chunk   = true
end

Instance Method Details

#read_bodyObject

Read the body of the HTTP response



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/pinch_response.rb', line 17

def read_body
  if block_given?
    @http_response.read_body do |chunk|
      if @first_chunk
        local_file_header = chunk.unpack('VvvvvvVVVvv')

        @offset_start = 30+local_file_header[9]+local_file_header[10]
        @compressed   = (local_file_header[3] != 0)
        @length       = @compressed ? local_file_header[7] : local_file_header[8]
        @cursor_start = @offset_start
        @to_be_read   = @length

        @first_chunk  = false
      end

      cursor_start = [@cursor_start, 0].max
      cursor_end   = [@to_be_read, chunk.length].min

      data = chunk[cursor_start, cursor_end]

      @cursor_start -= chunk.length

      if data
        @to_be_read -= data.length

        if @compressed
          yield @zstream.inflate(data)
        else
          yield data
        end
      end
    end
  else
    @zstream.inflate @http_response.read_body
  end
end