Module: APISmith::Client::InstanceMethods
- Defined in:
- lib/api_smith/client.rb
Overview
The most important part of the client functionality - namely, provides a set of tools and methods that make it possible to build API clients. This is where the bulk of the client work takes place.
Instance Method Summary collapse
-
#delete(path, options = {}) ⇒ Object
Given a path relative to the endpoint (or ‘/`), will perform a DELETE request.
-
#get(path, options = {}) ⇒ Object
Given a path relative to the endpoint (or ‘/`), will perform a GET request.
-
#post(path, options = {}) ⇒ Object
Given a path relative to the endpoint (or ‘/`), will perform a POST request.
-
#put(path, options = {}) ⇒ Object
Given a path relative to the endpoint (or ‘/`), will perform a PUT request.
-
#request!(method, path, options, *param_types) ⇒ Object
Performs a HTTP request using HTTParty, using a set of expanded options built up by the current client.
Instance Method Details
#delete(path, options = {}) ⇒ Object
Given a path relative to the endpoint (or ‘/`), will perform a DELETE request.
If provided, will use a ‘:transformer` to convert the resultant data into a useable object. Also, in the case an object is nested, allows you to traverse an object.
79 80 81 |
# File 'lib/api_smith/client.rb', line 79 def delete(path, = {}) request! :delete, path, , :query end |
#get(path, options = {}) ⇒ Object
Given a path relative to the endpoint (or ‘/`), will perform a GET request.
If provided, will use a ‘:transformer` to convert the resultant data into a useable object. Also, in the case an object is nested, allows you to traverse an object.
40 41 42 |
# File 'lib/api_smith/client.rb', line 40 def get(path, = {}) request! :get, path, , :query end |
#post(path, options = {}) ⇒ Object
Given a path relative to the endpoint (or ‘/`), will perform a POST request.
If provided, will use a ‘:transformer` to convert the resultant data into a useable object. Also, in the case an object is nested, allows you to traverse an object.
53 54 55 |
# File 'lib/api_smith/client.rb', line 53 def post(path, = {}) request! :post, path, , :query, :body end |
#put(path, options = {}) ⇒ Object
Given a path relative to the endpoint (or ‘/`), will perform a PUT request.
If provided, will use a ‘:transformer` to convert the resultant data into a useable object. Also, in the case an object is nested, allows you to traverse an object.
66 67 68 |
# File 'lib/api_smith/client.rb', line 66 def put(path, = {}) request! :put, path, , :query, :body end |
#request!(method, path, options, *param_types) ⇒ Object
Performs a HTTP request using HTTParty, using a set of expanded options built up by the current client.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/api_smith/client.rb', line 103 def request!(method, path, , *param_types) # Merge in the default request options, e.g. those to be passed to HTTParty raw = (:request, ) # Exapdn the path out into a full version when the endpoint is present. full_path = [:skip_endpoint] ? path : path_for(path) # For each of the given param_types (e.g. :query, :body) will automatically # merge in the options for the current request. param_types.each do |type| [type] = (type, ) end # Finally, use HTTParty to get the response response = instrument_request method, full_path, do self.class.send method, full_path, end # Pre-process the response to check for errors. check_response_errors response # Unpack the response using the :response_container option inner_response = extract_response path, response, # Finally, apply any transformations transform_response inner_response, end |