Class: Kookaburra::APIDriver
- Inherits:
-
Object
- Object
- Kookaburra::APIDriver
- Defined in:
- lib/kookaburra/api_driver.rb
Overview
Communicate with a Web Services API
You will create a subclass of APIDriver in your testing implementation to be used with you subclass of GivenDriver. While the GivenDriver implements the "business domain" DSL for setting up your application state, the APIDriver maps discreet operations to your application's web service API and can (optionally) handle encoding input data and decoding response bodies to and from your preferred serialization format.
Class Method Summary collapse
-
.decode_with {|data| ... } ⇒ Object
Deserialize response body.
-
.encode_with {|data| ... } ⇒ Object
Serializes input data.
-
.header(name, value) ⇒ Object
Set custom HTTP headers.
Instance Method Summary collapse
-
#delete(path, data = nil, headers = {}) ⇒ Object
Convenience method to make a DELETE request.
-
#get(path, data = nil, headers = {}) ⇒ Object
Convenience method to make a GET request.
-
#initialize(configuration, http_client = RestClient) ⇒ APIDriver
constructor
Create a new APIDriver instance.
-
#post(path, data = nil, headers = {}) ⇒ Object
Convenience method to make a POST request.
-
#put(path, data = nil, headers = {}) ⇒ Object
Convenience method to make a PUT request.
-
#request(method, path, data, headers) ⇒ Object
Make an HTTP request.
Constructor Details
#initialize(configuration, http_client = RestClient) ⇒ APIDriver
Create a new Kookaburra::APIDriver instance
95 96 97 98 |
# File 'lib/kookaburra/api_driver.rb', line 95 def initialize(configuration, http_client = RestClient) @configuration = configuration @http_client = http_client end |
Class Method Details
.decode_with {|data| ... } ⇒ Object
Deserialize response body
If specified, the response bodies of all requests made using this Kookaburra::APIDriver will be processed through this function prior to being returned.
57 58 59 60 61 |
# File 'lib/kookaburra/api_driver.rb', line 57 def decode_with(&block) define_method(:decode) do |data| block.call(data) end end |
.encode_with {|data| ... } ⇒ Object
33 34 35 36 37 38 |
# File 'lib/kookaburra/api_driver.rb', line 33 def encode_with(&block) define_method(:encode) do |data| return if data.nil? block.call(data) end end |
.header(name, value) ⇒ Object
Set custom HTTP headers
Can be called multiple times to set HTTP headers that will be provided with every request made by the Kookaburra::APIDriver.
77 78 79 |
# File 'lib/kookaburra/api_driver.rb', line 77 def header(name, value) headers[name] = value end |
Instance Method Details
#delete(path, data = nil, headers = {}) ⇒ Object
Convenience method to make a DELETE request
125 126 127 128 |
# File 'lib/kookaburra/api_driver.rb', line 125 def delete(path, data = nil, headers = {}) path = add_querystring_to_path(path, data) request(:delete, path, nil, headers) end |
#get(path, data = nil, headers = {}) ⇒ Object
Convenience method to make a GET request
117 118 119 120 |
# File 'lib/kookaburra/api_driver.rb', line 117 def get(path, data = nil, headers = {}) path = add_querystring_to_path(path, data) request(:get, path, nil, headers) end |
#post(path, data = nil, headers = {}) ⇒ Object
Convenience method to make a POST request
103 104 105 |
# File 'lib/kookaburra/api_driver.rb', line 103 def post(path, data = nil, headers = {}) request(:post, path, data, headers) end |
#put(path, data = nil, headers = {}) ⇒ Object
Convenience method to make a PUT request
110 111 112 |
# File 'lib/kookaburra/api_driver.rb', line 110 def put(path, data = nil, headers = {}) request(:put, path, data, headers) end |
#request(method, path, data, headers) ⇒ Object
Make an HTTP request
If you need to make a request other than the typical GET, POST, PUT and DELETE, you can use this method directly.
This will follow redirects when the server's response code is in the 3XX range. If the response is a 303, the request will be transformed into a GET request.
167 168 169 170 171 172 173 174 |
# File 'lib/kookaburra/api_driver.rb', line 167 def request(method, path, data, headers) data = encode(data) headers = global_headers.merge(headers) response = @http_client.send(method, url_for(path), *[data, headers].compact) decode(response.body) rescue RestClient::Exception => e raise_unexpected_response(e) end |