Class: Ferrum::Network::Response

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

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, params) ⇒ Response

Initializes the responses object.

Parameters:



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_sizeInteger?

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

#paramsHash{String => Object} (readonly) Also known as: to_h

The parsed JSON attributes for the [Network.Response](chromedevtools.github.io/devtools-protocol/1-3/Network/#type-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.



163
164
165
# File 'lib/ferrum/network/response.rb', line 163

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

#bodyString

The response body.

Returns:

  • (String)


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

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

#content_typeString?

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

#headersHash{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_sizeInteger

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

#idString

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

#inspectString

Inspects the response object.

Returns:

  • (String)


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

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)


145
146
147
# File 'lib/ferrum/network/response.rb', line 145

def loaded?
  @loaded
end

#main?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/ferrum/network/response.rb', line 138

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

#redirect?Boolean

Whether the response is a redirect.

Returns:

  • (Boolean)


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

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

#statusInteger

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_textString

The HTTP status text.

Returns:

  • (String)


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

def status_text
  @response["statusText"]
end

#typeString

The resource type of the response.

Returns:

  • (String)


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

def type
  @params["type"]
end

#urlString

The URL of the response.

Returns:

  • (String)


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

def url
  @response["url"]
end