Class: Ferrum::Network::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ferrum/network/response.rb

Overview

Represents a Network.Response object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, params) ⇒ Response

Initializes the responses object.

Parameters:

  • page (Page) —

    The page associated with the network response.

  • params (Hash{String => Object}) —

    The parsed JSON attributes for the Network.Response



35
36
37
38
39
# File 'lib/ferrum/network/response.rb', line 35

def initialize(page, params)
  @page = page
  @params = params
  @response = params["response"] || params["redirectResponse"]
end

Instance Attribute Details

#body_size ⇒ Integer?

The response body size.

Returns:

  • (Integer, nil)


13
14
15
# File 'lib/ferrum/network/response.rb', line 13

def body_size
  @body_size
end

#loaded=(value) ⇒ Boolean (writeonly)

The response is fully loaded by the browser.

Returns:

  • (Boolean)


24
25
26
# File 'lib/ferrum/network/response.rb', line 24

def loaded=(value)
  @loaded = value
end

#params ⇒ Hash{String => Object} (readonly) Also known as: to_h

The parsed JSON attributes for the Network.Response object.

Returns:

  • (Hash{String => Object})


19
20
21
# File 'lib/ferrum/network/response.rb', line 19

def params
  @params
end

Instance Method Details

#==(other) ⇒ Boolean

Compares the response's ID to another response's ID.

Returns:

  • (Boolean) —

    Indicates whether the response has the same ID as the other response object.



178
179
180
# File 'lib/ferrum/network/response.rb', line 178

def ==(other)
  id == other.id
end

#body ⇒ String?

The response body.

Returns:

  • (String, nil)


142
143
144
145
146
# File 'lib/ferrum/network/response.rb', line 142

def body
  body!
rescue Ferrum::BrowserError
  # nop
end

#body! ⇒ String

The response body.

Returns:

  • (String)

Raises:



129
130
131
132
133
134
135
# File 'lib/ferrum/network/response.rb', line 129

def body!
  @body ||= begin
    body, encoded = @page.command("Network.getResponseBody", requestId: id)
                         .values_at("body", "base64Encoded")
    encoded ? Base64.decode64(body) : body
  end
end

#content_type ⇒ String?

The Content-Type header value of the response.

Returns:

  • (String, nil)


109
110
111
# File 'lib/ferrum/network/response.rb', line 109

def content_type
  @content_type ||= headers.find { |k, _| k.downcase == "content-type" }&.last&.sub(/;.*\z/, "")
end

#headers ⇒ Hash{String => String}

The headers of the response.

Returns:

  • (Hash{String => String})


82
83
84
# File 'lib/ferrum/network/response.rb', line 82

def headers
  @response["headers"]
end

#headers_size ⇒ Integer

The total size in bytes of the response.

Returns:

  • (Integer)


91
92
93
# File 'lib/ferrum/network/response.rb', line 91

def headers_size
  @response["encodedDataLength"]
end

#id ⇒ String

The request ID associated with the response.

Returns:

  • (String)


46
47
48
# File 'lib/ferrum/network/response.rb', line 46

def id
  @params["requestId"]
end

#inspect ⇒ String

Inspects the response object.

Returns:

  • (String)


187
188
189
# File 'lib/ferrum/network/response.rb', line 187

def inspect
  %(#<#{self.class} @params=#{@params.inspect} @response=#{@response.inspect}>)
end

#loaded? ⇒ Boolean

The response is fully loaded by the browser or not.

Returns:

  • (Boolean)


160
161
162
# File 'lib/ferrum/network/response.rb', line 160

def loaded?
  @loaded
end

#main? ⇒ Boolean

Whether this response is the main frame's current response.

Returns:

  • (Boolean)


153
154
155
# File 'lib/ferrum/network/response.rb', line 153

def main?
  @page.network.response == self
end

#redirect? ⇒ Boolean

Whether the response is a redirect.

Returns:

  • (Boolean)


167
168
169
# File 'lib/ferrum/network/response.rb', line 167

def redirect?
  params.key?("redirectResponse")
end

#status ⇒ Integer

The HTTP status of the response.

Returns:

  • (Integer)


64
65
66
# File 'lib/ferrum/network/response.rb', line 64

def status
  @response["status"]
end

#status_text ⇒ String

The HTTP status text.

Returns:

  • (String)


73
74
75
# File 'lib/ferrum/network/response.rb', line 73

def status_text
  @response["statusText"]
end

#type ⇒ String

The resource type of the response.

Returns:

  • (String)


100
101
102
# File 'lib/ferrum/network/response.rb', line 100

def type
  @params["type"]
end

#url ⇒ String

The URL of the response.

Returns:

  • (String)


55
56
57
# File 'lib/ferrum/network/response.rb', line 55

def url
  @response["url"]
end