Class: Mechanize::HTTP::Agent

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

Overview

Agent - A Monkeypatch for the mechanize progressbar

Instance Method Summary collapse

Instance Method Details

#response_read(response, request, _uri = nil) ⇒ Object

Raises:

  • (Mechanize::ResponseCodeError)


9
10
11
12
13
14
15
16
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
# File 'lib/mechanizeprogress.rb', line 9

def response_read(response, request, _uri = nil)
  body_io = StringIO.new
  body_io.set_encoding Encoding::BINARY if body_io.respond_to? :set_encoding
  total = 0
  mpbar = MechanizeProgressBar.new(context, request, response) # (2of7)a

  begin
    response.read_body do |part|
      total += part.length
      body_io.write(part)
      # log.debug("Read #{part.length} bytes (#{total} total)") if log
      log.debug("Read #{part.length} bytes (#{total} total)") if log && !mpbar.suppress_logger? # (3of7)m
      mpbar.inc(part.length) # (4of7)a
    end
  rescue Net::HTTP::Persistent::Error => e
    body_io.rewind
    raise Mechanize::ResponseReadError.new(e, response, body_io)
  ensure # (5of7)a
    mpbar.finish # (6of7)a
  end

  body_io.rewind
  log.debug("Read #{total} bytes total") if log && !mpbar.suppress_logger? # (7of7)a

  raise Mechanize::ResponseCodeError, response if
    Net::HTTPUnknownResponse == response

  content_length = response.content_length

  unless Net::HTTP::Head == request || Net::HTTPRedirection == response
    if content_length && content_length != body_io.length
      raise EOFError, "Content-Length (#{content_length}) does not match response body length (#{body_io.length})"
    end
  end

  body_io
end