Class: Ferrum::SessionClient
- Inherits:
-
Object
- Object
- Ferrum::SessionClient
- 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
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
Class Method Summary collapse
-
.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_idisnil).
Instance Method Summary collapse
-
#close ⇒ void
Removes all event callbacks registered for this session from the underlying client's subscriber.
-
#command(method, async: false, timeout: nil, **params) ⇒ Boolean, Hash
Sends a CDP command scoped to this session.
-
#initialize(client, session_id) ⇒ SessionClient
constructor
A new instance of SessionClient.
-
#method_missing(name) ⇒ untyped
Delegates any method not defined on
SessionClientto the underlying Client, e.g. -
#off(event, id) ⇒ void
Unsubscribes from a CDP event scoped to this session.
-
#on(event) ⇒ Integer
Subscribes to a CDP event scoped to this session.
-
#respond_to_missing?(name, include_private) ⇒ Boolean
Supports #method_missing delegation by reporting the underlying Client's methods as responded to.
-
#subscribed?(event) ⇒ Boolean
Whether there's at least one callback registered for the event, scoped to this session.
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?.
119 120 121 |
# File 'lib/ferrum/client.rb', line 119 def method_missing(name, ...) @client.send(name, ...) end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
18 19 20 |
# File 'lib/ferrum/client.rb', line 18 def client @client end |
#session_id ⇒ Object (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).
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
#close ⇒ void
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.
61 62 63 64 |
# File 'lib/ferrum/client.rb', line 61 def command(method, async: false, timeout: nil, **params) = (method, params) @client.(, async: async, timeout: timeout) end |
#off(event, id) ⇒ void
This method returns an undefined value.
Unsubscribes from a CDP event scoped to this session.
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.
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.
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.
101 102 103 |
# File 'lib/ferrum/client.rb', line 101 def subscribed?(event) @client.subscribed?(event_name(event)) end |