xing-backend 0.0.11 → 0.0.12
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/lib/xing/serializers.rb +1 -0
- data/lib/xing/serializers/paged_list.rb +55 -0
- data/spec/xing/serializers/base_spec.rb +0 -1
- data/spec/xing/serializers/paged_list_spec.rb +118 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 827d47919fcc3646787ebec8fd234016afbddb78
|
4
|
+
data.tar.gz: 270b23ee03b4fcb2adfae0993069dd12f5c38bef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af3d2370c38452056e039a5e8409ed3602ef9f2ffc4633270695b551cdba582a9a48446f907bf9c5e17370907b950c75f0691208e9bafad957f3ae7a9d733cbb
|
7
|
+
data.tar.gz: c4dcade49aeac1b828d90f2df37e6ef56e0734acdc44bcc37869a2fb0e7dec7d75c302cf7f369d728266514e7a0122a1f8b154318ec370d7ef6c4d123f7dc2a5
|
data/lib/xing/serializers.rb
CHANGED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'xing/serializers/base'
|
2
|
+
|
3
|
+
module Xing::Serializers
|
4
|
+
class PagedList < Base
|
5
|
+
def initialize(list, page_num, total_pages, options = {})
|
6
|
+
@page_num = page_num.to_i
|
7
|
+
@total_pages = total_pages
|
8
|
+
|
9
|
+
super(list, options)
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :page_num, :total_pages
|
13
|
+
|
14
|
+
def item_serializer_class
|
15
|
+
raise NotImplemented,
|
16
|
+
"subclasses of Xing::Serializers::PagesData must override item_serializer_class to refer to the per-item serializer"
|
17
|
+
end
|
18
|
+
|
19
|
+
def item_serializer_options
|
20
|
+
{}
|
21
|
+
end
|
22
|
+
|
23
|
+
def page_link(options)
|
24
|
+
raise NotImplemented,
|
25
|
+
"subclasses of Xing::Serializers::PagesData must override page_link to return the URL of a page based on a :page => Integer() hash"
|
26
|
+
end
|
27
|
+
|
28
|
+
def self_link
|
29
|
+
raise NotImplemented,
|
30
|
+
"subclasses of Xing::Serializers::PagesData must override self_link to return their own path"
|
31
|
+
end
|
32
|
+
|
33
|
+
def as_json_without_wrap(options={})
|
34
|
+
object.map do |item|
|
35
|
+
item_serializer_class.new(item, item_serializer_options).as_json
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def next_link
|
40
|
+
page_link({page: page_num + 1}) unless page_num == total_pages
|
41
|
+
end
|
42
|
+
|
43
|
+
def previous_link
|
44
|
+
page_link({page: page_num - 1}) unless page_num == 1
|
45
|
+
end
|
46
|
+
|
47
|
+
def links
|
48
|
+
{
|
49
|
+
:self => self_link,
|
50
|
+
:next => next_link,
|
51
|
+
:previous => previous_link
|
52
|
+
}
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'xing/serializers/paged_list'
|
2
|
+
require 'json_spec'
|
3
|
+
|
4
|
+
describe Xing::Serializers::PagedList do
|
5
|
+
include JsonSpec::Matchers
|
6
|
+
|
7
|
+
class ItemSerializer < Xing::Serializers::Base
|
8
|
+
attributes :name, :position
|
9
|
+
def links
|
10
|
+
{ :self => 'url_for_this_model' }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class PageSerializer < Xing::Serializers::PagedList
|
15
|
+
def item_serializer_class
|
16
|
+
ItemSerializer
|
17
|
+
end
|
18
|
+
|
19
|
+
# Structures a testable "link" text
|
20
|
+
def page_link(options)
|
21
|
+
page_num = options.delete(:page)
|
22
|
+
"page_link:#{page_num}-#{options.keys.join(",")}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def self_link
|
26
|
+
"url_for_this_page"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
let :list do
|
31
|
+
(1..3).map do |index|
|
32
|
+
double("a mock activemodel").tap do |model|
|
33
|
+
allow(model).to receive(:read_attribute_for_serialization).with(:name).and_return("Name #{index}!")
|
34
|
+
allow(model).to receive(:read_attribute_for_serialization).with(:position).and_return(index)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
let :total_pages do 3 end
|
40
|
+
|
41
|
+
let :serializer do
|
42
|
+
PageSerializer.new(list, page_num, total_pages)
|
43
|
+
end
|
44
|
+
|
45
|
+
let :json do
|
46
|
+
serializer.to_json
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'as_json' do
|
50
|
+
let :page_num do 1 end
|
51
|
+
|
52
|
+
it "should have the correct structure" do
|
53
|
+
expect(json).to have_json_path('links/self')
|
54
|
+
expect(json).to have_json_path('links/next')
|
55
|
+
expect(json).to have_json_path('links/previous')
|
56
|
+
expect(json).to have_json_path('data/')
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should generate a JSON with the proper links and self" do
|
60
|
+
expect(json).to be_json_eql('"url_for_this_page"').at_path('links/self')
|
61
|
+
expect(json).to be_json_eql('"page_link:2-"').at_path('links/next')
|
62
|
+
expect(json).to be_json_eql('"Name 2!"').at_path('data/1/data/name')
|
63
|
+
expect(json).to be_json_eql('2').at_path('data/1/data/position')
|
64
|
+
expect(json).to be_json_eql('"url_for_this_model"').at_path('data/1/links/self')
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should call the video response serializer" do
|
68
|
+
expect(ItemSerializer).to receive(:new).with(list[0], anything)
|
69
|
+
expect(ItemSerializer).to receive(:new).with(list[1], anything)
|
70
|
+
expect(ItemSerializer).to receive(:new).with(list[2], anything)
|
71
|
+
expect(json).to have_json_size(3).at_path('data')
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe 'links' do
|
76
|
+
context 'first page' do
|
77
|
+
let :page_num do
|
78
|
+
1
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'should not have a previous link' do
|
82
|
+
expect(serializer.previous_link).to be_nil
|
83
|
+
end
|
84
|
+
|
85
|
+
it 'should have a next link' do
|
86
|
+
expect(serializer.next_link).to_not be_nil
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context 'last page' do
|
91
|
+
let :page_num do
|
92
|
+
3
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should have a previous link' do
|
96
|
+
expect(serializer.previous_link).to_not be_nil
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should not have a next link' do
|
100
|
+
expect(serializer.next_link).to be_nil
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'neither first page or last page' do
|
105
|
+
let :page_num do
|
106
|
+
2
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should have a previous link' do
|
110
|
+
expect(serializer.previous_link).to_not be_nil
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'should have a next link' do
|
114
|
+
expect(serializer.next_link).to_not be_nil
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xing-backend
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan Dorn
|
@@ -19,14 +19,14 @@ dependencies:
|
|
19
19
|
requirements:
|
20
20
|
- - "~>"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 4.2.
|
22
|
+
version: 4.2.1
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
27
|
- - "~>"
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 4.2.
|
29
|
+
version: 4.2.1
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: active_model_serializers
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -132,6 +132,7 @@ files:
|
|
132
132
|
- lib/xing/mappers/base.rb
|
133
133
|
- lib/xing/serializers.rb
|
134
134
|
- lib/xing/serializers/base.rb
|
135
|
+
- lib/xing/serializers/paged_list.rb
|
135
136
|
- lib/xing/serializers/root_resources.rb
|
136
137
|
- lib/xing/services.rb
|
137
138
|
- lib/xing/services/error_converter.rb
|
@@ -148,6 +149,7 @@ files:
|
|
148
149
|
- spec/xing/controllers/root_resources_controller_spec.rb
|
149
150
|
- spec/xing/mappers/base_spec.rb
|
150
151
|
- spec/xing/serializers/base_spec.rb
|
152
|
+
- spec/xing/serializers/paged_list_spec.rb
|
151
153
|
- spec/xing/serializers/root_resources_spec.rb
|
152
154
|
- spec/xing/services/error_converter_spec.rb
|
153
155
|
- spec/xing/services/json_tree_lister_spec.rb
|
@@ -200,7 +202,7 @@ rdoc_options:
|
|
200
202
|
- "--main"
|
201
203
|
- doc/README
|
202
204
|
- "--title"
|
203
|
-
- xing-backend-0.0.
|
205
|
+
- xing-backend-0.0.12 Documentation
|
204
206
|
require_paths:
|
205
207
|
- lib/
|
206
208
|
required_ruby_version: !ruby/object:Gem::Requirement
|