From a43dad0349ffad30dcf84813bbff4de654d7c914 Mon Sep 17 00:00:00 2001 From: Vladimir Kochnev Date: Tue, 7 Jul 2015 18:29:21 +0300 Subject: [PATCH] Move connection pool info below main usage area [ci skip] --- README.md | 94 +++++++++++++++++++++++++++---------------------------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 465299e8..c485d0aa 100644 --- a/README.md +++ b/README.md @@ -74,9 +74,55 @@ User.find(1) # PUT "https://api.example.com/users/1" with `fullname=Lindsay+Fünke` and return the updated User object ``` +### ActiveRecord-like methods + +These are the basic ActiveRecord-like methods you can use with your models: + +```ruby +class User + include Her::Model +end + +# Update a fetched resource +user = User.find(1) +user.fullname = "Lindsay Fünke" # OR user.assign_attributes(fullname: "Lindsay Fünke") +user.save # returns false if it fails, errors in user.response_errors array +# PUT "/users/1" with `fullname=Lindsay+Fünke` + +# Update a resource without fetching it +User.save_existing(1, fullname: "Lindsay Fünke") +# PUT "/users/1" with `fullname=Lindsay+Fünke` + +# Destroy a fetched resource +user = User.find(1) +user.destroy +# DELETE "/users/1" + +# Destroy a resource without fetching it +User.destroy_existing(1) +# DELETE "/users/1" + +# Fetching a collection of resources +User.all +# GET "/users" +User.where(moderator: 1).all +# GET "/users?moderator=1" + +# Create a new resource +User.create(fullname: "Maeby Fünke") +# POST "/users" with `fullname=Maeby+Fünke` + +# Save a new resource +user = User.new(fullname: "Maeby Fünke") +user.save! # raises Her::Errors::ResourceInvalid if it fails +# POST "/users" with `fullname=Maeby+Fünke` +``` + +You can look into the [`her-example`](https://github.com/remiprev/her-example) repository for a sample application using Her. + ### Persistent connection -Her creates a connection instance only once but that's not all. In the example above `Faraday::Adapter::NetHttp` is used. It makes Her use of Ruby's standard `Net::HTTP` library which doesn't force you to install additional http client gems. However you should know that `Net::HTTP` connections are not persistent in Faraday (not *reusable* or not *keep-alive* in other words) so new TCP/IP connection is established for each requet. +Her creates a connection instance only once but that's not all. In the example above `Faraday::Adapter::NetHttp` is used. It makes Her use Ruby's standard `Net::HTTP` library which doesn't force you to install additional http client gems. However you should know that `Net::HTTP` connections are not persistent in Faraday (not *reusable* or not *keep-alive* in other words) so new TCP/IP connection is established for each requet. To avoid this problem you should use a different HTTP client. For example, there is a [httpclient](https://github.com/nahi/httpclient) gem which supports persistent connections. Add it to Gemfile: @@ -143,52 +189,6 @@ end To take full advantage of concurrent connection pool make sure you have a [persistent connection](#persistent-connection). -### ActiveRecord-like methods - -These are the basic ActiveRecord-like methods you can use with your models: - -```ruby -class User - include Her::Model -end - -# Update a fetched resource -user = User.find(1) -user.fullname = "Lindsay Fünke" # OR user.assign_attributes(fullname: "Lindsay Fünke") -user.save # returns false if it fails, errors in user.response_errors array -# PUT "/users/1" with `fullname=Lindsay+Fünke` - -# Update a resource without fetching it -User.save_existing(1, fullname: "Lindsay Fünke") -# PUT "/users/1" with `fullname=Lindsay+Fünke` - -# Destroy a fetched resource -user = User.find(1) -user.destroy -# DELETE "/users/1" - -# Destroy a resource without fetching it -User.destroy_existing(1) -# DELETE "/users/1" - -# Fetching a collection of resources -User.all -# GET "/users" -User.where(moderator: 1).all -# GET "/users?moderator=1" - -# Create a new resource -User.create(fullname: "Maeby Fünke") -# POST "/users" with `fullname=Maeby+Fünke` - -# Save a new resource -user = User.new(fullname: "Maeby Fünke") -user.save! # raises Her::Errors::ResourceInvalid if it fails -# POST "/users" with `fullname=Maeby+Fünke` -``` - -You can look into the [`her-example`](https://github.com/remiprev/her-example) repository for a sample application using Her. - ## Middleware Since Her relies on [Faraday](https://github.com/lostisland/faraday) to send HTTP requests, you can choose the middleware used to handle requests and responses. Using the block in the `setup` call, you have access to Faraday’s `connection` object and are able to customize the middleware stack used on each request and response.