module RequestHelper
Wraps the HTTP client to provide a consistent interface.
Definitions
ELEMENT_KEY = "element-6066-11e4-a52e-4f735466cecf"
The web element identifier is the string constant "element-6066-11e4-a52e-4f735466cecf".
CONTENT_TYPE = "application/json"
The content type for requests and responses.
GET_HEADERS
Headers to send with GET requests.
Implementation
GET_HEADERS = [
["user-agent", "Async::WebDriver/#{VERSION}"],
["accept", CONTENT_TYPE],
].freeze
POST_HEADERS
Headers to send with POST requests.
Implementation
POST_HEADERS = GET_HEADERS + [
["content-type", "#{CONTENT_TYPE}; charset=UTF-8"],
].freeze
def request_path(path = nil)
The path used for making requests to the web driver bridge.
Signature
-
parameter
path
String | Nil
The path to append to the request path.
-
returns
String
The path used for making requests to the web driver bridge.
Implementation
def request_path(path = nil)
if path
"/#{path}"
else
"/"
end
end
def unwrap_object(value)
Unwrap JSON objects into their corresponding Ruby objects.
If the value is a Hash and represents an element, then it will be unwrapped into an class Async::WebDriver::Element
.
Signature
-
parameter
value
Hash | Array | Object
The value to unwrap.
-
returns
Object
The unwrapped value.
Implementation
def unwrap_object(value)
if value.is_a?(Hash) and value.key?(ELEMENT_KEY)
Element.new(self.session, value[ELEMENT_KEY])
else
value
end
end
def unwrap_objects(value)
Used by JSON.load
to unwrap objects.
Implementation
def unwrap_objects(value)
case value
when Hash
value.transform_values!(&method(:unwrap_object))
when Array
value.map!(&method(:unwrap_object))
end
end
def extract_value(reply)
Extract the value from the reply.
If the value is a Hash and represents an error, then it will be raised as an appropriate subclass of class Async::WebDriver::Error
.
Signature
-
parameter
reply
Hash
The reply from the server.
-
returns
Object
The value of the reply.
Implementation
def extract_value(reply)
value = reply["value"]
if value.is_a?(Hash) and error = value["error"]
raise ERROR_CODES.fetch(error, Error), value["message"]
end
if block_given?
return yield(reply)
else
return value
end
end
def get(path)
Make a GET request to the bridge and extract the value.
Signature
-
parameter
path
String | Nil
The path to append to the request path.
-
returns
Object | Nil
The value of the reply.
Implementation
def get(path)
Console.debug(self, "GET #{request_path(path)}")
response = @delegate.get(request_path(path), GET_HEADERS)
reply = JSON.load(response.read, self.method(:unwrap_objects))
return extract_value(reply)
end
def post(path, arguments = {}, &block)
Make a POST request to the bridge and extract the value.
Signature
-
parameter
path
String | Nil
The path to append to the request path.
-
parameter
arguments
Hash | Nil
The arguments to send with the request.
-
returns
Object | Nil
The value of the reply.
Implementation
def post(path, arguments = {}, &block)
Console.debug(self, "POST #{request_path(path)}", arguments: arguments)
response = @delegate.post(request_path(path), POST_HEADERS, arguments ? JSON.dump(arguments) : nil)
reply = JSON.load(response.read, self.method(:unwrap_objects))
return extract_value(reply, &block)
end
def delete(path = nil)
Make a DELETE request to the bridge and extract the value.
Signature
-
parameter
path
String | Nil
The path to append to the request path.
-
returns
Object | Nil
The value of the reply, if any.
Implementation
def delete(path = nil)
Console.debug(self, "DELETE #{request_path(path)}")
response = @delegate.delete(request_path(path), POST_HEADERS)
reply = JSON.load(response.read, self.method(:unwrap_objects))
return extract_value(reply)
end