Class: Bidi2pdf::Bidi::UserContext
- Inherits:
-
Object
- Object
- Bidi2pdf::Bidi::UserContext
- Defined in:
- lib/bidi2pdf/bidi/user_context.rb
Overview
Represents a user context for managing browser interactions and cookies using the Bidi2pdf library. This class provides methods for creating user contexts, setting cookies, and creating browser windows.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
The WebSocket client.
Instance Method Summary collapse
-
#close ⇒ nil
Closes the user context.
-
#context_id ⇒ String
Retrieves the user context ID, creating it if it does not exist.
-
#create_browser_window ⇒ BrowserTab
Creates a new browser window in the user context.
-
#initialize(client) ⇒ UserContext
constructor
Initializes a new user context.
-
#set_cookie(name:, value:, domain:, source_origin:, path: "/", secure: true, http_only: false, same_site: "strict", ttl: 30) ⇒ Object
Sets a cookie in the user context.
Constructor Details
#initialize(client) ⇒ UserContext
Initializes a new user context.
33 34 35 36 |
# File 'lib/bidi2pdf/bidi/user_context.rb', line 33 def initialize(client) @client = client @context_id = nil end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns The WebSocket client.
28 29 30 |
# File 'lib/bidi2pdf/bidi/user_context.rb', line 28 def client @client end |
Instance Method Details
#close ⇒ nil
Closes the user context.
This method removes the user context from the browser, effectively cleaning up any associated resources. If the user context does not exist, the method does nothing.
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/bidi2pdf/bidi/user_context.rb', line 116 def close return unless context_id res = client.send_cmd_and_wait(Bidi2pdf::Bidi::Commands::BrowserRemoveUserContext.new(user_context_id: context_id)) do |response| raise "Error removing user context: #{response.inspect}" if response["error"] response["result"] end Bidi2pdf.logger.debug "User context deleted: #{res.inspect}" @context_id = nil end |
#context_id ⇒ String
Retrieves the user context ID, creating it if it does not exist.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/bidi2pdf/bidi/user_context.rb', line 42 def context_id @context_id ||= begin res = client.send_cmd_and_wait(Bidi2pdf::Bidi::Commands::BrowserCreateUserContext.new) do |response| raise "Error creating user context: #{response.inspect}" if response["error"] response["result"]["userContext"] end Bidi2pdf.logger.debug "User context created: #{res.inspect}" res end end |
#create_browser_window ⇒ BrowserTab
Creates a new browser window in the user context.
99 100 101 102 103 104 105 106 107 |
# File 'lib/bidi2pdf/bidi/user_context.rb', line 99 def create_browser_window cmd = Bidi2pdf::Bidi::Commands::CreateWindow.new(user_context_id: context_id) client.send_cmd_and_wait(cmd) do |response| browsing_context_id = response["result"]["context"] BrowserTab.new(client, browsing_context_id, context_id) end end |
#set_cookie(name:, value:, domain:, source_origin:, path: "/", secure: true, http_only: false, same_site: "strict", ttl: 30) ⇒ Object
Sets a cookie in the user context.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bidi2pdf/bidi/user_context.rb', line 67 def ( name:, value:, domain:, source_origin:, path: "/", secure: true, http_only: false, same_site: "strict", ttl: 30 ) cmd = Bidi2pdf::Bidi::Commands::SetUsercontextCookie.new( user_context_id: context_id, source_origin: source_origin, name: name, value: value, domain: domain, path: path, secure: secure, http_only: http_only, same_site: same_site, ttl: ttl ) client.send_cmd_and_wait(cmd) do |response| Bidi2pdf.logger.debug "Cookie set: #{response.inspect}" end end |