Class: Anakin::Sessions

Inherits:
Object
  • Object
show all
Defined in:
lib/anakin/sessions.rb

Overview

Browser-session management. Saved sessions can be replayed via the /v1/browser-connect CDP endpoint to skip re-login flows.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Sessions

Returns a new instance of Sessions.



5
6
7
# File 'lib/anakin/sessions.rb', line 5

def initialize(client)
  @client = client
end

Instance Method Details

#create(name:, description: nil) ⇒ Object

Create an empty named session that can later be saved into.



16
17
18
19
20
# File 'lib/anakin/sessions.rb', line 16

def create(name:, description: nil)
  body = { name: name }
  body[:description] = description if description
  @client.send(:request_json, :post, '/browser-sessions', body: body)
end

#delete(session_id) ⇒ Object

Delete a saved session.



39
40
41
42
# File 'lib/anakin/sessions.rb', line 39

def delete(session_id)
  @client.send(:request_json, :delete, "/browser-sessions/#{session_id}")
  nil
end

#listObject

List all saved sessions for the API key.



10
11
12
13
# File 'lib/anakin/sessions.rb', line 10

def list
  resp = @client.send(:request_json, :get, '/browser-sessions')
  resp.is_a?(Hash) && resp.key?('sessions') ? resp['sessions'] : resp
end

#save(session_id, name: nil, description: nil) ⇒ Object

Save the current state of a CDP session by ID.



23
24
25
26
27
28
# File 'lib/anakin/sessions.rb', line 23

def save(session_id, name: nil, description: nil)
  body = {}
  body[:name] = name if name
  body[:description] = description if description
  @client.send(:request_json, :post, "/browser-sessions/#{session_id}/save", body: body)
end

#update(session_id, name: nil, description: nil) ⇒ Object

Update a saved session’s metadata.



31
32
33
34
35
36
# File 'lib/anakin/sessions.rb', line 31

def update(session_id, name: nil, description: nil)
  body = {}
  body[:name] = name if name
  body[:description] = description if description
  @client.send(:request_json, :put, "/browser-sessions/#{session_id}", body: body)
end