Class: Puppeteer::HTTPResponse
- Inherits:
-
Object
- Object
- Puppeteer::HTTPResponse
- Includes:
- IfPresent
- Defined in:
- lib/puppeteer/http_response.rb,
sig/puppeteer/http_response.rbs
Defined Under Namespace
Classes: InternalAccessor, Redirected, RemoteAddress, SecurityDetails
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#internal ⇒ Object
readonly
Returns the value of attribute internal.
-
#remote_address ⇒ Object
readonly
Returns the value of attribute remote_address.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#security_details ⇒ Object
readonly
Returns the value of attribute security_details.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#status_text ⇒ Object
readonly
Returns the value of attribute status_text.
-
#timing ⇒ Object
readonly
Returns the value of attribute timing.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #buffer ⇒ String
- #frame ⇒ Puppeteer::Frame
- #from_cache? ⇒ Boolean
- #from_service_worker? ⇒ Boolean
- #initialize(client, request, response_payload, extra_info) ⇒ Object constructor
- #inspect ⇒ Object
- #json ⇒ Hash[untyped, untyped]
- #ok? ⇒ Boolean
- #text ⇒ String
Methods included from IfPresent
Constructor Details
#initialize(client, request, response_payload, extra_info) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/puppeteer/http_response.rb', line 38 def initialize(client, request, response_payload, extra_info) @client = client @request = request @body_loaded_promise = Async::Promise.new @remote_address = RemoteAddress.new( ip: response_payload['remoteIPAddress'], port: response_payload['remotePort'], ) @status_text = parse_status_text_from_extra_info(extra_info) || response_payload['statusText'] @url = request.url @from_disk_cache = !!response_payload['fromDiskCache'] @from_service_worker = !!response_payload['fromServiceWorker'] @status = extra_info ? extra_info['statusCode'] : response_payload['status'] @headers = {} headers = extra_info ? extra_info['headers'] : response_payload['headers'] headers.each do |key, value| header_name = key.downcase @headers[header_name] = normalize_header_value(header_name, value) end @security_details = if_present(response_payload['securityDetails']) do |security_payload| SecurityDetails.new(security_payload) end @timing = response_payload['timing'] @internal = InternalAccessor.new(self) end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
70 71 72 |
# File 'lib/puppeteer/http_response.rb', line 70 def headers @headers end |
#internal ⇒ Object (readonly)
Returns the value of attribute internal.
68 69 70 |
# File 'lib/puppeteer/http_response.rb', line 68 def internal @internal end |
#remote_address ⇒ Object (readonly)
Returns the value of attribute remote_address.
70 71 72 |
# File 'lib/puppeteer/http_response.rb', line 70 def remote_address @remote_address end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
70 71 72 |
# File 'lib/puppeteer/http_response.rb', line 70 def request @request end |
#security_details ⇒ Object (readonly)
Returns the value of attribute security_details.
70 71 72 |
# File 'lib/puppeteer/http_response.rb', line 70 def security_details @security_details end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
70 71 72 |
# File 'lib/puppeteer/http_response.rb', line 70 def status @status end |
#status_text ⇒ Object (readonly)
Returns the value of attribute status_text.
70 71 72 |
# File 'lib/puppeteer/http_response.rb', line 70 def status_text @status_text end |
#timing ⇒ Object (readonly)
Returns the value of attribute timing.
70 71 72 |
# File 'lib/puppeteer/http_response.rb', line 70 def timing @timing end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
70 71 72 |
# File 'lib/puppeteer/http_response.rb', line 70 def url @url end |
Instance Method Details
#buffer ⇒ String
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/puppeteer/http_response.rb', line 110 def buffer @body_loaded_promise.wait response = @request.client.('Network.getResponseBody', requestId: @request.internal.request_id) if response['base64Encoded'] Base64.decode64(response['body']) else response['body'] end rescue Puppeteer::Connection::ProtocolError => err if err..include?('No resource with given identifier found') raise Puppeteer::Error.new( 'Could not load response body for this request. This might happen if the request is a preflight request.', ) end raise end |
#frame ⇒ Puppeteer::Frame
155 156 157 |
# File 'lib/puppeteer/http_response.rb', line 155 def frame @request.frame end |
#from_cache? ⇒ Boolean
145 146 147 |
# File 'lib/puppeteer/http_response.rb', line 145 def from_cache? @from_disk_cache || @request.internal.from_memory_cache? end |
#from_service_worker? ⇒ Boolean
150 151 152 |
# File 'lib/puppeteer/http_response.rb', line 150 def from_service_worker? @from_service_worker end |
#inspect ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/puppeteer/http_response.rb', line 85 def inspect values = %i[remote_address url status status_text headers security_details request].map do |sym| value = instance_variable_get(:"@#{sym}") "@#{sym}=#{value}" end "#<Puppeteer::HTTPRequest #{values.join(' ')}>" end |
#json ⇒ Hash[untyped, untyped]
140 141 142 |
# File 'lib/puppeteer/http_response.rb', line 140 def json JSON.parse(text) end |
#ok? ⇒ Boolean
105 106 107 |
# File 'lib/puppeteer/http_response.rb', line 105 def ok? @status == 0 || (@status >= 200 && @status <= 299) end |
#text ⇒ String
129 130 131 132 133 134 135 136 |
# File 'lib/puppeteer/http_response.rb', line 129 def text content = buffer content = content.dup.force_encoding('UTF-8') unless content.valid_encoding? raise Puppeteer::Error.new('Could not decode response body as UTF-8') end content end |