You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+62-9Lines changed: 62 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
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).
4
4
5
-
*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)*
5
+
*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/JsonApiClient/json_api_client/tree/0.x)*
6
6
7
7
## Usage
8
8
@@ -52,14 +52,14 @@ u.update_attributes(
52
52
c:"d"
53
53
)
54
54
55
-
u.persisted?
55
+
u.persisted?
56
56
# => true
57
57
58
58
u.destroy
59
59
60
-
u.destroyed?
60
+
u.destroyed?
61
61
# => true
62
-
u.persisted?
62
+
u.persisted?
63
63
# => false
64
64
65
65
u =MyApi::Person.create(
@@ -164,7 +164,7 @@ module MyApi
164
164
classAccount < JsonApiClient::Resource
165
165
belongs_to :user
166
166
end
167
-
167
+
168
168
classCustomer < JsonApiClient::Resource
169
169
belongs_to :user, shallow_path:true
170
170
end
@@ -474,9 +474,17 @@ module MyApi
474
474
end
475
475
```
476
476
477
+
##### Server errors handling
478
+
479
+
Non-success API response will cause the specific `JsonApiClient::Errors::SomeException` raised, depends on responded HTTP status.
480
+
Please refer to [JsonApiClient::Middleware::Status#handle_status](https://github.com/JsonApiClient/json_api_client/blob/master/lib/json_api_client/middleware/status.rb)
481
+
method for concrete status-to-exception mapping used out of the box.
482
+
483
+
JsonApiClient will try determine is failed API response JsonApi-compatible, if so - JsonApi error messages will be parsed from response body, and tracked as a part of particular exception message. In additional, `JsonApiClient::Errors::ServerError` exception will keep the actual HTTP status and message within its message.
484
+
477
485
##### Custom status handler
478
486
479
-
You can change handling of response status using `connection_options`. For example you can override 400 status handling.
487
+
You can change handling of response status using `connection_options`. For example you can override 400 status handling.
480
488
By default it raises `JsonApiClient::Errors::ClientError` but you can skip exception if you want to process errors from the server.
481
489
You need to provide a `proc` which should call `throw(:handled)` default handler for this status should be skipped.
482
490
```ruby
@@ -569,7 +577,7 @@ end
569
577
570
578
You can customize how your resources find pagination information from the response.
571
579
572
-
If the [existing paginator](https://github.com/chingor13/json_api_client/blob/master/lib/json_api_client/paginating/paginator.rb) fits your requirements but you don't use the default `page` and `per_page` params for pagination, you can customise the param keys as follows:
580
+
If the [existing paginator](https://github.com/JsonApiClient/json_api_client/blob/master/lib/json_api_client/paginating/paginator.rb) fits your requirements but you don't use the default `page` and `per_page` params for pagination, you can customise the param keys as follows:
Please note that this is a global configuration, so library authors should create a custom paginator that inherits `JsonApiClient::Paginating::Paginator` and configure the custom paginator to avoid modifying global config.
580
588
581
-
If the [existing paginator](https://github.com/chingor13/json_api_client/blob/master/lib/json_api_client/paginating/paginator.rb) does not fit your needs, you can create a custom paginator:
589
+
If the [existing paginator](https://github.com/JsonApiClient/json_api_client/blob/master/lib/json_api_client/paginating/paginator.rb) does not fit your needs, you can create a custom paginator:
582
590
583
591
```ruby
584
592
classMyPaginator
@@ -591,6 +599,20 @@ class MyApi::Base < JsonApiClient::Resource
591
599
end
592
600
```
593
601
602
+
### NestedParamPaginator
603
+
604
+
The default `JsonApiClient::Paginating::Paginator` is not strict about how it handles the param keys ([#347](https://github.com/JsonApiClient/json_api_client/issues/347)). There is a second paginator that more rigorously adheres to the JSON:API pagination recommendation style of `page[page]=1&page[per_page]=10`.
605
+
606
+
If this second style suits your needs better, it is available as a class override:
You can also extend `NestedParamPaginator` in your custom paginators or assign the `page_param` or `per_page_param` as with the default version above.
615
+
594
616
### Custom type
595
617
596
618
If your model must be named differently from classified type of resource you can easily customize it.
@@ -636,6 +658,37 @@ end
636
658
637
659
```
638
660
661
+
### Safe singular resource fetching
662
+
663
+
That is a bit curios, but `json_api_client` returns an array from `.find` method, always.
664
+
The history of this fact was discussed [here](https://github.com/JsonApiClient/json_api_client/issues/75)
665
+
666
+
So, when we searching for a single resource by primary key, we typically write the things like
667
+
668
+
```ruby
669
+
admin =User.find(id).first
670
+
```
671
+
672
+
The next thing which we need to notice - `json_api_client` will just interpolate the incoming `.find` param to the end of API URL, just like that:
673
+
674
+
> http://somehost/api/v1/users/{id}
675
+
676
+
What will happen if we pass the blank id (nil or empty string) to the `.find` method then?.. Yeah, `json_api_client` will try to call the INDEX API endpoint instead of SHOW one:
677
+
678
+
> http://somehost/api/v1/users/
679
+
680
+
Lets sum all together - in case if `id` comes blank (from CGI for instance), we can silently receive the `admin` variable equal to some existing resource, with all the consequences.
681
+
682
+
Even worse, `admin` variable can equal to *random* resource, depends on ordering applied by INDEX endpoint.
683
+
684
+
If you prefer to get `JsonApiClient::Errors::NotFound` raised, please define in your base Resource class:
685
+
686
+
```ruby
687
+
classResource < JsonApiClient::Resource
688
+
self.raise_on_blank_find_param =true
689
+
end
690
+
```
691
+
639
692
## Contributing
640
693
641
694
Contributions are welcome! Please fork this repo and send a pull request. Your pull request should have:
@@ -649,4 +702,4 @@ required. The commits will be squashed into master once accepted.
649
702
650
703
## Changelog
651
704
652
-
See [changelog](https://github.com/chingor13/json_api_client/blob/master/CHANGELOG.md)
705
+
See [changelog](https://github.com/JsonApiClient/json_api_client/blob/master/CHANGELOG.md)
0 commit comments