Class: Async::Webdriver::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/async/webdriver/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(endpoint:) ⇒ Connection

Returns a new instance of Connection.



9
10
11
12
# File 'lib/async/webdriver/connection.rb', line 9

def initialize(endpoint:)
  @url = endpoint
  @internet = Async::HTTP::Internet.new
end

Instance Method Details

#call(method:, path: nil, headers: [], body: nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/async/webdriver/connection.rb', line 14

def call(method:, path:nil, headers:[], body:nil)
  body_array = case body
  when Hash
    [body.to_json]
  end

  path_or_url = if path
    "#{@url}/#{path}"
  else
    @url
  end

  task = Async.run do |t|
    r = @internet.call method, path_or_url, headers, body_array

    body = begin
      JSON.parse r.read
    rescue JSON::ParserError => ex
      p ex
      exit 1
    end

    @internet.close

    status = body.dig "status"
    if status == 0
      # POST /session has different response structure than other calls
      if method == "post" && path == "session"
        {
          "id" => body.dig("sessionId"),
          "capabilities" => body.dig("value")
        }
      else # everything else works like this
        body.dig "value"
      end
    else
      p body
      raise "Error: #{status} - #{body.dig("value", "message")}"
    end
  end

  task.wait
end