Class: Ferrum::SessionClient

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

Overview

A thin wrapper around Client that scopes commands and events to a single CDP session (a sessionId obtained via Target.attachToTarget). Targets connect through one of these rather than the top-level Client directly, so their commands/events don't leak into other sessions. Method calls not defined here are forwarded to the underlying Client.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, session_id) ⇒ SessionClient

Returns a new instance of SessionClient.



37
38
39
40
# File 'lib/ferrum/client.rb', line 37

def initialize(client, session_id)
  @client = client
  @session_id = session_id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ untyped

Delegates any method not defined on SessionClient to the underlying Client, e.g. subscribed?.

Returns:

  • (untyped)


119
120
121
# File 'lib/ferrum/client.rb', line 119

def method_missing(name, ...)
  @client.send(name, ...)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



18
19
20
# File 'lib/ferrum/client.rb', line 18

def client
  @client
end

#session_idObject (readonly)

Returns the value of attribute session_id.



18
19
20
# File 'lib/ferrum/client.rb', line 18

def session_id
  @session_id
end

Class Method Details

.event_name(event, session_id) ⇒ String

Builds the internal event key used to route an event to a specific session, joining the CDP event name with a session id (or leaving it session-less when session_id is nil).

Parameters:

  • event (String)

    The CDP event name, e.g. "Page.loadEventFired".

  • session_id (String, nil)

    The target session id, or nil for the browser-wide session.

Returns:

  • (String)


33
34
35
# File 'lib/ferrum/client.rb', line 33

def self.event_name(event, session_id)
  [event, session_id].compact.join("_")
end

Instance Method Details

#closevoid

This method returns an undefined value.

Removes all event callbacks registered for this session from the underlying client's subscriber.



129
130
131
# File 'lib/ferrum/client.rb', line 129

def close
  @client.subscriber.clear(session_id: session_id)
end

#command(method, async: false, timeout: nil, **params) ⇒ Boolean, Hash

Sends a CDP command scoped to this session.

Parameters:

  • method (String)

    The CDP method name, e.g. "Page.navigate".

  • async (Boolean) (defaults to: false)

    Whether to send the command without waiting for a response.

  • params (Hash)

    The command's parameters.

  • timeout (Numeric, nil) (defaults to: nil)

    How long to wait for this command's response, overriding Browser::Options#protocol_timeout. See Client#send_message.

Returns:

  • (Boolean, Hash)

    true when sent asynchronously, otherwise the command's result.



61
62
63
64
# File 'lib/ferrum/client.rb', line 61

def command(method, async: false, timeout: nil, **params)
  message = build_message(method, params)
  @client.send_message(message, async: async, timeout: timeout)
end

#off(event, id) ⇒ void

This method returns an undefined value.

Unsubscribes from a CDP event scoped to this session.

Parameters:

  • event (String)

    The CDP event name.

  • id (Integer)

    The subscription id returned by #on.



90
91
92
# File 'lib/ferrum/client.rb', line 90

def off(event, id)
  @client.off(event_name(event), id)
end

#on(event) ⇒ Integer

Subscribes to a CDP event scoped to this session.

Parameters:

  • event (String)

    The CDP event name.

Returns:

  • (Integer)

    The subscription id, used to unsubscribe via #off.



75
76
77
# File 'lib/ferrum/client.rb', line 75

def on(event, &)
  @client.on(event_name(event), &)
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Supports #method_missing delegation by reporting the underlying Client's methods as responded to.

Returns:

  • (Boolean)


109
110
111
# File 'lib/ferrum/client.rb', line 109

def respond_to_missing?(name, include_private)
  @client.respond_to?(name, include_private)
end

#subscribed?(event) ⇒ Boolean

Whether there's at least one callback registered for the event, scoped to this session.

Parameters:

  • event (String)

    The CDP event name.

Returns:

  • (Boolean)


101
102
103
# File 'lib/ferrum/client.rb', line 101

def subscribed?(event)
  @client.subscribed?(event_name(event))
end