Class: Ferrum::Contexts
- Inherits:
-
Object
- Object
- Ferrum::Contexts
- Includes:
- Enumerable
- Defined in:
- lib/ferrum/contexts.rb
Constant Summary collapse
- ALLOWED_TARGET_TYPES =
%w[page iframe].freeze
Instance Attribute Summary collapse
-
#contexts ⇒ Object
readonly
Returns the value of attribute contexts.
Instance Method Summary collapse
- #[](id) ⇒ Object
- #close_connections ⇒ Object
- #create(**options) ⇒ Object
- #default_context ⇒ Object
- #dispose(context_id) ⇒ Object
- #each(&block) ⇒ Object
- #find_by(target_id:) ⇒ Object
-
#initialize(client) ⇒ Contexts
constructor
A new instance of Contexts.
- #reset ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(client) ⇒ Contexts
Returns a new instance of Contexts.
13 14 15 16 17 18 19 |
# File 'lib/ferrum/contexts.rb', line 13 def initialize(client) @contexts = Concurrent::Map.new @client = client subscribe auto_attach discover end |
Instance Attribute Details
#contexts ⇒ Object (readonly)
Returns the value of attribute contexts.
11 12 13 |
# File 'lib/ferrum/contexts.rb', line 11 def contexts @contexts end |
Instance Method Details
#[](id) ⇒ Object
31 32 33 |
# File 'lib/ferrum/contexts.rb', line 31 def [](id) @contexts[id] end |
#close_connections ⇒ Object
57 58 59 |
# File 'lib/ferrum/contexts.rb', line 57 def close_connections @contexts.each_value(&:close_targets_connection) end |
#create(**options) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/ferrum/contexts.rb', line 41 def create(**) response = @client.command("Target.createBrowserContext", **) context_id = response["browserContextId"] context = Context.new(@client, self, context_id) @contexts[context_id] = context context end |
#default_context ⇒ Object
21 22 23 |
# File 'lib/ferrum/contexts.rb', line 21 def default_context @default_context ||= create end |
#dispose(context_id) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/ferrum/contexts.rb', line 49 def dispose(context_id) context = @contexts[context_id] context.close_targets_connection @client.command("Target.disposeBrowserContext", browserContextId: context.id) @contexts.delete(context_id) true end |
#each(&block) ⇒ Object
25 26 27 28 29 |
# File 'lib/ferrum/contexts.rb', line 25 def each(&block) return enum_for(__method__) unless block_given? @contexts.each(&block) end |
#find_by(target_id:) ⇒ Object
35 36 37 38 39 |
# File 'lib/ferrum/contexts.rb', line 35 def find_by(target_id:) context = nil @contexts.each_value { |c| context = c if c.target?(target_id) } context end |
#reset ⇒ Object
61 62 63 64 |
# File 'lib/ferrum/contexts.rb', line 61 def reset @default_context = nil @contexts.each_key { |id| dispose(id) } end |
#size ⇒ Object
66 67 68 |
# File 'lib/ferrum/contexts.rb', line 66 def size @contexts.size end |