restpack_serializer 0.4.6 → 0.4.7
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/README.md +21 -8
- data/lib/restpack_serializer/options.rb +3 -4
- data/lib/restpack_serializer/serializable/paging.rb +3 -3
- data/lib/restpack_serializer/serializable/side_loading.rb +2 -2
- data/lib/restpack_serializer/version.rb +1 -1
- data/spec/serializable/options_spec.rb +4 -4
- data/spec/serializable/paging_spec.rb +7 -7
- data/spec/serializable/resource_spec.rb +2 -2
- data/spec/serializable/side_loading/belongs_to_spec.rb +3 -3
- data/spec/serializable/side_loading/has_many_spec.rb +2 -2
- data/spec/serializable/side_loading/side_loading_spec.rb +3 -3
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3daa5bd16f4c61dc8e0e46a1aa91234ea6eee250
|
4
|
+
data.tar.gz: 0ccaa8526f5811905ca6aee4c6c762fedd2576e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac15ac3b387840b892689ccb93047f7356bf0e65a493228efde51916a1c67f4c196d59852b26519fa01975b8ab75ea6ee80e20f3e38ba532a4563c92afbd106c
|
7
|
+
data.tar.gz: 6cb929625f66ff9ae579b6d28d46a9c75d771a59f9b1f16681aa7b7b097a3d400d3c93931be4d4f54ea99d6918a3cc429b1643956fa5eec0977aed137f8beb65
|
data/README.md
CHANGED
@@ -120,7 +120,7 @@ http://restpack-serializer-sample.herokuapp.com/songs.json?page=2&page_size=3 yi
|
|
120
120
|
"page": 2,
|
121
121
|
"page_size": 3,
|
122
122
|
"count": 42,
|
123
|
-
"
|
123
|
+
"include": [],
|
124
124
|
"page_count": 14,
|
125
125
|
"previous_page": 1,
|
126
126
|
"next_page": 3,
|
@@ -159,7 +159,7 @@ class AlbumSerializer
|
|
159
159
|
end
|
160
160
|
```
|
161
161
|
|
162
|
-
In this example, we are allowing related `songs` and `artists` to be included in API responses. Side-loads can be specifed by using the `
|
162
|
+
In this example, we are allowing related `songs` and `artists` to be included in API responses. Side-loads can be specifed by using the `include` parameter:
|
163
163
|
|
164
164
|
#### No side-loads
|
165
165
|
|
@@ -167,7 +167,7 @@ In this example, we are allowing related `songs` and `artists` to be included in
|
|
167
167
|
|
168
168
|
#### Side-load related Artists
|
169
169
|
|
170
|
-
* http://restpack-serializer-sample.herokuapp.com/albums.json?
|
170
|
+
* http://restpack-serializer-sample.herokuapp.com/albums.json?include=artists
|
171
171
|
|
172
172
|
which yields:
|
173
173
|
|
@@ -216,7 +216,7 @@ which yields:
|
|
216
216
|
"page": 1,
|
217
217
|
"page_size": 10,
|
218
218
|
"count": 4,
|
219
|
-
"
|
219
|
+
"include": [
|
220
220
|
"artists"
|
221
221
|
],
|
222
222
|
"page_count": 1,
|
@@ -269,7 +269,7 @@ which yields:
|
|
269
269
|
|
270
270
|
#### Side-load related Songs
|
271
271
|
|
272
|
-
* http://restpack-serializer-sample.herokuapp.com/albums.json?
|
272
|
+
* http://restpack-serializer-sample.herokuapp.com/albums.json?include=songs
|
273
273
|
|
274
274
|
An album `:has_many` songs, so the side-loaded songs are paged. The `meta.songs` includes `previous_href` and `next_href` which point to the previous and next page of this side-loaded data. These URLs take the form:
|
275
275
|
|
@@ -277,11 +277,11 @@ An album `:has_many` songs, so the side-loaded songs are paged. The `meta.songs`
|
|
277
277
|
|
278
278
|
#### Side-load related Artists and Songs
|
279
279
|
|
280
|
-
* http://restpack-serializer-sample.herokuapp.com/albums.json?
|
280
|
+
* http://restpack-serializer-sample.herokuapp.com/albums.json?include=artists,songs
|
281
281
|
|
282
282
|
## Filtering
|
283
283
|
|
284
|
-
Simple filtering based on primary and foreign keys is
|
284
|
+
Simple filtering based on primary and foreign keys is supported by default:
|
285
285
|
|
286
286
|
#### By primary key:
|
287
287
|
|
@@ -293,6 +293,19 @@ Simple filtering based on primary and foreign keys is possible:
|
|
293
293
|
* http://restpack-serializer-sample.herokuapp.com/albums.json?artist_id=1
|
294
294
|
* http://restpack-serializer-sample.herokuapp.com/albums.json?artist_ids=2,3
|
295
295
|
|
296
|
+
#### Custom filters:
|
297
|
+
|
298
|
+
Custom filters can be defined with the `can_filter_by` option:
|
299
|
+
|
300
|
+
```ruby
|
301
|
+
class Account
|
302
|
+
include RestPack::Serializer
|
303
|
+
attributes :id, :application_id, :created_by, :name, :href
|
304
|
+
|
305
|
+
can_filter_by :application_id
|
306
|
+
end
|
307
|
+
```
|
308
|
+
|
296
309
|
Side-loading is available when filtering:
|
297
310
|
|
298
|
-
* http://restpack-serializer-sample.herokuapp.com/albums.json?artist_ids=2,3&
|
311
|
+
* http://restpack-serializer-sample.herokuapp.com/albums.json?artist_ids=2,3&include=artists,songs
|
@@ -1,13 +1,13 @@
|
|
1
1
|
module RestPack::Serializer
|
2
2
|
class Options
|
3
|
-
attr_accessor :page, :page_size, :
|
3
|
+
attr_accessor :page, :page_size, :include, :filters, :serializer, :model_class, :scope, :include_links
|
4
4
|
|
5
5
|
def initialize(serializer, params = {}, scope = nil)
|
6
6
|
params.symbolize_keys! if params.respond_to?(:symbolize_keys!)
|
7
7
|
|
8
8
|
@page = 1
|
9
9
|
@page_size = RestPack::Serializer.config.page_size
|
10
|
-
@
|
10
|
+
@include = []
|
11
11
|
@filters = filters_from_params(params, serializer)
|
12
12
|
@serializer = serializer
|
13
13
|
@model_class = serializer.model_class
|
@@ -16,7 +16,7 @@ module RestPack::Serializer
|
|
16
16
|
|
17
17
|
@page = params[:page].to_i if params[:page]
|
18
18
|
@page_size = params[:page_size].to_i if params[:page_size]
|
19
|
-
@
|
19
|
+
@include = params[:include].split(',').map(&:to_sym) if params[:include]
|
20
20
|
end
|
21
21
|
|
22
22
|
def scope_with_filters
|
@@ -51,6 +51,5 @@ module RestPack::Serializer
|
|
51
51
|
end
|
52
52
|
filters
|
53
53
|
end
|
54
|
-
|
55
54
|
end
|
56
55
|
end
|
@@ -21,7 +21,7 @@ module RestPack::Serializer::Paging
|
|
21
21
|
|
22
22
|
if options.include_links
|
23
23
|
result[:links] = self.links
|
24
|
-
Array(RestPack::Serializer::Factory.create(*options.
|
24
|
+
Array(RestPack::Serializer::Factory.create(*options.include)).each do |serializer|
|
25
25
|
result[:links].merge! serializer.class.links
|
26
26
|
end
|
27
27
|
end
|
@@ -42,7 +42,7 @@ module RestPack::Serializer::Paging
|
|
42
42
|
page: options.page,
|
43
43
|
page_size: options.page_size,
|
44
44
|
count: page.total_entries,
|
45
|
-
|
45
|
+
include: options.include
|
46
46
|
}
|
47
47
|
|
48
48
|
meta[:page_count] = ((page.total_entries - 1) / options.page_size) + 1
|
@@ -62,7 +62,7 @@ module RestPack::Serializer::Paging
|
|
62
62
|
params = []
|
63
63
|
params << "page=#{page}" unless page == 1
|
64
64
|
params << "page_size=#{options.page_size}" unless options.default_page_size?
|
65
|
-
params << "
|
65
|
+
params << "include=#{options.include.join(',')}" if options.include.any?
|
66
66
|
params << options.filters_as_url_params if options.filters.any?
|
67
67
|
|
68
68
|
url += '?' + params.join('&') if params.any?
|
@@ -6,9 +6,9 @@ module RestPack::Serializer::SideLoading
|
|
6
6
|
side_loads = {
|
7
7
|
:meta => { }
|
8
8
|
}
|
9
|
-
return side_loads if models.empty? || options.
|
9
|
+
return side_loads if models.empty? || options.include.nil?
|
10
10
|
|
11
|
-
options.
|
11
|
+
options.include.each do |include|
|
12
12
|
side_load_data = side_load(include, models, options)
|
13
13
|
side_loads[:meta].merge!(side_load_data[:meta] || {})
|
14
14
|
side_loads.merge! side_load_data.except(:meta)
|
@@ -7,7 +7,7 @@ describe RestPack::Serializer::Options do
|
|
7
7
|
|
8
8
|
describe 'default values' do
|
9
9
|
it { subject.model_class.should == MyApp::Song }
|
10
|
-
it { subject.
|
10
|
+
it { subject.include.should == [] }
|
11
11
|
it { subject.page.should == 1 }
|
12
12
|
it { subject.page_size.should == 10 }
|
13
13
|
it { subject.filters.should == {} }
|
@@ -22,9 +22,9 @@ describe RestPack::Serializer::Options do
|
|
22
22
|
it { subject.page_size.should == 8 }
|
23
23
|
end
|
24
24
|
|
25
|
-
describe 'with
|
26
|
-
let(:params) { { '
|
27
|
-
it { subject.
|
25
|
+
describe 'with include' do
|
26
|
+
let(:params) { { 'include' => 'model1,model2' } }
|
27
|
+
it { subject.include.should == [:model1, :model2] }
|
28
28
|
end
|
29
29
|
|
30
30
|
context 'with filters' do
|
@@ -98,29 +98,29 @@ describe RestPack::Serializer::Paging do
|
|
98
98
|
end
|
99
99
|
|
100
100
|
context "when sideloading" do
|
101
|
-
let(:params) { {
|
101
|
+
let(:params) { { include: 'albums' } }
|
102
102
|
|
103
103
|
it "includes side-loaded models" do
|
104
104
|
page[:albums].should_not == nil
|
105
105
|
end
|
106
106
|
|
107
107
|
it "includes the side-loads in the main meta data" do
|
108
|
-
page[:meta][:songs][:
|
108
|
+
page[:meta][:songs][:include].should == [:albums]
|
109
109
|
end
|
110
110
|
|
111
111
|
it "includes the side-loads in page hrefs" do
|
112
|
-
page[:meta][:songs][:next_href].should == '/songs.json?page=2&
|
112
|
+
page[:meta][:songs][:next_href].should == '/songs.json?page=2&include=albums'
|
113
113
|
end
|
114
114
|
|
115
115
|
context "with includes as comma delimited string" do
|
116
|
-
let(:params) { {
|
116
|
+
let(:params) { { include: "albums,artists" } }
|
117
117
|
it "includes side-loaded models" do
|
118
118
|
page[:albums].should_not == nil
|
119
119
|
page[:artists].should_not == nil
|
120
120
|
end
|
121
121
|
|
122
122
|
it "includes the side-loads in page hrefs" do
|
123
|
-
page[:meta][:songs][:next_href].should == '/songs.json?page=2&
|
123
|
+
page[:meta][:songs][:next_href].should == '/songs.json?page=2&include=albums,artists'
|
124
124
|
end
|
125
125
|
|
126
126
|
it "includes links" do
|
@@ -195,7 +195,7 @@ describe RestPack::Serializer::Paging do
|
|
195
195
|
|
196
196
|
context "paging with paged side-load" do
|
197
197
|
let(:page) { MyApp::AlbumSerializer.page_with_options(options) }
|
198
|
-
let(:options) { RestPack::Serializer::Options.new(MyApp::AlbumSerializer, {
|
198
|
+
let(:options) { RestPack::Serializer::Options.new(MyApp::AlbumSerializer, { include: 'songs' }) }
|
199
199
|
|
200
200
|
it "includes side-loaded paging data in meta data" do
|
201
201
|
page[:meta][:albums].should_not == nil
|
@@ -207,7 +207,7 @@ describe RestPack::Serializer::Paging do
|
|
207
207
|
|
208
208
|
context "paging with two paged side-loads" do
|
209
209
|
let(:page) { MyApp::ArtistSerializer.page_with_options(options) }
|
210
|
-
let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, {
|
210
|
+
let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { include: 'albums,songs' }) }
|
211
211
|
|
212
212
|
it "includes side-loaded paging data in meta data" do
|
213
213
|
page[:meta][:albums].should_not == nil
|
@@ -15,7 +15,7 @@ describe RestPack::Serializer::Resource do
|
|
15
15
|
end
|
16
16
|
|
17
17
|
describe "side-loading" do
|
18
|
-
let(:params) { { id: @song.id,
|
18
|
+
let(:params) { { id: @song.id, include: 'albums' } }
|
19
19
|
|
20
20
|
it "includes side-loaded models" do
|
21
21
|
resource[:albums].count.should == 1
|
@@ -23,7 +23,7 @@ describe RestPack::Serializer::Resource do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it "includes the side-loads in the main meta data" do
|
26
|
-
resource[:meta][:songs][:
|
26
|
+
resource[:meta][:songs][:include].should == [:albums]
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -22,7 +22,7 @@ describe RestPack::Serializer::SideLoading do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
context "when including :albums" do
|
25
|
-
let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "
|
25
|
+
let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "include" => "albums" }) }
|
26
26
|
|
27
27
|
it "returns a hash with no data" do
|
28
28
|
side_loads.should == { :meta => {} }
|
@@ -34,7 +34,7 @@ describe RestPack::Serializer::SideLoading do
|
|
34
34
|
let(:models) { [MyApp::Song.first] }
|
35
35
|
|
36
36
|
context "when including :albums" do
|
37
|
-
let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "
|
37
|
+
let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "include" => "albums" }) }
|
38
38
|
|
39
39
|
it "returns side-loaded albums" do
|
40
40
|
side_loads.should == {
|
@@ -53,7 +53,7 @@ describe RestPack::Serializer::SideLoading do
|
|
53
53
|
let(:models) { [song1, song2] }
|
54
54
|
|
55
55
|
context "when including :albums" do
|
56
|
-
let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "
|
56
|
+
let(:options) { RestPack::Serializer::Options.new(MyApp::SongSerializer, { "include" => "albums" }) }
|
57
57
|
|
58
58
|
it "returns side-loaded albums" do
|
59
59
|
side_loads.should == {
|
@@ -14,7 +14,7 @@ describe RestPack::Serializer::SideLoading do
|
|
14
14
|
let(:models) { [@artist1] }
|
15
15
|
|
16
16
|
context "when including :albums" do
|
17
|
-
let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "
|
17
|
+
let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "include" => "albums" }) }
|
18
18
|
|
19
19
|
it "returns side-loaded albums" do
|
20
20
|
side_loads[:albums].count.should == @artist1.albums.count
|
@@ -28,7 +28,7 @@ describe RestPack::Serializer::SideLoading do
|
|
28
28
|
let(:models) { [@artist1, @artist2] }
|
29
29
|
|
30
30
|
context "when including :albums" do
|
31
|
-
let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "
|
31
|
+
let(:options) { RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "include" => "albums" }) }
|
32
32
|
|
33
33
|
it "returns side-loaded albums" do
|
34
34
|
expected_count = @artist1.albums.count + @artist2.albums.count
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe RestPack::Serializer::SideLoading do
|
4
|
-
context "invalid :
|
4
|
+
context "invalid :include" do
|
5
5
|
before(:each) do
|
6
6
|
FactoryGirl.create(:song)
|
7
7
|
end
|
@@ -12,7 +12,7 @@ describe RestPack::Serializer::SideLoading do
|
|
12
12
|
message = ":wrong is not a valid include for MyApp::Song"
|
13
13
|
|
14
14
|
expect do
|
15
|
-
MyApp::SongSerializer.side_loads([MyApp::Song.first], RestPack::Serializer::Options.new(MyApp::SongSerializer, { "
|
15
|
+
MyApp::SongSerializer.side_loads([MyApp::Song.first], RestPack::Serializer::Options.new(MyApp::SongSerializer, { "include" => "wrong" }))
|
16
16
|
end.to raise_error(exception, message)
|
17
17
|
end
|
18
18
|
end
|
@@ -24,7 +24,7 @@ describe RestPack::Serializer::SideLoading do
|
|
24
24
|
message = ":payments is not a valid include for MyApp::Artist"
|
25
25
|
|
26
26
|
expect do
|
27
|
-
MyApp::ArtistSerializer.side_loads([payment.artist], RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "
|
27
|
+
MyApp::ArtistSerializer.side_loads([payment.artist], RestPack::Serializer::Options.new(MyApp::ArtistSerializer, { "include" => "payments" }))
|
28
28
|
end.to raise_error(exception, message)
|
29
29
|
end
|
30
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restpack_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gavin Joyce
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -267,7 +267,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
267
267
|
version: '0'
|
268
268
|
requirements: []
|
269
269
|
rubyforge_project:
|
270
|
-
rubygems_version: 2.0.
|
270
|
+
rubygems_version: 2.0.7
|
271
271
|
signing_key:
|
272
272
|
specification_version: 4
|
273
273
|
summary: Model serialization, paging, side-loading and filtering
|