Module: Async::WebDriver::RequestHelper
Overview
Wraps the HTTP client to provide a consistent interface.
Constant Summary collapse
- ELEMENT_KEY =
The web element identifier is the string constant “element-6066-11e4-a52e-4f735466cecf”.
"element-6066-11e4-a52e-4f735466cecf"- CONTENT_TYPE =
The content type for requests and responses.
"application/json"- GET_HEADERS =
Headers to send with GET requests.
[ ["user-agent", "Async::WebDriver/#{VERSION}"], ["accept", CONTENT_TYPE], ].freeze
- POST_HEADERS =
Headers to send with POST requests.
GET_HEADERS + [ ["content-type", "#{CONTENT_TYPE}; charset=UTF-8"], ].freeze
Instance Method Summary collapse
-
#delete(path = nil) ⇒ Object
Make a DELETE request to the bridge and extract the value.
-
#extract_value(reply) ⇒ Object
Extract the value from the reply.
-
#get(path) ⇒ Object
Make a GET request to the bridge and extract the value.
-
#post(path, arguments = {}, &block) ⇒ Object
Make a POST request to the bridge and extract the value.
-
#request_path(path = nil) ⇒ Object
The path used for making requests to the web driver bridge.
-
#unwrap_object(value) ⇒ Object
Unwrap JSON objects into their corresponding Ruby objects.
-
#unwrap_objects(value) ⇒ Object
Used by ‘JSON.load` to unwrap objects.
Instance Method Details
#delete(path = nil) ⇒ Object
Make a DELETE request to the bridge and extract the value.
111 112 113 114 115 116 117 |
# File 'lib/async/webdriver/request_helper.rb', line 111 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 |
#extract_value(reply) ⇒ Object
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 Error.
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/async/webdriver/request_helper.rb', line 71 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 |
#get(path) ⇒ Object
Make a GET request to the bridge and extract the value.
88 89 90 91 92 93 94 |
# File 'lib/async/webdriver/request_helper.rb', line 88 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 |
#post(path, arguments = {}, &block) ⇒ Object
Make a POST request to the bridge and extract the value.
100 101 102 103 104 105 106 |
# File 'lib/async/webdriver/request_helper.rb', line 100 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 |
#request_path(path = nil) ⇒ Object
The path used for making requests to the web driver bridge.
33 34 35 36 37 38 39 |
# File 'lib/async/webdriver/request_helper.rb', line 33 def request_path(path = nil) if path "/#{path}" else "/" end end |
#unwrap_object(value) ⇒ Object
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 Element.
47 48 49 50 51 52 53 |
# File 'lib/async/webdriver/request_helper.rb', line 47 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 |
#unwrap_objects(value) ⇒ Object
Used by ‘JSON.load` to unwrap objects.
56 57 58 59 60 61 62 63 |
# File 'lib/async/webdriver/request_helper.rb', line 56 def unwrap_objects(value) case value when Hash value.transform_values!(&method(:unwrap_object)) when Array value.map!(&method(:unwrap_object)) end end |