Class: Puppeteer::Response

Inherits:
Object
  • Object
show all
Includes:
IfPresent
Defined in:
lib/puppeteer/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) ⇒ Response

Returns a new instance of Response.

Parameters:



34
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
# File 'lib/puppeteer/response.rb', line 34

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

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

  @status = response_payload['status']
  @status_text = response_payload['statusText']
  @url = request.url
  @from_disk_cache = !!response_payload['fromDiskCache']
  @from_service_worker = !!response_payload['fromServiceWorker']

  @headers = {}
  response_payload['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.



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

def headers
  @headers
end

#internalObject (readonly)

Returns the value of attribute internal.



61
62
63
# File 'lib/puppeteer/response.rb', line 61

def internal
  @internal
end

#remote_addressObject (readonly)

Returns the value of attribute remote_address.



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

def remote_address
  @remote_address
end

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

#security_detailsObject (readonly)

Returns the value of attribute security_details.



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

def security_details
  @security_details
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

#status_textObject (readonly)

Returns the value of attribute status_text.



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

def status_text
  @status_text
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#bufferObject



70
71
72
73
74
75
76
77
78
# File 'lib/puppeteer/response.rb', line 70

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



98
99
100
# File 'lib/puppeteer/response.rb', line 98

def frame
  @request.frame
end

#from_cache?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/puppeteer/response.rb', line 90

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

#from_service_worker?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/puppeteer/response.rb', line 94

def from_service_worker?
  @from_service_worker
end

#jsonObject

Parameters:

  • json (Hash)


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

def json
  JSON.parse(text)
end

#ok?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/puppeteer/response.rb', line 66

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

#textObject

Parameters:

  • text (String)


81
82
83
# File 'lib/puppeteer/response.rb', line 81

def text
  buffer
end