Class: Puppeteer::HTTPResponse

Inherits:
Object
  • Object
show all
Includes:
IfPresent
Defined in:
lib/puppeteer/http_response.rb

Defined Under Namespace

Classes: InternalAccessor, Redirected, RemoteAddress, SecurityDetails

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IfPresent

#if_present

Constructor Details

#initialize(client, request, response_payload, extra_info) ⇒ HTTPResponse

Returns a new instance of HTTPResponse.

Parameters:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppeteer/http_response.rb', line 35

def initialize(client, request, response_payload, extra_info)
  @client = client
  @request = request

  @body_loaded_promise = resolvable_future
  @remote_address = RemoteAddress.new(
    ip: response_payload['remoteIPAddress'],
    port: response_payload['remotePort'],
  )

  @status_text = parse_štatus_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|
    @headers[key.downcase] = value
  end
  @security_details = if_present(response_payload['securityDetails']) do |security_payload|
    SecurityDetails.new(security_payload)
  end

  @internal = InternalAccessor.new(self)
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



65
66
67
# File 'lib/puppeteer/http_response.rb', line 65

def headers
  @headers
end

#internalObject (readonly)

Returns the value of attribute internal.



63
64
65
# File 'lib/puppeteer/http_response.rb', line 63

def internal
  @internal
end

#remote_addressObject (readonly)

Returns the value of attribute remote_address.



65
66
67
# File 'lib/puppeteer/http_response.rb', line 65

def remote_address
  @remote_address
end

#requestObject (readonly)

Returns the value of attribute request.



65
66
67
# File 'lib/puppeteer/http_response.rb', line 65

def request
  @request
end

#security_detailsObject (readonly)

Returns the value of attribute security_details.



65
66
67
# File 'lib/puppeteer/http_response.rb', line 65

def security_details
  @security_details
end

#statusObject (readonly)

Returns the value of attribute status.



65
66
67
# File 'lib/puppeteer/http_response.rb', line 65

def status
  @status
end

#status_textObject (readonly)

Returns the value of attribute status_text.



65
66
67
# File 'lib/puppeteer/http_response.rb', line 65

def status_text
  @status_text
end

#urlObject (readonly)

Returns the value of attribute url.



65
66
67
# File 'lib/puppeteer/http_response.rb', line 65

def url
  @url
end

Instance Method Details

#bufferObject



91
92
93
94
95
96
97
98
99
# File 'lib/puppeteer/http_response.rb', line 91

def buffer
  await @body_loaded_promise
  response = @client.send_message('Network.getResponseBody', requestId: @request.internal.request_id)
  if response['base64Encoded']
    Base64.decode64(response['body'])
  else
    response['body']
  end
end

#frameObject



119
120
121
# File 'lib/puppeteer/http_response.rb', line 119

def frame
  @request.frame
end

#from_cache?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/puppeteer/http_response.rb', line 111

def from_cache?
  @from_disk_cache || @request.internal.from_memory_cache?
end

#from_service_worker?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/puppeteer/http_response.rb', line 115

def from_service_worker?
  @from_service_worker
end

#inspectObject



67
68
69
70
71
72
73
# File 'lib/puppeteer/http_response.rb', line 67

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

#jsonObject

Parameters:

  • json (Hash)


107
108
109
# File 'lib/puppeteer/http_response.rb', line 107

def json
  JSON.parse(text)
end

#ok?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/puppeteer/http_response.rb', line 87

def ok?
  @status == 0 || (@status >= 200 && @status <= 299)
end

#textObject

Parameters:

  • text (String)


102
103
104
# File 'lib/puppeteer/http_response.rb', line 102

def text
  buffer
end