Skip to content

Commit cf86cd8

Browse files
committed
merging with release-1.1
2 parents 7e59446 + c40354e commit cf86cd8

40 files changed

+1774
-520
lines changed

.gitignore

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,14 @@ doc
1616
# For MacOS:
1717
.DS_Store
1818

19-
# juwelier generated
20-
pkg
21-
2219
# For MacOS:
2320
.DS_Store
2421

2522
# For TextMate
2623
#*.tmproj
2724
#tmtags
2825

29-
# For vim:
30-
#*.swp
26+
*.swp
3127

3228
# For redcar:
3329
#.redcar
@@ -37,3 +33,9 @@ pkg
3733

3834
# For the gem
3935
test.db
36+
37+
# For those using rbenv
38+
.ruby-version
39+
40+
# For those who install gems locally to a vendor dir
41+
/vendor

.travis.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
language: ruby
22
rvm:
3-
- 2.3.1
3+
- 2.2.9
4+
- 2.3.6
5+
- 2.4.3
6+
- 2.5.0
47
script:
5-
- bundle exec rake spec
8+
- bundle exec rspec

Gemfile.lock

Lines changed: 16 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
PATH
22
remote: .
33
specs:
4-
fast_jsonapi (1.0.17)
5-
activerecord (~> 5.0)
6-
activesupport (~> 5.0)
7-
multi_json (~> 1.12)
8-
oj (~> 3.3)
4+
fast_jsonapi (1.1.0)
5+
activesupport (>= 4.2)
96

107
GEM
118
remote: https://rubygems.org/
@@ -39,8 +36,6 @@ GEM
3936
i18n (~> 0.7)
4037
minitest (~> 5.1)
4138
tzinfo (~> 1.1)
42-
addressable (2.5.2)
43-
public_suffix (>= 2.0.2, < 4.0)
4439
arel (8.0.0)
4540
benchmark-perf (0.2.1)
4641
builder (3.2.3)
@@ -49,55 +44,27 @@ GEM
4944
activesupport
5045
concurrent-ruby (1.0.5)
5146
crass (1.0.3)
52-
descendants_tracker (0.0.4)
53-
thread_safe (~> 0.3, >= 0.3.1)
5447
diff-lcs (1.3)
55-
docile (1.1.5)
5648
erubi (1.7.0)
57-
faraday (0.12.2)
58-
multipart-post (>= 1.2, < 3)
59-
git (1.3.0)
60-
github_api (0.18.2)
61-
addressable (~> 2.4)
62-
descendants_tracker (~> 0.0.4)
63-
faraday (~> 0.8)
64-
hashie (~> 3.5, >= 3.5.2)
65-
oauth2 (~> 1.0)
66-
hashie (3.5.6)
67-
highline (1.7.10)
6849
i18n (0.9.1)
6950
concurrent-ruby (~> 1.0)
70-
json (1.8.6)
51+
jsonapi-deserializable (0.2.0)
52+
jsonapi-rb (0.5.0)
53+
jsonapi-deserializable (~> 0.2.0)
54+
jsonapi-serializable (~> 0.3.0)
7155
jsonapi-renderer (0.2.0)
72-
juwelier (2.1.3)
73-
builder
74-
bundler (>= 1.13)
75-
git (>= 1.2.5)
76-
github_api
77-
highline (>= 1.6.15)
78-
nokogiri (>= 1.5.10)
79-
rake
80-
rdoc
81-
semver
82-
jwt (1.5.6)
56+
jsonapi-serializable (0.3.0)
57+
jsonapi-renderer (~> 0.2.0)
58+
jsonapi-serializers (1.0.0)
59+
activesupport
8360
loofah (2.1.1)
8461
crass (~> 1.0.2)
8562
nokogiri (>= 1.5.9)
8663
mini_portile2 (2.3.0)
8764
minitest (5.10.3)
88-
multi_json (1.12.2)
89-
multi_xml (0.6.0)
90-
multipart-post (2.0.0)
9165
nokogiri (1.8.1)
9266
mini_portile2 (~> 2.3.0)
93-
oauth2 (1.4.0)
94-
faraday (>= 0.8, < 0.13)
95-
jwt (~> 1.0)
96-
multi_json (~> 1.3)
97-
multi_xml (~> 0.5)
98-
rack (>= 1.2, < 3)
9967
oj (3.4.0)
100-
public_suffix (3.0.1)
10168
rack (2.0.3)
10269
rack-test (0.8.2)
10370
rack (>= 1.0, < 3)
@@ -106,9 +73,6 @@ GEM
10673
nokogiri (>= 1.6)
10774
rails-html-sanitizer (1.0.3)
10875
loofah (~> 2.0)
109-
rake (12.3.0)
110-
rdoc (3.12.2)
111-
json (~> 1.4)
11276
rspec (3.5.0)
11377
rspec-core (~> 3.5.0)
11478
rspec-expectations (~> 3.5.0)
@@ -125,12 +89,6 @@ GEM
12589
diff-lcs (>= 1.2.0, < 2.0)
12690
rspec-support (~> 3.5.0)
12791
rspec-support (3.5.0)
128-
semver (1.0.1)
129-
simplecov (0.15.1)
130-
docile (~> 1.1.0)
131-
json (>= 1.8, < 3)
132-
simplecov-html (~> 0.10.0)
133-
simplecov-html (0.10.2)
13492
skylight (1.5.0)
13593
activesupport (>= 3.0.0)
13694
sqlite3 (1.3.13)
@@ -142,17 +100,18 @@ PLATFORMS
142100
ruby
143101

144102
DEPENDENCIES
145-
active_model_serializers (~> 0.10.4)
103+
active_model_serializers (~> 0.10.7)
104+
activerecord (>= 4.2)
146105
bundler (~> 1.0)
147106
byebug
148107
fast_jsonapi!
149-
juwelier (~> 2.1.0)
150-
rdoc (~> 3.12)
108+
jsonapi-rb (~> 0.5.0)
109+
jsonapi-serializers (~> 1.0.0)
110+
oj (~> 3.3)
151111
rspec (~> 3.5.0)
152112
rspec-benchmark (~> 0.3.0)
153-
simplecov
154113
skylight (~> 1.3)
155114
sqlite3 (~> 1.3)
156115

157116
BUNDLED WITH
158-
1.16.0
117+
1.16.1

README.md

Lines changed: 103 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ Fast JSON API serialized 250 records in 3.01 ms
2121
* [Features](#features)
2222
* [Installation](#installation)
2323
* [Usage](#usage)
24+
* [Rails Generator](#rails-generator)
2425
* [Model Definition](#model-definition)
2526
* [Serializer Definition](#serializer-definition)
2627
* [Object Serialization](#object-serialization)
2728
* [Compound Document](#compound-document)
29+
* [Key Transforms](#key-transforms)
2830
* [Collection Serialization](#collection-serialization)
2931
* [Caching](#caching)
3032
* [Contributing](#contributing)
@@ -54,6 +56,14 @@ $ bundle install
5456

5557
## Usage
5658

59+
### Rails Generator
60+
You can use the bundled generator if you are using the library inside of
61+
a Rails project:
62+
63+
rails g Serializer Movie name year
64+
65+
This will create a new serializer in `app/serializers/movie_serializer.rb`
66+
5767
### Model Definition
5868

5969
```ruby
@@ -68,6 +78,7 @@ end
6878
class MovieSerializer
6979
include FastJsonapi::ObjectSerializer
7080
set_type :movie # optional
81+
set_id :owner_id # optional
7182
attributes :name, :year
7283
has_many :actors
7384
belongs_to :owner, record_type: :user
@@ -104,7 +115,7 @@ json_string = MovieSerializer.new(movie).serialized_json
104115
```json
105116
{
106117
"data": {
107-
"id": "232",
118+
"id": "3",
108119
"type": "movie",
109120
"attributes": {
110121
"name": "test movie",
@@ -134,6 +145,65 @@ json_string = MovieSerializer.new(movie).serialized_json
134145
}
135146

136147
```
148+
149+
### Key Transforms
150+
By default fast_jsonapi underscores the key names. It supports the same key transforms that are supported by AMS. Here is the syntax of specifying a key transform
151+
152+
```ruby
153+
class MovieSerializer
154+
include FastJsonapi::ObjectSerializer
155+
# Available options :camel, :camel_lower, :dash, :underscore(default)
156+
set_key_transform :camel
157+
end
158+
```
159+
Here are examples of how these options transform the keys
160+
161+
```ruby
162+
set_key_transform :camel # "some_key" => "SomeKey"
163+
set_key_transform :camel_lower # "some_key" => "someKey"
164+
set_key_transform :dash # "some_key" => "some-key"
165+
set_key_transform :underscore # "some_key" => "some_key"
166+
```
167+
168+
### Attributes
169+
Attributes are defined in FastJsonapi using the `attributes` method. This method is also aliased as `attribute`, which is useful when defining a single attribute.
170+
171+
By default, attributes are read directly from the model property of the same name. In this example, `name` is expected to be a property of the object being serialized:
172+
173+
```ruby
174+
class MovieSerializer
175+
include FastJsonapi::ObjectSerializer
176+
177+
attribute :name
178+
end
179+
```
180+
181+
Custom attributes that must be serialized but do not exist on the model can be declared using Ruby block syntax:
182+
183+
```ruby
184+
class MovieSerializer
185+
include FastJsonapi::ObjectSerializer
186+
187+
attributes :name, :year
188+
189+
attribute :name_with_year do |object|
190+
"#{object.name} (#{object.year})"
191+
end
192+
end
193+
```
194+
195+
The block syntax can also be used to override the property on the object:
196+
197+
```ruby
198+
class MovieSerializer
199+
include FastJsonapi::ObjectSerializer
200+
201+
attribute :name do |object|
202+
"#{object.name} Part 2"
203+
end
204+
end
205+
```
206+
137207
### Compound Document
138208

139209
Support for top-level included member through ` options[:include] `.
@@ -169,69 +239,66 @@ end
169239
Option | Purpose | Example
170240
------------ | ------------- | -------------
171241
set_type | Type name of Object | ```set_type :movie ```
242+
set_id | ID of Object | ```set_id :owner_id ```
172243
cache_options | Hash to enable caching and set cache length | ```cache_options enabled: true, cache_length: 12.hours```
173244
id_method_name | Set custom method name to get ID of an object | ```has_many :locations, id_method_name: :place_ids ```
174245
object_method_name | Set custom method name to get related objects | ```has_many :locations, object_method_name: :places ```
175246
record_type | Set custom Object Type for a relationship | ```belongs_to :owner, record_type: :user```
176247
serializer | Set custom Serializer for a relationship | ```has_many :actors, serializer: :custom_actor```
177248

249+
### Instrumentation
178250

179-
## Contributing
251+
`fast_jsonapi` also has builtin [Skylight](https://www.skylight.io/) integration. To enable, add the following to an initializer:
180252

181-
Please follow the steps on [contribution check](https://github.com/Netflix/fast_jsonapi/blob/master/CONTRIBUTING.md).
182-
This gem is built using a gem building gem called [juwelier](https://github.com/flajann2/juwelier).
253+
```ruby
254+
require 'fast_jsonapi/instrumentation/skylight'
255+
```
183256

184-
Beyond just editing source code, you’ll be interacting with the gem using rake a lot. To see all the tasks available with a brief description, you can run:
257+
Skylight relies on `ActiveSupport::Notifications` to track these two core methods. If you would like to use these notifications without using Skylight, simply require the instrumentation integration:
185258

186-
```bash
187-
rake -T
259+
```ruby
260+
require 'fast_jsonapi/instrumentation'
188261
```
189262

190-
### Updating Project information
191-
You can update the project information of the gem by updating the [Rakefile](Rakefile). Then you need to generate a new gemspec:
263+
The two instrumented notifcations are supplied by these two constants:
264+
* `FastJsonapi::ObjectSerializer::SERIALIZABLE_HASH_NOTIFICATION`
265+
* `FastJsonapi::ObjectSerializer::SERIALIZED_JSON_NOTIFICATION`
192266

193-
```bash
194-
rake gemspec
267+
It is also possible to instrument one method without the other by using one of the following require statements:
268+
269+
```ruby
270+
require 'fast_jsonapi/instrumentation/serializable_hash'
271+
require 'fast_jsonapi/instrumentation/serialized_json'
195272
```
196273

274+
Same goes for the Skylight integration:
275+
```ruby
276+
require 'fast_jsonapi/instrumentation/skylight/normalizers/serializable_hash'
277+
require 'fast_jsonapi/instrumentation/skylight/normalizers/serialized_json'
278+
```
279+
280+
## Contributing
281+
Please see [contribution check](https://github.com/Netflix/fast_jsonapi/blob/master/CONTRIBUTING.md) for more details on contributing
282+
197283
### Running Tests
198-
We use [RSpec](http://rspec.info/) for testing. We have unit tests, functional tests and performance tests. To run tests use the following rake task:
284+
We use [RSpec](http://rspec.info/) for testing. We have unit tests, functional tests and performance tests. To run tests use the following command:
199285

200286
```bash
201-
rake spec
287+
rspec
202288
```
203289

204-
### Installation
290+
To run tests without the performance tests (for quicker test runs):
205291

206292
```bash
207-
$ rake install
293+
rspec spec --tag ~performance:true
208294
```
209295

210-
The install rake task builds the gem and then installs it. You're all
211-
set if you're using [RVM](http://rvm.beginrescueend.com/), but you may
212-
need to run it with sudo if you have a system-installed Ruby:
213-
214-
### Bumping Version
215-
216-
It feels good to release code. Do it, do it often. But before that, bump
217-
the version. Then release it. There's a few ways to update the version:
296+
To run tests only performance tests:
218297

219298
```bash
220-
# version:write like before
221-
$ rake version:write MAJOR=0 MINOR=3 PATCH=0
222-
223-
# bump just major, ie 0.1.0 -> 1.0.0
224-
$ rake version:bump:major
225-
226-
# bump just minor, ie 0.1.0 -> 0.2.0
227-
$ rake version:bump:minor
228-
229-
# bump just patch, ie 0.1.0 -> 0.1.1
230-
$ rake version:bump:patch
299+
rspec spec --tag performance:true
231300
```
232301

233-
---
234-
235302
### We're Hiring!
236303

237304
Join the Netflix Studio Engineering team and help us build gems like this!

0 commit comments

Comments
 (0)