abc_jsonapi 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +109 -5
- data/lib/abc_jsonapi/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a32de4446187395c51ca0c74337fce16277306c3720a1a3a1343cc7a931ea2eb
|
4
|
+
data.tar.gz: d04e65627b5ebfbf07a179256e42468c159adc51a5956638b31a404274d5a745
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d98ad8a97ee7dc9b5ec4cfae77b55be6cbafe06fe64e1cf69c39f0b46f2392c15e1683a412bd82594e9cd07019fddb700831c530c118a12b795ce3344a60adb
|
7
|
+
data.tar.gz: cf1b1de98a118746bae5d939a83d524ac60cfd04b1a99423c8f84c6e5bb92ecd31f6339151d0fa4e0268c55e87c9b2a83e7e83b639138775a9bc3f29ec4141f2
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,36 +1,140 @@
|
|
1
1
|
# AbcJsonapi
|
2
2
|
|
3
3
|
Minimalistic gem for JSON Serialization according to https://jsonapi.org spec
|
4
|
+
Inspired by [https://github.com/Netflix/fast_jsonapi](https://github.com/Netflix/fast_jsonapi)
|
5
|
+
Contributions are welcome.
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
7
9
|
Add this line to your application's Gemfile:
|
8
10
|
|
9
11
|
```ruby
|
12
|
+
|
10
13
|
gem 'abc_jsonapi'
|
14
|
+
|
11
15
|
```
|
12
16
|
|
13
17
|
And then execute:
|
14
18
|
|
15
|
-
|
19
|
+
\$ bundle
|
16
20
|
|
17
21
|
Or install it yourself as:
|
18
22
|
|
19
|
-
|
23
|
+
\$ gem install abc_jsonapi
|
24
|
+
|
25
|
+
## Features
|
26
|
+
|
27
|
+
- Relationships (belongs_to, has_many, has_one)
|
28
|
+
- Compound documents ("include" option)
|
29
|
+
- Custom include strategies. It is useful when serializing a collection of records to avoid N+1 problem.
|
30
|
+
- _"Virtual"_ attributes or overriding default attribute serializing behavior
|
20
31
|
|
21
32
|
## Usage
|
22
33
|
|
23
|
-
|
34
|
+
Syntax is very similar to Active Model Serializer.
|
35
|
+
|
36
|
+
The serializer is able to work with any ruby objects. Not only ActiveRecord. The idea is to use duck typing for getting object attributes, relationships and all other data.
|
37
|
+
|
38
|
+
#### Model Example
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class Author
|
42
|
+
attr_reader :first_name, :last_name, :public_name, :contact_id
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
#### Serializer Definition
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
class AuthorSerializer
|
50
|
+
include AbcJsonapi::Serializer
|
51
|
+
resource_type :people (optional)
|
52
|
+
attributes :first_name, :last_name, :public_name
|
53
|
+
belongs_to :contact
|
54
|
+
has_many :books
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
#### Custom resource type
|
59
|
+
|
60
|
+
Default resource type of direct serializing model is taken from serializer filename. In case of `AuthorSerializer` with disabled pluralize_resources it will be `"author"`. Resource type of compound documents is downcased class of related object.
|
61
|
+
|
62
|
+
#### Attributes
|
63
|
+
|
64
|
+
Jsonapi attributes may be declared with class method of serializer - `attributes`:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
attributes :first_attribute, :second, *other
|
68
|
+
```
|
69
|
+
|
70
|
+
`attributes` arguments will be called on serializing model as methods.
|
71
|
+
|
72
|
+
Also there is `attribute` method to declare single property. You can pass a block to define the way it should be returned.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
attribute :date_of_birth do |object|
|
76
|
+
object.date_of_birth.strftime("%FT%T.%3N%:z") if object.stop.present?
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
#### Serializer usage
|
81
|
+
|
82
|
+
Return ruby hash:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
AuthorSerializer.new(resource).serializable_hash
|
86
|
+
```
|
87
|
+
|
88
|
+
Return json:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
AuthorSerializer.new(resource).serialized_json
|
92
|
+
```
|
93
|
+
|
94
|
+
## Compound Documents
|
95
|
+
|
96
|
+
To include relationships you can pass `include` option at the serializer initialization stage.
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
options = {}
|
100
|
+
options[:include] = :books # or "books"
|
101
|
+
AuthorSerializer.new(resource, options).serialized_json
|
102
|
+
```
|
103
|
+
|
104
|
+
## Configuration
|
105
|
+
|
106
|
+
#### Global config options
|
107
|
+
|
108
|
+
- **transform_keys** (_true/false_) - convert keys to any case or not. Default value: _true_
|
109
|
+
- **key_transform_method** (_"camel"_ or _"snake"_) - certain key transform method to use with **transform_keys** option enabled. Default value: _"camel"_
|
110
|
+
- **pluralize_resources** (_true/false_) - pluralize all resources types in response by default or not. Default value: _false_
|
111
|
+
|
112
|
+
#### Usage
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
AbcJsonapi.configure do |config|
|
116
|
+
config.transform_keys = true
|
117
|
+
config.key_transform_method = "camel"
|
118
|
+
config.pluralize_resources = true
|
119
|
+
end
|
120
|
+
```
|
24
121
|
|
25
122
|
## Development
|
26
123
|
|
27
|
-
|
124
|
+
**TODO**:
|
125
|
+
|
126
|
+
- Write specs
|
127
|
+
- Use custom include relationship strategies for downloading relationships json block (solve N+1 problem when serializing collection).
|
128
|
+
- Meta per resource
|
129
|
+
- Add other jsonapi features
|
130
|
+
|
131
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
132
|
|
29
133
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
134
|
|
31
135
|
## Contributing
|
32
136
|
|
33
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
137
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ezname/abc_jsonapi.
|
34
138
|
|
35
139
|
## License
|
36
140
|
|
data/lib/abc_jsonapi/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: abc_jsonapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zhmakin Evgeniy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -180,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
180
180
|
- !ruby/object:Gem::Version
|
181
181
|
version: '0'
|
182
182
|
requirements: []
|
183
|
-
rubygems_version: 3.0.
|
183
|
+
rubygems_version: 3.0.6
|
184
184
|
signing_key:
|
185
185
|
specification_version: 4
|
186
186
|
summary: Json API serializer gem
|