Skip to content

Commit

Permalink
Move connection pool info below main usage area [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
marshall-lee committed Jul 7, 2015
1 parent 5288afd commit a43dad0
Showing 1 changed file with 47 additions and 47 deletions.
94 changes: 47 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit a43dad0

Please sign in to comment.