gem 'cool_http', github: 'jorgeto86/cool_http'
The client is encapsulated in a class. Requests are encapsulated in modules. Declared methods in a client will be used in all requests. However, declare methos in modules will be used only for his corresponding request.
- url
- http_method
- body
- path
- query
- connection_options
We suggest the clients and modules should be in app/clients
.
# For example:
app
|
clients
|--- comments
| |--- comments.rb # All comments
| |--- delete.rb # Delete a comment
| |--- new.rb # Create a comment
| |--- show.rb # Show a comment
| |--- update.rb # Update a comment
|
|--- comments_client.rb # Comments client
This example allows do test requests to a Rest API (http://jsonplaceholder.typicode.com/).
# Comments client. Clients have to inherit from cool_http
class CommentsClient < CoolHttp
# This url will be use for all request because it is declared in client but
# you can use a different url for a request declaring url method in the request module.
def url
"http://jsonplaceholder.typicode.com"
end
# This path will be use for all request least show request. This is because
# show has his own path.
def path(params=nil)
"/comments/"
end
end
# Request to create a comment
module Comments
module New
private
def http_method
:post
end
def body(params=nil)
{
postId: 1,
name: params[:name],
email: params[:email],
body: params[:body]
}
end
end
end
# Request to show a comment
module Comments
module Show
private
def http_method
:get
end
# This path is only used to show request
def path(params=nil)
"/comments/#{params[:id]}"
end
end
end
# Request for all comments
module Comments
module Comments
private
def http_method
:get
end
end
end
To do a request, creates a client object with module corresponding to the request
and call perform
method. This accepts 3 params.
- path_params: It is a hash with path params.
- body_params: It is a hash with body params.
- query_params: It is a hash with query params.
CommentsClient.new(:comments).perform
path_params = { id: 1 }
CommentsClient.new(:show).perform(path_params)
path_params = { id: 1 }
body_params = {name: 'test'
CommentsClient.new(:update).perform(path_params, body_params)
body_params = { name: 'test', email: 'test@test.com', body: 'hola' }
CommentsClient.new(:new).perform(nil, body_params)
path_params = { id: 1 }
CommentsClient.new(:delete).perform(path_params)