Class: Ferrum::Client
  
  
  
  
  
    - Inherits:
 
    - 
      Object
      
        
        show all
      
    
 
  
  
  
  
  
      - Extended by:
 
      - Forwardable
 
  
  
  
  
  
  
  
  
    - Defined in:
 
    - lib/ferrum/client.rb,
  lib/ferrum/client/subscriber.rb,
 lib/ferrum/client/web_socket.rb
 
  
  
 
Defined Under Namespace
  
    
  
    
      Classes: Subscriber, WebSocket
    
  
  Instance Attribute Summary collapse
  
  
    
      Instance Method Summary
      collapse
    
    
  
  
  Constructor Details
  
    
  
  
    #initialize(ws_url, options)  ⇒ Client 
  
  
  
  
    
Returns a new instance of Client.
   
 
  
  
    
      
68
69
70
71
72
73
74
75
76
77 
     | 
    
      # File 'lib/ferrum/client.rb', line 68
def initialize(ws_url, options)
  @command_id = 0
  @ws_url = ws_url
  @options = options
  @pendings = Concurrent::Hash.new
  @ws = WebSocket.new(ws_url, options.ws_max_receive_size, options.logger)
  @subscriber = Subscriber.new
  start
end
 
     | 
  
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #options  ⇒ Object  
  
  
  
  
    
Returns the value of attribute options.
   
 
  
  
    
      
66
67
68 
     | 
    
      # File 'lib/ferrum/client.rb', line 66
def options
  @options
end
 
     | 
  
 
    
      
      
      
  
  
    #subscriber  ⇒ Object  
  
  
  
  
    
Returns the value of attribute subscriber.
   
 
  
  
    
      
66
67
68 
     | 
    
      # File 'lib/ferrum/client.rb', line 66
def subscriber
  @subscriber
end
 
     | 
  
 
    
      
      
      
  
  
    #ws_url  ⇒ Object  
  
  
  
  
    
Returns the value of attribute ws_url.
   
 
  
  
    
      
66
67
68 
     | 
    
      # File 'lib/ferrum/client.rb', line 66
def ws_url
  @ws_url
end
 
     | 
  
 
    
   
  
    Instance Method Details
    
      
  
  
    #build_message(method, params)  ⇒ Object 
  
  
  
  
    
      
135
136
137 
     | 
    
      # File 'lib/ferrum/client.rb', line 135
def build_message(method, params)
  { method: method, params: params }.merge(id: next_command_id)
end
     | 
  
 
    
      
  
  
    #close  ⇒ Object 
  
  
  
  
    
      
120
121
122
123
124
125
126 
     | 
    
      # File 'lib/ferrum/client.rb', line 120
def close
  @ws.close
  
  @pendings.clear
  @thread.kill unless @thread.join(1)
  @subscriber.close
end
 
     | 
  
 
    
      
  
  
    #command(method, async: false, **params)  ⇒ Object 
  
  
  
  
    
      
79
80
81
82 
     | 
    
      # File 'lib/ferrum/client.rb', line 79
def command(method, async: false, **params)
  message = build_message(method, params)
  send_message(message, async: async)
end
 
     | 
  
 
    
      
  
  
    #inspect  ⇒ Object 
  
  
  
  
    
      
128
129
130
131
132
133 
     | 
    
      # File 'lib/ferrum/client.rb', line 128
def inspect
  "#<#{self.class} " \
    "@command_id=#{@command_id.inspect} " \
    "@pendings=#{@pendings.inspect} " \
    "@ws=#{@ws.inspect}>"
end
     | 
  
 
    
      
  
  
    #off(event, id)  ⇒ Object 
  
  
  
  
    
      
108
109
110 
     | 
    
      # File 'lib/ferrum/client.rb', line 108
def off(event, id)
  @subscriber.off(event, id)
end
 
     | 
  
 
    
      
  
  
    #on(event, &block)  ⇒ Object 
  
  
  
  
    
      
104
105
106 
     | 
    
      # File 'lib/ferrum/client.rb', line 104
def on(event, &block)
  @subscriber.on(event, &block)
end
 
     | 
  
 
    
      
  
  
    #send_message(message, async:)  ⇒ Object 
  
  
  
  
    
      
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 
     | 
    
      # File 'lib/ferrum/client.rb', line 84
def send_message(message, async:)
  if async
    @ws.send_message(message)
    true
  else
    pending = Concurrent::IVar.new
    @pendings[message[:id]] = pending
    @ws.send_message(message)
    data = pending.value!(timeout)
    @pendings.delete(message[:id])
    raise DeadBrowserError if data.nil? && @ws.messages.closed?
    raise TimeoutError unless data
    error, response = data.values_at("error", "result")
    raise_browser_error(error) if error
    response
  end
end
     | 
  
 
    
      
  
  
    #session(session_id)  ⇒ Object 
  
  
  
  
    
      
116
117
118 
     | 
    
      # File 'lib/ferrum/client.rb', line 116
def session(session_id)
  SessionClient.new(self, session_id)
end
 
     | 
  
 
    
      
  
  
    #subscribed?(event)  ⇒ Boolean 
  
  
  
  
    
      
112
113
114 
     | 
    
      # File 'lib/ferrum/client.rb', line 112
def subscribed?(event)
  @subscriber.subscribed?(event)
end
 
     |