jsonapi-realizer 6.0.0.rc1 → 6.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +61 -50
- data/lib/jsonapi/realizer/adapter.rb +2 -0
- data/lib/jsonapi/realizer/resource.rb +1 -1
- data/lib/jsonapi/realizer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d793387ade1272c7e808efdf832b814e5e27c5f8774e8504a77179c330882969
|
4
|
+
data.tar.gz: ad543bd542140615082ce32ec252d51e773376040a1a7d8c16df6c93d7bba1ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: daebfcf746fcf038f60777f2edf677798e764f2078556d0218c23dc295230144b7053dcf502e1692c45e514ff2dff09d369f0690b8d1069bc73b388c0c421939
|
7
|
+
data.tar.gz: 4e5ce6aa5fab832de40dee2d0d00f12047839decc9ca25090fa87cd059ecec81d30d4472ea8b722d3793dae72d603f97ba822692369e5694bb224b40fce146c0
|
data/README.md
CHANGED
@@ -5,78 +5,84 @@
|
|
5
5
|
- [![Version](http://img.shields.io/gem/v/jsonapi-realizer.svg?style=flat-square)](https://rubygems.org/gems/jsonapi-realizer)
|
6
6
|
|
7
7
|
|
8
|
-
This library handles incoming [json:api](https://www.jsonapi.org) payloads and turns them, via an adapter system, into data models
|
8
|
+
This library handles incoming [json:api](https://www.jsonapi.org) payloads and turns them, via an adapter system, into native data models. While designed with rails in mind, this library doesn't require rails to use.
|
9
|
+
|
9
10
|
|
10
11
|
## Using
|
11
12
|
|
12
13
|
In order to use this library you'll want to have some models:
|
13
14
|
|
14
|
-
``` ruby
|
15
|
-
class Photo < ApplicationRecord
|
16
|
-
belongs_to :photographer, class_name: "Profile"
|
17
|
-
end
|
18
15
|
|
16
|
+
``` ruby
|
19
17
|
class Profile < ApplicationRecord
|
20
18
|
has_many :photos
|
21
19
|
end
|
22
20
|
```
|
23
21
|
|
24
|
-
*Note: They don't have to be ActiveRecord models, but we have built-in support for that library (adapter-based).*
|
25
|
-
|
26
|
-
Second you'll need some realizers:
|
27
|
-
|
28
22
|
``` ruby
|
29
|
-
class
|
30
|
-
|
31
|
-
|
32
|
-
|
23
|
+
class Photo < ApplicationRecord
|
24
|
+
belongs_to :photographer, class_name: "Profile"
|
25
|
+
end
|
26
|
+
```
|
33
27
|
|
34
|
-
|
28
|
+
*Note: They don't have to be ActiveRecord models, but we have built-in support for that library (via an adapter).*
|
35
29
|
|
36
|
-
|
37
|
-
has :src
|
38
|
-
end
|
30
|
+
Second you'll need some realizers:
|
39
31
|
|
32
|
+
``` ruby
|
40
33
|
class ProfileRealizer
|
41
34
|
include JSONAPI::Realizer::Resource
|
42
35
|
|
43
|
-
|
36
|
+
type :profiles, class_name: "Profile", adapter: :active_record
|
44
37
|
|
45
|
-
has_many :photos
|
38
|
+
has_many :photos, class_name: "PhotoRealizer"
|
46
39
|
|
47
40
|
has :name
|
48
41
|
end
|
49
42
|
```
|
50
43
|
|
51
|
-
You can define aliases for these properties:
|
52
|
-
|
53
44
|
``` ruby
|
54
|
-
|
45
|
+
class PhotoRealizer
|
46
|
+
include JSONAPI::Realizer::Resource
|
47
|
+
|
48
|
+
type :photos, class_name: "Photo", adapter: :active_record
|
55
49
|
|
56
|
-
|
50
|
+
has_one :photographer, as: :profiles, class_name: "ProfileRealizer"
|
51
|
+
|
52
|
+
has :title
|
53
|
+
has :src
|
54
|
+
end
|
57
55
|
```
|
58
56
|
|
59
|
-
|
57
|
+
Now that we have these we can invoke them in the controller:
|
60
58
|
|
61
59
|
``` ruby
|
62
60
|
class PhotosController < ApplicationController
|
63
61
|
def create
|
64
|
-
|
62
|
+
realizer = PhotoRealizer.new(
|
63
|
+
:intent => :create,
|
64
|
+
:parameters => params,
|
65
|
+
:headers => request.headers
|
66
|
+
)
|
65
67
|
|
66
|
-
|
68
|
+
realizer.object.save!
|
67
69
|
|
68
|
-
render json:
|
70
|
+
render json: realizer.object.to_json
|
69
71
|
end
|
70
72
|
|
71
73
|
def index
|
72
|
-
|
74
|
+
realizer = PhotoRealizer.new(
|
75
|
+
:intent => :index,
|
76
|
+
:parameters => params,
|
77
|
+
:headers => request.headers
|
78
|
+
)
|
73
79
|
|
74
|
-
render json:
|
80
|
+
render json: realizer.object.to_json
|
75
81
|
end
|
76
82
|
end
|
77
83
|
```
|
78
84
|
|
79
|
-
Notice that we
|
85
|
+
Notice that we have to handle creating the model ourselves with `realizer.object.save!`. `jsonapi-realizer` doesn't act on a request, it only prepares you to act on the request.
|
80
86
|
|
81
87
|
|
82
88
|
### Adapters
|
@@ -88,33 +94,38 @@ There are two core adapters:
|
|
88
94
|
|
89
95
|
An adapter must provide the following interfaces:
|
90
96
|
|
91
|
-
0. `
|
92
|
-
0. `
|
93
|
-
0. `
|
94
|
-
0. `
|
95
|
-
0. `
|
96
|
-
0. `
|
97
|
+
0. `find_many(scope)`, describes how to find many records
|
98
|
+
0. `find_one(scope, id)`, describes how to find one record
|
99
|
+
0. `filtering(scope, filters)`, describes how to filter records by a set of properties
|
100
|
+
0. `sorting(scope, sorts)`, describes how to sort records
|
101
|
+
0. `paginate(scope, per, offset)`, describes how to page records
|
102
|
+
0. `write_attributes(model, attributes)`, describes how to write a set of properties
|
103
|
+
0. `write_relationships(model, relationships)`, describes how to write a set of relationships
|
104
|
+
0. `include_relationships(scope, includes)`, describes how to eager include related models
|
97
105
|
|
98
|
-
You can also provide custom adapter interfaces like below
|
106
|
+
You can also provide custom adapter interfaces like below:
|
99
107
|
|
100
108
|
``` ruby
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
model_class.where { id == id or slug == id }.first
|
108
|
-
end
|
109
|
+
JSONAPI::Realizer.configuration do |let|
|
110
|
+
let.adapter_mappings = {
|
111
|
+
active_record_postgres_pagination: PostgresActiveRecordPaginationAdapter
|
112
|
+
}
|
113
|
+
end
|
114
|
+
```
|
109
115
|
|
110
|
-
|
111
|
-
|
116
|
+
``` ruby
|
117
|
+
module PostgresActiveRecordPaginationAdapter < JSONAPI::Realizer::Adapter::ActiveRecord
|
118
|
+
def paginate(scope, per, offset)
|
119
|
+
scope.offset(offset).limit(per)
|
112
120
|
end
|
121
|
+
end
|
122
|
+
```
|
113
123
|
|
114
|
-
|
124
|
+
``` ruby
|
125
|
+
class PhotoRealizer
|
126
|
+
include JSONAPI::Realizer::Resource
|
115
127
|
|
116
|
-
|
117
|
-
has :src
|
128
|
+
type :photos, class_name: "Photo", adapter: :active_record_postgres_pagination
|
118
129
|
end
|
119
130
|
```
|
120
131
|
|
@@ -33,6 +33,8 @@ module JSONAPI
|
|
33
33
|
raise(ArgumentError, "need to provide a Adapter#write_relationships interface") unless respond_to?(:write_relationships)
|
34
34
|
raise(ArgumentError, "need to provide a Adapter#include_relationships interface") unless respond_to?(:include_relationships)
|
35
35
|
raise(ArgumentError, "need to provide a Adapter#paginate interface") unless respond_to?(:paginate)
|
36
|
+
raise(ArgumentError, "need to provide a Adapter#sorting interface") unless respond_to?(:sorting)
|
37
|
+
raise(ArgumentError, "need to provide a Adapter#filtering interface") unless respond_to?(:filtering)
|
36
38
|
end
|
37
39
|
end
|
38
40
|
end
|
@@ -322,7 +322,7 @@ module JSONAPI
|
|
322
322
|
end
|
323
323
|
|
324
324
|
def attribute(name)
|
325
|
-
configuration.attributes.fetch(name.to_sym){raise(Error::
|
325
|
+
configuration.attributes.fetch(name.to_sym){raise(Error::ResourceAttributeNotFound, name: name, realizer: self)}
|
326
326
|
end
|
327
327
|
|
328
328
|
def relation(name)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-realizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 6.0.0.
|
4
|
+
version: 6.0.0.rc2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kurtis Rainbolt-Greene
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubocop
|