xing-backend 0.0.18 → 0.0.19
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/paged.rb +35 -0
- data/lib/xing/serializers/paged_index.rb +35 -0
- data/lib/xing/serializers/paged_list.rb +14 -15
- data/lib/xing/services/page_wrapper.rb +25 -0
- data/spec/xing/mappers/base_spec.rb +1 -1
- data/spec/xing/serializers/paged_index_spec.rb +73 -0
- data/spec/xing/serializers/paged_list_spec.rb +55 -14
- data/spec_help/spec_helper.rb +2 -0
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7bb90034c2383f0866fde062f849ea3aabace59
|
4
|
+
data.tar.gz: a5823af9381a90361195f139eb80292cb085f738
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 43fd7dd223af84a296b348e0731aaaf800ff4b41b61068df3069dd9dd8f934cb4bdd4ec5228a9af313a107442a0ae3192598951da13ef40943beffef481b5382
|
7
|
+
data.tar.gz: d0b79eed9ed10eb308b65b0104c7fe6d93a36aa63853139157b98ade4445979fc05c65fbaa5f5fcc371d4d3d4c93245aae46837a31a80e8dc4f731117854195e
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Xing::Serializers
|
2
|
+
module Paged
|
3
|
+
def template_link
|
4
|
+
raise NotImplementedError,
|
5
|
+
"subclasses of Xing::Serializers::PagedList must override template_link to provide a path URL template with a single :page field\n" +
|
6
|
+
"usually you can do something like my_resource_path_rfc6570 (sometimes you'll need to also call .partial_expand)\n\n" +
|
7
|
+
"If you prefer, you can return nil from template_link and override page_link to do something else."
|
8
|
+
end
|
9
|
+
|
10
|
+
def total_pages
|
11
|
+
object.total_pages
|
12
|
+
end
|
13
|
+
|
14
|
+
def first_link
|
15
|
+
page_link({page: 1})
|
16
|
+
end
|
17
|
+
|
18
|
+
def last_link
|
19
|
+
page_link({page: total_pages})
|
20
|
+
end
|
21
|
+
|
22
|
+
def page_link(options)
|
23
|
+
template_link.expand(options)
|
24
|
+
end
|
25
|
+
|
26
|
+
def links
|
27
|
+
{
|
28
|
+
:self => self_link,
|
29
|
+
:first => first_link,
|
30
|
+
:last => last_link,
|
31
|
+
:template => template_link
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'xing/serializers/base'
|
2
|
+
require 'xing/serializers/paged'
|
3
|
+
|
4
|
+
module Xing::Serializers
|
5
|
+
# Serializes the reference index of a long paginated list. We assume the
|
6
|
+
# interface provided by Kaminari: the object to be serialized needs to
|
7
|
+
# respond to:
|
8
|
+
#
|
9
|
+
# current_page, limit_value, total_pages, total_count, each(and various
|
10
|
+
# Enumerable methods)
|
11
|
+
|
12
|
+
class PagedIndex < Base
|
13
|
+
include Paged
|
14
|
+
|
15
|
+
def self.total_called(name)
|
16
|
+
attributes name
|
17
|
+
alias_method name, :total_items
|
18
|
+
end
|
19
|
+
|
20
|
+
attributes :per_page, :total_pages, :total_items
|
21
|
+
|
22
|
+
def total_items
|
23
|
+
object.total_count
|
24
|
+
end
|
25
|
+
|
26
|
+
def per_page
|
27
|
+
object.limit_value
|
28
|
+
end
|
29
|
+
|
30
|
+
def self_link
|
31
|
+
raise NotImplementedError,
|
32
|
+
"subclasses of Xing::Serializers::PagedIndex must override self_link to provide a path URL to themselves"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -1,19 +1,19 @@
|
|
1
1
|
require 'xing/serializers/list'
|
2
|
+
require 'xing/serializers/paged'
|
2
3
|
|
3
4
|
module Xing::Serializers
|
4
|
-
class PagedList < List
|
5
|
-
def initialize(list, page_num, total_pages, options = {})
|
6
|
-
@page_num = page_num.to_i
|
7
|
-
@total_pages = total_pages
|
8
5
|
|
9
|
-
|
10
|
-
|
6
|
+
# Serializes a single page of a long paginated list. We assume the interface
|
7
|
+
# provided by Kaminari: the object to be serialized needs to respond to:
|
8
|
+
#
|
9
|
+
# current_page, limit_value, total_pages, total_count, each(and various
|
10
|
+
# Enumerable methods)
|
11
11
|
|
12
|
-
|
12
|
+
class PagedList < List
|
13
|
+
include Paged
|
13
14
|
|
14
|
-
def
|
15
|
-
|
16
|
-
"subclasses of Xing::Serializers::PagesData must override page_link to return the URL of a page based on a :page => Integer() hash"
|
15
|
+
def page_num
|
16
|
+
object.current_page
|
17
17
|
end
|
18
18
|
|
19
19
|
def next_link
|
@@ -25,11 +25,10 @@ module Xing::Serializers
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def links
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
}
|
28
|
+
super.merge(
|
29
|
+
:next => next_link,
|
30
|
+
:previous => previous_link
|
31
|
+
)
|
33
32
|
end
|
34
33
|
end
|
35
34
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Xing::Services
|
2
|
+
# If you want to use the PagedList serializers, but are using resources that
|
3
|
+
# aren't actually provided by Kaminari, you can instead feed them to
|
4
|
+
# PageWrapper and you should get everything you need
|
5
|
+
class PageWrapper
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
def initialize(list, page_num, total_items, per_page)
|
9
|
+
@list, @total_items, @per_page, @page_num = list, total_items, per_page, page_num
|
10
|
+
end
|
11
|
+
attr_reader :list, :total_items, :per_page, :page_num
|
12
|
+
|
13
|
+
alias current_page page_num
|
14
|
+
alias total_count total_items
|
15
|
+
alias limit_value per_page
|
16
|
+
|
17
|
+
def total_pages
|
18
|
+
(total_items / per_page.to_f).ceil
|
19
|
+
end
|
20
|
+
|
21
|
+
def each(*args, &block)
|
22
|
+
@list.each(*args, &block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'xing/serializers/paged_index'
|
2
|
+
require 'xing/services/page_wrapper'
|
3
|
+
|
4
|
+
describe Xing::Serializers::PagedIndex do
|
5
|
+
class PageIndexSerializer < Xing::Serializers::PagedIndex
|
6
|
+
def self_link
|
7
|
+
"url_for_index"
|
8
|
+
end
|
9
|
+
|
10
|
+
def template_link
|
11
|
+
Addressable::Template.new("/page{/page}")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
let :per_page do
|
16
|
+
3
|
17
|
+
end
|
18
|
+
|
19
|
+
let :list do
|
20
|
+
(1..per_page).map do |index|
|
21
|
+
double("a mock activemodel").tap do |model|
|
22
|
+
allow(model).to receive(:read_attribute_for_serialization).with(:name).and_return("Name #{index}!")
|
23
|
+
allow(model).to receive(:read_attribute_for_serialization).with(:position).and_return(index)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
let :page_num do 1 end
|
29
|
+
|
30
|
+
let :total_pages do 3 end
|
31
|
+
|
32
|
+
let :total_items do
|
33
|
+
total_pages * per_page - 1
|
34
|
+
end
|
35
|
+
|
36
|
+
let :json do
|
37
|
+
serializer.to_json
|
38
|
+
end
|
39
|
+
|
40
|
+
describe 'as_json' do
|
41
|
+
let :serializer do
|
42
|
+
PageIndexSerializer.new(Xing::Services::PageWrapper.new(list, page_num, total_items, per_page))
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should generate a JSON with the proper links and self" do
|
46
|
+
expect(parse_json(json, "links/self")).to eq("url_for_index")
|
47
|
+
expect(parse_json(json, "links/first")).to eq("/page/1")
|
48
|
+
expect(parse_json(json, "links/last")).to eq("/page/3")
|
49
|
+
expect(parse_json(json, "data/per_page")).to eq(per_page)
|
50
|
+
expect(parse_json(json, "data/total_pages")).to eq(total_pages)
|
51
|
+
expect(parse_json(json, "data/total_items")).to eq(total_items)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe 'using Kaminari' do
|
56
|
+
let :kaminari_list do
|
57
|
+
Kaminari.paginate_array((list * 3)[0..-2]).page(1).per(per_page)
|
58
|
+
end
|
59
|
+
|
60
|
+
let :serializer do
|
61
|
+
PageIndexSerializer.new(kaminari_list)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should generate a JSON with the proper links and self" do
|
65
|
+
expect(parse_json(json, "links/self")).to eq("url_for_index")
|
66
|
+
expect(parse_json(json, "links/first")).to eq("/page/1")
|
67
|
+
expect(parse_json(json, "links/last")).to eq("/page/3")
|
68
|
+
expect(parse_json(json, "data/per_page")).to eq(per_page)
|
69
|
+
expect(parse_json(json, "data/total_pages")).to eq(total_pages)
|
70
|
+
expect(parse_json(json, "data/total_items")).to eq(total_items)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -1,9 +1,6 @@
|
|
1
1
|
require 'xing/serializers/paged_list'
|
2
|
-
require 'json_spec'
|
3
2
|
|
4
3
|
describe Xing::Serializers::PagedList do
|
5
|
-
include JsonSpec::Matchers
|
6
|
-
|
7
4
|
class ItemSerializer < Xing::Serializers::Base
|
8
5
|
attributes :name, :position
|
9
6
|
def links
|
@@ -16,10 +13,8 @@ describe Xing::Serializers::PagedList do
|
|
16
13
|
ItemSerializer
|
17
14
|
end
|
18
15
|
|
19
|
-
|
20
|
-
|
21
|
-
page_num = options.delete(:page)
|
22
|
-
"page_link:#{page_num}-#{options.keys.join(",")}"
|
16
|
+
def template_link
|
17
|
+
Addressable::Template.new("/page{/page}")
|
23
18
|
end
|
24
19
|
|
25
20
|
def self_link
|
@@ -27,8 +22,12 @@ describe Xing::Serializers::PagedList do
|
|
27
22
|
end
|
28
23
|
end
|
29
24
|
|
25
|
+
let :per_page do
|
26
|
+
3
|
27
|
+
end
|
28
|
+
|
30
29
|
let :list do
|
31
|
-
(1..
|
30
|
+
(1..per_page).map do |index|
|
32
31
|
double("a mock activemodel").tap do |model|
|
33
32
|
allow(model).to receive(:read_attribute_for_serialization).with(:name).and_return("Name #{index}!")
|
34
33
|
allow(model).to receive(:read_attribute_for_serialization).with(:position).and_return(index)
|
@@ -38,8 +37,12 @@ describe Xing::Serializers::PagedList do
|
|
38
37
|
|
39
38
|
let :total_pages do 3 end
|
40
39
|
|
40
|
+
let :total_items do
|
41
|
+
total_pages * per_page - 1
|
42
|
+
end
|
43
|
+
|
41
44
|
let :serializer do
|
42
|
-
PageSerializer.new(list, page_num,
|
45
|
+
PageSerializer.new(Xing::Services::PageWrapper.new(list, page_num, total_items, per_page))
|
43
46
|
end
|
44
47
|
|
45
48
|
let :json do
|
@@ -57,11 +60,49 @@ describe Xing::Serializers::PagedList do
|
|
57
60
|
end
|
58
61
|
|
59
62
|
it "should generate a JSON with the proper links and self" do
|
60
|
-
expect(json).to
|
61
|
-
expect(json).to
|
62
|
-
expect(json).to
|
63
|
-
expect(json).to
|
64
|
-
expect(json).to
|
63
|
+
expect(parse_json(json, "links/self")).to eq("url_for_this_page")
|
64
|
+
expect(parse_json(json, "links/next")).to eq("/page/2")
|
65
|
+
expect(parse_json(json, "links/first")).to eq("/page/1")
|
66
|
+
expect(parse_json(json, "links/last")).to eq("/page/3")
|
67
|
+
expect(parse_json(json, "data/1/data/name")).to eq("Name 2!")
|
68
|
+
expect(parse_json(json, "data/1/data/position")).to eq(2)
|
69
|
+
expect(parse_json(json, "data/1/links/self")).to eq("url_for_this_model")
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should call the video response serializer" do
|
73
|
+
expect(ItemSerializer).to receive(:new).with(list[0], anything)
|
74
|
+
expect(ItemSerializer).to receive(:new).with(list[1], anything)
|
75
|
+
expect(ItemSerializer).to receive(:new).with(list[2], anything)
|
76
|
+
expect(json).to have_json_size(3).at_path('data')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe 'using Kaminara' do
|
81
|
+
let :kaminari_list do
|
82
|
+
Kaminari.paginate_array(list * 3).page(1).per(per_page)
|
83
|
+
end
|
84
|
+
|
85
|
+
let :serializer do
|
86
|
+
PageSerializer.new(kaminari_list)
|
87
|
+
end
|
88
|
+
|
89
|
+
let :page_num do 1 end
|
90
|
+
|
91
|
+
it "should have the correct structure" do
|
92
|
+
expect(json).to have_json_path('links/self')
|
93
|
+
expect(json).to have_json_path('links/next')
|
94
|
+
expect(json).to have_json_path('links/previous')
|
95
|
+
expect(json).to have_json_path('data/')
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should generate a JSON with the proper links and self" do
|
99
|
+
expect(parse_json(json, "links/self")).to eq("url_for_this_page")
|
100
|
+
expect(parse_json(json, "links/next")).to eq("/page/2")
|
101
|
+
expect(parse_json(json, "links/first")).to eq("/page/1")
|
102
|
+
expect(parse_json(json, "links/last")).to eq("/page/3")
|
103
|
+
expect(parse_json(json, "data/1/data/name")).to eq("Name 2!")
|
104
|
+
expect(parse_json(json, "data/1/data/position")).to eq(2)
|
105
|
+
expect(parse_json(json, "data/1/links/self")).to eq("url_for_this_model")
|
65
106
|
end
|
66
107
|
|
67
108
|
it "should call the video response serializer" do
|
data/spec_help/spec_helper.rb
CHANGED
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.19
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan Dorn
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date:
|
14
|
+
date: 2016-01-05 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: rails
|
@@ -135,12 +135,15 @@ files:
|
|
135
135
|
- lib/xing/serializers.rb
|
136
136
|
- lib/xing/serializers/base.rb
|
137
137
|
- lib/xing/serializers/list.rb
|
138
|
+
- lib/xing/serializers/paged.rb
|
139
|
+
- lib/xing/serializers/paged_index.rb
|
138
140
|
- lib/xing/serializers/paged_list.rb
|
139
141
|
- lib/xing/serializers/root_resources.rb
|
140
142
|
- lib/xing/services.rb
|
141
143
|
- lib/xing/services/error_converter.rb
|
142
144
|
- lib/xing/services/json_tree_lister.rb
|
143
145
|
- lib/xing/services/locator.rb
|
146
|
+
- lib/xing/services/page_wrapper.rb
|
144
147
|
- lib/xing/services/snapshot_fetcher.rb
|
145
148
|
- lib/xing/services/snapshot_writer.rb
|
146
149
|
- spec/xing/builders/list_builder_spec.rb
|
@@ -149,6 +152,7 @@ files:
|
|
149
152
|
- spec/xing/controllers/root_resources_controller_spec.rb
|
150
153
|
- spec/xing/mappers/base_spec.rb
|
151
154
|
- spec/xing/serializers/base_spec.rb
|
155
|
+
- spec/xing/serializers/paged_index_spec.rb
|
152
156
|
- spec/xing/serializers/paged_list_spec.rb
|
153
157
|
- spec/xing/serializers/root_resources_spec.rb
|
154
158
|
- spec/xing/services/error_converter_spec.rb
|
@@ -200,7 +204,7 @@ rdoc_options:
|
|
200
204
|
- "--main"
|
201
205
|
- doc/README
|
202
206
|
- "--title"
|
203
|
-
- xing-backend-0.0.
|
207
|
+
- xing-backend-0.0.19 Documentation
|
204
208
|
require_paths:
|
205
209
|
- lib/
|
206
210
|
required_ruby_version: !ruby/object:Gem::Requirement
|