simple_json_api 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +5 -1
- data/README.md +40 -1
- data/lib/simple_json_api/array_refinements.rb +1 -1
- data/lib/simple_json_api/array_serializer.rb +1 -1
- data/lib/simple_json_api/field_list.rb +1 -1
- data/lib/simple_json_api/include_list.rb +1 -1
- data/lib/simple_json_api/json_api_builder.rb +8 -16
- data/lib/simple_json_api/serializer.rb +1 -1
- data/lib/simple_json_api/version.rb +1 -1
- data/lib/simple_json_api.rb +18 -7
- data/simple_json_api.gemspec +0 -1
- data/test/test_helper.rb +4 -3
- data/test/unit/field_list_test.rb +7 -7
- data/test/unit/include_list_test.rb +2 -6
- metadata +2 -18
- data/lib/simple_json_api/association.rb +0 -10
- data/lib/simple_json_api/attribute.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a442243658287849495015d9e7d70033be758c9
|
4
|
+
data.tar.gz: 47fa80783b4c0689f3e26051a012aeda9e077caf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 39b59ac72db54031090ca63f62bdc8387cef3ce49039a49841ca7cb0df7cd0ea3972dbf88af350ed81b3c6b34e2d77e6baf2996eb3714525c35811f2942787e4
|
7
|
+
data.tar.gz: 5d3a296127944821174cc21c352ad079989899501268d34517499b4700e44b88642d639b83770a35f7c210d5ad86a4bcf3939a14687c9789457ac280bcb3aff0
|
data/.travis.yml
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
- 2.
|
3
|
+
- 2.1
|
4
|
+
- 2.2
|
4
5
|
env:
|
5
6
|
- 'RAILS_VERSION=4.0'
|
6
7
|
- 'RAILS_VERSION=4.1'
|
7
8
|
- 'RAILS_VERSION=4.2'
|
8
9
|
- 'RAILS_VERSION=master'
|
9
10
|
matrix:
|
11
|
+
exclude:
|
12
|
+
- rvm: 2.1
|
13
|
+
env: 'RAILS_VERSION=master'
|
10
14
|
allow_failures:
|
11
15
|
- env: 'RAILS_VERSION=master'
|
data/README.md
CHANGED
@@ -22,11 +22,50 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
### Define serializers
|
26
26
|
|
27
|
+
A serializer will need to be created for each resource. The serializer will defined the attributes and associations of the serialized json.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class ProjectSerializer < SimpleJsonApi::ResourceSerializer
|
31
|
+
serializes :projects, model: Project
|
32
|
+
attribute :id
|
33
|
+
attribute :name, key: :project_name
|
34
|
+
attribute :description
|
35
|
+
attribute :position
|
36
|
+
has_one :todolist
|
37
|
+
has_many :tags
|
38
|
+
def href
|
39
|
+
"http://example.com/projects/#{_object.id}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
Serializers are subclasses of `SimpleJsonApi::ResourceSerializer`. The `serializes` method defines the `root_class` for the object, specifying the model is optional, but may be necessary when the serializer cannot be determined for polymorphic associations.
|
45
|
+
`attribute` defines the attributes of the serializer, the optional `key` allows the name to be changed during serialization.
|
46
|
+
Associations are declared with `has_many`, `has_one`, and `belongs_to`. These can optionally set `polymorphic: true`. Also if the serializer cannot be determined by the name of the association, that is the name doesn't match any registered serializers, then the `serializer` parameter will need to be set.
|
47
|
+
|
48
|
+
### Using serializers
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
render json: SimpleJsonApi.render(
|
52
|
+
model: @projects,
|
53
|
+
serializer: ProjectSerializer,
|
54
|
+
fields: 'id,name',
|
55
|
+
include: 'todolist'
|
56
|
+
)
|
57
|
+
```
|
58
|
+
|
59
|
+
When rendering the model is the AR data to be serialized, if it is an Array the ArraySerializer will be used for the top collection.
|
60
|
+
`fields` and `include` follow the jsonapi spec. [Include](http://jsonapi.org/format/#fetching-includes) will be the associations to be included in the generated json. [Fields](http://jsonapi.org/format/#fetching-sparse-fieldsets) is the list of attributes that will be included. The primary key will always be included.
|
27
61
|
|
28
62
|
## TODO
|
29
63
|
|
64
|
+
0. Better documentation
|
65
|
+
1. Handle meta section
|
66
|
+
2. Handle links section
|
67
|
+
3. Optimize queries (caching?)
|
68
|
+
4. Handle PUTS/POSTS/DELETES
|
30
69
|
|
31
70
|
## Contributing
|
32
71
|
|
@@ -17,7 +17,7 @@ module SimpleJsonApi
|
|
17
17
|
def_delegators :@field_list, :fields_for
|
18
18
|
|
19
19
|
# TODO: sort: nil
|
20
|
-
def initialize(model
|
20
|
+
def initialize(model, serializer, options = {})
|
21
21
|
@model = model
|
22
22
|
handle_options(options, serializer)
|
23
23
|
@serializer = get_serializer(serializer)
|
@@ -28,12 +28,10 @@ module SimpleJsonApi
|
|
28
28
|
|
29
29
|
def handle_options(options, serializer)
|
30
30
|
@field_list = FieldList.new(
|
31
|
-
|
32
|
-
|
31
|
+
options.fetch(:fields, nil),
|
32
|
+
serializer
|
33
33
|
)
|
34
|
-
@include = IncludeList.new(
|
35
|
-
include: options.fetch(:include, nil)
|
36
|
-
).parse
|
34
|
+
@include = IncludeList.new(options.fetch(:include, nil)).parse
|
37
35
|
end
|
38
36
|
|
39
37
|
def as_json(options = nil)
|
@@ -64,23 +62,17 @@ module SimpleJsonApi
|
|
64
62
|
|
65
63
|
def add_to_linked(assoc_base, item, serializer)
|
66
64
|
linked = @linked[serializer._root_name] ||= []
|
67
|
-
return if linked.already_linked?(
|
68
|
-
linked << serializer.new(
|
69
|
-
object: item, builder: self, base: assoc_base
|
70
|
-
).serialize
|
65
|
+
return if linked.already_linked?(item.json_pk, item.json_id)
|
66
|
+
linked << serializer.new(item, self, nil, assoc_base).serialize
|
71
67
|
end
|
72
68
|
|
73
69
|
private
|
74
70
|
|
75
71
|
def get_serializer(serializer)
|
76
72
|
if use_array_serializer?
|
77
|
-
ArraySerializer.new(
|
78
|
-
object: @model,
|
79
|
-
each_serializer: serializer,
|
80
|
-
builder: self
|
81
|
-
)
|
73
|
+
ArraySerializer.new(@model, self, serializer)
|
82
74
|
else
|
83
|
-
serializer.new(
|
75
|
+
serializer.new(@model, self)
|
84
76
|
end
|
85
77
|
end
|
86
78
|
|
@@ -9,7 +9,7 @@ module SimpleJsonApi
|
|
9
9
|
attr_reader :_each_serializer
|
10
10
|
attr_reader :_object
|
11
11
|
|
12
|
-
def initialize(object
|
12
|
+
def initialize(object, builder, each_serializer = nil, base = nil)
|
13
13
|
@_object = object
|
14
14
|
@_builder = builder
|
15
15
|
@_each_serializer = each_serializer
|
data/lib/simple_json_api.rb
CHANGED
@@ -2,8 +2,8 @@ require 'simple_json_api/version'
|
|
2
2
|
|
3
3
|
require 'simple_json_api/active_record_refinements'
|
4
4
|
require 'simple_json_api/array_refinements'
|
5
|
-
require 'simple_json_api/association'
|
6
|
-
require 'simple_json_api/attribute'
|
5
|
+
# require 'simple_json_api/association'
|
6
|
+
# require 'simple_json_api/attribute'
|
7
7
|
require 'simple_json_api/dsl'
|
8
8
|
require 'simple_json_api/field_list'
|
9
9
|
require 'simple_json_api/include_list'
|
@@ -16,12 +16,23 @@ require 'simple_json_api/json_api_builder'
|
|
16
16
|
|
17
17
|
# SimpleJsonApi
|
18
18
|
module SimpleJsonApi
|
19
|
+
# Wrapper for a linked association
|
20
|
+
Association = Struct.new(:name, :type, :serializer, :polymorphic, :key) do
|
21
|
+
def key
|
22
|
+
name_s = name.to_s
|
23
|
+
(type == :has_many) ? name_s.pluralize.to_sym : name_s.singularize.to_sym
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Attribute
|
28
|
+
Attribute = Struct.new(:name, :key) do
|
29
|
+
def key
|
30
|
+
self[:key] || self[:name]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
19
34
|
# Main hook to generate json
|
20
35
|
def self.render(model:, serializer:, options: {})
|
21
|
-
JsonApiBuilder.new(
|
22
|
-
model: model,
|
23
|
-
serializer: serializer,
|
24
|
-
options: options
|
25
|
-
).to_json
|
36
|
+
JsonApiBuilder.new(model, serializer, options).to_json
|
26
37
|
end
|
27
38
|
end
|
data/simple_json_api.gemspec
CHANGED
@@ -29,6 +29,5 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.add_development_dependency 'diffy'
|
30
30
|
spec.add_development_dependency 'minitest'
|
31
31
|
spec.add_development_dependency 'minitest-reporters'
|
32
|
-
spec.add_development_dependency 'simplecov'
|
33
32
|
spec.add_development_dependency 'sqlite3'
|
34
33
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,16 +1,17 @@
|
|
1
1
|
require 'codeclimate-test-reporter'
|
2
|
+
CodeClimate::TestReporter.configuration.git_dir = '.'
|
2
3
|
CodeClimate::TestReporter.start
|
3
4
|
|
4
|
-
require 'bundler/setup'
|
5
|
-
|
6
5
|
require 'simplecov'
|
7
6
|
if ENV['COVERAGE']
|
8
7
|
SimpleCov.start do
|
9
8
|
add_filter '/test/'
|
10
9
|
end
|
11
|
-
SimpleCov.minimum_coverage
|
10
|
+
SimpleCov.minimum_coverage 98
|
12
11
|
end
|
13
12
|
|
13
|
+
require 'bundler/setup'
|
14
|
+
|
14
15
|
require 'minitest/autorun'
|
15
16
|
require 'minitest/reporters'
|
16
17
|
Minitest::Reporters.use!(
|
@@ -5,8 +5,8 @@ module SimpleJsonApi
|
|
5
5
|
describe 'FieldListTest' do
|
6
6
|
it 'should parse string field list' do
|
7
7
|
fl = FieldList.new(
|
8
|
-
|
9
|
-
|
8
|
+
'id,name',
|
9
|
+
ProjectSerializer
|
10
10
|
)
|
11
11
|
fl.fields_for(:projects).must_equal %w(id name)
|
12
12
|
fl.fields_for(:todos).must_equal nil
|
@@ -14,11 +14,11 @@ module SimpleJsonApi
|
|
14
14
|
|
15
15
|
it 'should parse hash field list' do
|
16
16
|
fl = FieldList.new(
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
{
|
18
|
+
'projects' => 'id,name',
|
19
|
+
'todos' => 'id,description'
|
20
|
+
},
|
21
|
+
ProjectSerializer
|
22
22
|
)
|
23
23
|
fl.fields_for(:projects).must_equal %w(id name)
|
24
24
|
fl.fields_for(:todos).must_equal %w(id description)
|
@@ -4,17 +4,13 @@ require 'test_helper'
|
|
4
4
|
module SimpleJsonApi
|
5
5
|
describe 'IncludeListTest' do
|
6
6
|
it 'should parse string include list' do
|
7
|
-
il = IncludeList.new(
|
8
|
-
include: 'todos,projects'
|
9
|
-
).parse
|
7
|
+
il = IncludeList.new('todos,projects').parse
|
10
8
|
assert il.include?(:projects)
|
11
9
|
refute il.include?(:todo_lists)
|
12
10
|
end
|
13
11
|
|
14
12
|
it 'should parse string for nested include list' do
|
15
|
-
il = IncludeList.new(
|
16
|
-
include: 'todos,projects.todolists'
|
17
|
-
).parse
|
13
|
+
il = IncludeList.new('todos,projects.todolists').parse
|
18
14
|
refute il.include?(:projects)
|
19
15
|
assert il.include?(:todos)
|
20
16
|
assert il.include?(:todolists, [:projects])
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_json_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gary Gordon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -122,20 +122,6 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
|
-
- !ruby/object:Gem::Dependency
|
126
|
-
name: simplecov
|
127
|
-
requirement: !ruby/object:Gem::Requirement
|
128
|
-
requirements:
|
129
|
-
- - ">="
|
130
|
-
- !ruby/object:Gem::Version
|
131
|
-
version: '0'
|
132
|
-
type: :development
|
133
|
-
prerelease: false
|
134
|
-
version_requirements: !ruby/object:Gem::Requirement
|
135
|
-
requirements:
|
136
|
-
- - ">="
|
137
|
-
- !ruby/object:Gem::Version
|
138
|
-
version: '0'
|
139
125
|
- !ruby/object:Gem::Dependency
|
140
126
|
name: sqlite3
|
141
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -170,8 +156,6 @@ files:
|
|
170
156
|
- lib/simple_json_api/active_record_refinements.rb
|
171
157
|
- lib/simple_json_api/array_refinements.rb
|
172
158
|
- lib/simple_json_api/array_serializer.rb
|
173
|
-
- lib/simple_json_api/association.rb
|
174
|
-
- lib/simple_json_api/attribute.rb
|
175
159
|
- lib/simple_json_api/dsl.rb
|
176
160
|
- lib/simple_json_api/field_list.rb
|
177
161
|
- lib/simple_json_api/include_list.rb
|
@@ -1,10 +0,0 @@
|
|
1
|
-
# SimpleJsonApi
|
2
|
-
module SimpleJsonApi
|
3
|
-
# Wrapper for a linked association
|
4
|
-
Association = Struct.new(:name, :type, :serializer, :polymorphic, :key) do
|
5
|
-
def key
|
6
|
-
name_s = name.to_s
|
7
|
-
(type == :has_many) ? name_s.pluralize.to_sym : name_s.singularize.to_sym
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|