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: CHANGELOG.md
+75Lines changed: 75 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,81 @@
2
2
3
3
## Unreleased
4
4
5
+
## 1.16.1
6
+
7
+
-[#361](https://github.com/JsonApiClient/json_api_client/pull/361) - Call super from inherited method so that it will execute parent classes' implementations
8
+
9
+
## 1.16.0
10
+
11
+
-[#359](https://github.com/JsonApiClient/json_api_client/pull/359) - Support gzip content encoding
12
+
13
+
## 1.15.0
14
+
15
+
-[#346](https://github.com/JsonApiClient/json_api_client/pull/346) - add the option to have immutable resources
-[#353](https://github.com/JsonApiClient/json_api_client/pull/353) - fix to support deserializing resources with relationships without those related resources being included in the response (issue [#352](https://github.com/JsonApiClient/json_api_client/issues/352)).
21
+
22
+
## 1.14.0
23
+
24
+
-[#338](https://github.com/JsonApiClient/json_api_client/pull/338) - implement hash and eql? for builder class
25
+
-[#351](https://github.com/JsonApiClient/json_api_client/pull/351) - Remove rudimental `last_result_set` relationship from serializer
26
+
27
+
## 1.13.0
28
+
29
+
-[#348](https://github.com/JsonApiClient/json_api_client/pull/348) - add NestedParamPaginator to address inconsistency in handling of pagination query string params (issue [#347](https://github.com/JsonApiClient/json_api_client/issues/347)).
30
+
31
+
## 1.12.2
32
+
33
+
-[#350](https://github.com/JsonApiClient/json_api_client/pull/350) - fix resource including with blank `relationships` response data
34
+
35
+
## 1.12.1
36
+
37
+
-[#349](https://github.com/JsonApiClient/json_api_client/pull/349) - fix resource including for STI objects
38
+
39
+
## 1.12.0
40
+
41
+
-[#345](https://github.com/JsonApiClient/json_api_client/pull/345) - track the real HTTP reason of ApiErrors
Copy file name to clipboardExpand all lines: README.md
+141-8Lines changed: 141 additions & 8 deletions
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(
@@ -157,13 +157,17 @@ articles.links.related
157
157
158
158
You can force nested resource paths for your models by using a `belongs_to` association.
159
159
160
-
**Note: Using belongs_to is only necessary for setting a nested path.**
160
+
**Note: Using belongs_to is only necessary for setting a nested path unless you provide `shallow_path: true` option.**
161
161
162
162
```ruby
163
163
moduleMyApi
164
164
classAccount < JsonApiClient::Resource
165
165
belongs_to :user
166
166
end
167
+
168
+
classCustomer < JsonApiClient::Resource
169
+
belongs_to :user, shallow_path:true
170
+
end
167
171
end
168
172
169
173
# try to find without the nested parameter
@@ -173,6 +177,28 @@ MyApi::Account.find(1)
173
177
# makes request to /users/2/accounts/1
174
178
MyApi::Account.where(user_id:2).find(1)
175
179
# => returns ResultSet
180
+
181
+
# makes request to /customers/1
182
+
MyApi::Customer.find(1)
183
+
# => returns ResultSet
184
+
185
+
# makes request to /users/2/customers/1
186
+
MyApi::Customer.where(user_id:2).find(1)
187
+
# => returns ResultSet
188
+
```
189
+
190
+
you can also override param name for `belongs_to` association
191
+
192
+
```ruby
193
+
moduleMyApi
194
+
classAccount < JsonApiClient::Resource
195
+
belongs_to :user, param::customer_id
196
+
end
197
+
end
198
+
199
+
# makes request to /users/2/accounts/1
200
+
MyApi::Account.where(customer_id:2).find(1)
201
+
# => returns ResultSet
176
202
```
177
203
178
204
## Custom Methods
@@ -448,6 +474,52 @@ module MyApi
448
474
end
449
475
```
450
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
+
485
+
##### Custom status handler
486
+
487
+
You can change handling of response status using `connection_options`. For example you can override 400 status handling.
488
+
By default it raises `JsonApiClient::Errors::ClientError` but you can skip exception if you want to process errors from the server.
489
+
You need to provide a `proc` which should call `throw(:handled)` default handler for this status should be skipped.
# server responds with { errors: [ { detail: 'bad request' } ] }
519
+
user.errors.messages # { base: ['bad request'] }
520
+
# on 401 it will raise CustomUnauthorizedError instead of JsonApiClient::Errors::NotAuthorized
521
+
```
522
+
451
523
##### Specifying an HTTP Proxy
452
524
453
525
All resources have a class method ```connection_options``` used to pass options to the JsonApiClient::Connection initializer.
@@ -505,7 +577,7 @@ end
505
577
506
578
You can customize how your resources find pagination information from the response.
507
579
508
-
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.
516
588
517
-
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:
518
590
519
591
```ruby
520
592
classMyPaginator
@@ -527,6 +599,36 @@ class MyApi::Base < JsonApiClient::Resource
527
599
end
528
600
```
529
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
+
616
+
### Custom type
617
+
618
+
If your model must be named differently from classified type of resource you can easily customize it.
619
+
It will work both for defined and not defined relationships
620
+
621
+
```ruby
622
+
classMyApi::Base < JsonApiClient::Resource
623
+
resolve_custom_type 'document--files', 'File'
624
+
end
625
+
626
+
classMyApi::File < MyApi::Base
627
+
defself.resource_name
628
+
'document--files'
629
+
end
630
+
end
631
+
```
530
632
531
633
### Type Casting
532
634
@@ -556,6 +658,37 @@ end
556
658
557
659
```
558
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
+
559
692
## Contributing
560
693
561
694
Contributions are welcome! Please fork this repo and send a pull request. Your pull request should have:
@@ -569,4 +702,4 @@ required. The commits will be squashed into master once accepted.
569
702
570
703
## Changelog
571
704
572
-
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