Class: Ferrum::Network::Response

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

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



39
40
41
42
43
# File 'lib/ferrum/network/response.rb', line 39

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

Instance Attribute Details

#body_size ⇒ Integer?

Note:

The writer is a private API, Ferrum sets it.

The response body size.

Returns:

  • (Integer, nil)


15
16
17
# File 'lib/ferrum/network/response.rb', line 15

def body_size
  @body_size
end

#loaded=(value) ⇒ Boolean (writeonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The response is fully loaded by the browser.

Returns:

  • (Boolean)


28
29
30
# File 'lib/ferrum/network/response.rb', line 28

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})


21
22
23
# File 'lib/ferrum/network/response.rb', line 21

def params
  @params
end

Instance Method Details

#==(other) ⇒ Boolean

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

Parameters:

  • other (Object)

Returns:

  • (Boolean) —

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



206
207
208
# File 'lib/ferrum/network/response.rb', line 206

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

#body ⇒ String?

The response body.

Returns:

  • (String, nil)

Raises:



170
171
172
173
174
# File 'lib/ferrum/network/response.rb', line 170

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

#body! ⇒ String

The response body.

Returns:

  • (String)

Raises:



155
156
157
158
159
160
161
# File 'lib/ferrum/network/response.rb', line 155

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)


113
114
115
# File 'lib/ferrum/network/response.rb', line 113

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

#from_disk_cache? ⇒ Boolean

Whether the response was served from the browser's disk cache.

Returns:

  • (Boolean)


122
123
124
# File 'lib/ferrum/network/response.rb', line 122

def from_disk_cache?
  @response["fromDiskCache"] == true
end

#from_service_worker? ⇒ Boolean

Whether a service worker answered the request instead of the network.

Returns:

  • (Boolean)


131
132
133
# File 'lib/ferrum/network/response.rb', line 131

def from_service_worker?
  @response["fromServiceWorker"] == true
end

#headers ⇒ Hash{String => String}

The headers of the response.

Returns:

  • (Hash{String => String})


86
87
88
# File 'lib/ferrum/network/response.rb', line 86

def headers
  @response["headers"]
end

#headers_size ⇒ Integer

The total size in bytes of the response.

Returns:

  • (Integer)


95
96
97
# File 'lib/ferrum/network/response.rb', line 95

def headers_size
  @response["encodedDataLength"]
end

#id ⇒ String

The request ID associated with the response.

Returns:

  • (String)


50
51
52
# File 'lib/ferrum/network/response.rb', line 50

def id
  @params["requestId"]
end

#inspect ⇒ String

Inspects the response object.

Returns:

  • (String)


215
216
217
# File 'lib/ferrum/network/response.rb', line 215

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)


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

def loaded?
  @loaded
end

#main? ⇒ Boolean

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

Returns:

  • (Boolean)


181
182
183
# File 'lib/ferrum/network/response.rb', line 181

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

#redirect? ⇒ Boolean

Whether the response is a redirect.

Returns:

  • (Boolean)


195
196
197
# File 'lib/ferrum/network/response.rb', line 195

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

#resource_type ⇒ String

The response's resource type, e.g. "Document", "Script" or "XHR".

Returns:

  • (String)


104
105
106
# File 'lib/ferrum/network/response.rb', line 104

def resource_type
  @params["type"]
end

#status ⇒ Integer

The HTTP status of the response.

Returns:

  • (Integer)


68
69
70
# File 'lib/ferrum/network/response.rb', line 68

def status
  @response["status"]
end

#status_text ⇒ String

The HTTP status text.

Returns:

  • (String)


77
78
79
# File 'lib/ferrum/network/response.rb', line 77

def status_text
  @response["statusText"]
end

#url ⇒ String

The URL of the response.

Returns:

  • (String)


59
60
61
# File 'lib/ferrum/network/response.rb', line 59

def url
  @response["url"]
end