Sha256: 4443d0352d509bb7ea0cd7ac02b373047e19339efe9eb7b27a2c2f2a2ca3bf12

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

module Webdriver
  class Connection
    def initialize(endpoint)
      uri = URI(endpoint)
      @http = Net::HTTP.new uri.hostname, uri.port
    end

    def get(path, headers={})
      call :get, path, headers
    end
    def post(path, headers={}, body=nil)
      call :post, path, headers, body
    end
    def post(path, headers={}, body=nil)
      call :post, path, headers, body
    end

    def call(method, path, headers={}, body={})
      path = "/#{path}"
      body_json = body.to_json if body
      Webdriver.debug [method, path, headers, body_json]

      response = case method
      when :get
        @http.get path
      when :post
        @http.post path, body_json
      when :delete
        @http.delete path, body_json
      else
        raise "err"
      end

      response_body = JSON.parse response.body

      status = response_body.dig "status"
      session_id = response_body.dig "sessionId"
      value = response_body.dig "value"

      case status
      when 0
        # POST /session has different response structure than other calls
        if method == :post && path == "/session"
          value.merge("id" => session_id)
        else # everything else works like this
          value
        end
      else
        if method == :get && path == "/status"
          value
        else
          pp [status, response_body]
          raise "err"
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
webdriver-0.5.0 lib/webdriver/connection.rb
webdriver-0.4.0 lib/webdriver/connection.rb
webdriver-0.3.0 lib/webdriver/connection.rb
webdriver-0.2.0 lib/webdriver/connection.rb
webdriver-0.1.2 lib/webdriver/connection.rb