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.
78 79 80 |
# File 'lib/api_smith/client.rb', line 78 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.
39 40 41 |
# File 'lib/api_smith/client.rb', line 39 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.
52 53 54 |
# File 'lib/api_smith/client.rb', line 52 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.
65 66 67 |
# File 'lib/api_smith/client.rb', line 65 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.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/api_smith/client.rb', line 102 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 = self.class.send method, full_path, # 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 |