README.md in json_api_client-1.0.0.beta2 vs README.md in json_api_client-1.0.0.beta3
- old
+ new
@@ -1,10 +1,10 @@
# JsonApiClient [![Build Status](https://travis-ci.org/chingor13/json_api_client.png)](https://travis-ci.org/chingor13/json_api_client) [![Code Climate](https://codeclimate.com/github/chingor13/json_api_client.png)](https://codeclimate.com/github/chingor13/json_api_client) [![Code Coverage](https://codeclimate.com/github/chingor13/json_api_client/coverage.png)](https://codeclimate.com/github/chingor13/json_api_client)
This gem is meant to help you build an API client for interacting with REST APIs as laid out by [http://jsonapi.org](http://jsonapi.org). It attempts to give you a query building framework that is easy to understand (it is similar to ActiveRecord scopes).
-*Note: master is currently tracking the 1.0.0 RC3 specification. If you're looking for the older code, see [0.x branch](https://github.com/chingor13/json_api_client/tree/0.x)*
+*Note: master is currently tracking the 1.0.0 specification. If you're looking for the older code, see [0.x branch](https://github.com/chingor13/json_api_client/tree/0.x)*
*Note: This is still a work in progress.*
## Usage
@@ -29,11 +29,11 @@
end
```
By convention, we figure guess the resource route from the class name. In the above example, `Article`'s path is "http://example.com/articles" and `Person`'s path would be "http://example.com/people".
-Some example usage:
+Some basic example usage:
```
MyApi::Article.all
MyApi::Article.where(author_id: 1).find(2)
MyApi::Article.where(author_id: 1).all
@@ -58,31 +58,45 @@
All class level finders/creators should return a `JsonApiClient::ResultSet` which behaves like an Array and contains extra data about the api response.
## Handling Validation Errors
+[See specification](http://jsonapi.org/format/#errors)
+
Out of the box, `json_api_client` handles server side validation only.
```
User.create(name: "Bob", email_address: "invalid email")
=> false
user = User.new(name: "Bob", email_address: "invalid email")
user.save
=> false
+
+# returns an error collector which is array-like
user.errors
=> ["Email address is invalid"]
+# get all error titles
+user.errors.full_messages
+=> ["Email address is invalid"]
+
+# get errors for a specific parameter
+user.errors[:email_address]
+=> ["Email address is invalid"]
+
user = User.find(1)
user.update_attributes(email_address: "invalid email")
=> false
user.errors
=> ["Email address is invalid"]
user.email_address
=> "invalid email"
```
+For now we are assuming that error sources are all parameters.
+
If you want to add client side validation, I suggest creating a form model class that uses ActiveModel's validations.
## Meta information
[See specification](http://jsonapi.org/format/#document-structure-meta)
@@ -202,14 +216,14 @@
## Sorting
[See specification](http://jsonapi.org/format/#fetching-sorting)
```
-# makes request to /people?sort=+age
+# makes request to /people?sort=age
youngest = Person.sort(:age).all
-# also makes request to /people?sort=+age
+# also makes request to /people?sort=age
youngest = Person.sort(age: :asc).all
# makes request to /people?sort=-age
oldest = Person.sort(age: :desc).all
```
@@ -226,11 +240,11 @@
# also makes request to /articles?page=2&per_page=30
articles = Article.paginate(page: 2, per_page: 30).to_a
```
-*Note: The mapping of pagination parameters is done by the `query_builder` which is [customizable](#fixme).*
+*Note: The mapping of pagination parameters is done by the `query_builder` which is [customizable](#custom-paginator).*
### Browsing
If the response contains additional pagination links, you can also get at those:
@@ -296,9 +310,26 @@
* `:boolean` - *Note: we will cast the string version of "true" and "false" to their respective values*
Also, we consider `nil` to be an acceptable value and will not cast the value.
## Customizing
+
+### Paths
+
+You can customize this path by changing your resource's `table_name`:
+
+```
+module MyApi
+ class SomeResource < Base
+ def self.table_name
+ "foobar"
+ end
+ end
+end
+
+# requests http://example.com/foobar
+MyApi::SomeResource.all
+```
### Connections
You can configure your API client to use a custom connection that implementes the `run` instance method. It should return data that your parser can handle. The default connection class wraps Faraday and lets you add middleware.