hal_decorator 0.3.3 → 0.3.4
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
- checksums.yaml.gz.sig +0 -0
- data/lib/hal_decorator/collection.rb +4 -0
- data/lib/hal_decorator/pagination.rb +126 -0
- data/lib/hal_decorator/serializer.rb +37 -14
- data.tar.gz.sig +0 -0
- metadata +45 -4
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd9ad6b7f80ed3d5d036ca4c7472b88e0b803989
|
4
|
+
data.tar.gz: 3977c9f47eab6816f2f7db9a2e37886ca6cee24a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 422776e1d29392bf2f63d501cff3d68235d656c950e3964417e50ac6b333cb46e90eda5526c8048a0ebb363926c1e910b4a50941906662c44a03457ef6da20ac
|
7
|
+
data.tar.gz: 56aec582f8f8824cddbfc7d9d5db60baafb50c63f7c6c5a50d36e013726d9cff0c25865f09c3b0e21b1042db993de97ba4162acab0033ac930c87089608585f7
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
@@ -0,0 +1,126 @@
|
|
1
|
+
module HALDecorator
|
2
|
+
class << self
|
3
|
+
attr_accessor :paginate
|
4
|
+
end
|
5
|
+
|
6
|
+
# TODO: Support Kaminari and Will_paginate
|
7
|
+
|
8
|
+
class Pagination
|
9
|
+
|
10
|
+
class Uri
|
11
|
+
def self.parse(str)
|
12
|
+
uri = nil
|
13
|
+
query = nil
|
14
|
+
unless str.nil? || str.empty?
|
15
|
+
if m = str.match(%r(\A([^\?]+)\??([^\#]+)?.*\Z))
|
16
|
+
uri = m[1]
|
17
|
+
query = query_from_string m[2]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
new(uri, query)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.query_from_string(str)
|
24
|
+
return {} if str.nil? || str.empty?
|
25
|
+
str.split('&').each_with_object({}) do |pair, q|
|
26
|
+
key_value = pair.split('=')
|
27
|
+
q[key_value[0]] = key_value[1];
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(uri, query)
|
32
|
+
@uri = uri
|
33
|
+
@query = query
|
34
|
+
end
|
35
|
+
|
36
|
+
def +(query)
|
37
|
+
self.class.new(@uri, @query.merge(query))
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_s
|
41
|
+
return if @uri.nil?
|
42
|
+
@uri.dup.tap do |uri|
|
43
|
+
next if @query.nil? || @query.empty?
|
44
|
+
uri << "?" + @query.map { |k,v| "#{k}=#{v}" }.join('&')
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.paginate!(serialized, collection)
|
50
|
+
new(serialized, collection).call
|
51
|
+
end
|
52
|
+
|
53
|
+
def initialize(serialized, collection)
|
54
|
+
@serialized = serialized
|
55
|
+
@collection = collection
|
56
|
+
@self_uri = Uri.parse serialized.dig(:_links, :self, :href)
|
57
|
+
end
|
58
|
+
|
59
|
+
def call
|
60
|
+
return unless should_paginate?
|
61
|
+
add_query_to_self
|
62
|
+
add_prev_link
|
63
|
+
add_next_link
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
attr_accessor :serialized, :collection, :self_uri
|
69
|
+
|
70
|
+
def should_paginate?
|
71
|
+
self_uri && current_page
|
72
|
+
end
|
73
|
+
|
74
|
+
def query(page)
|
75
|
+
return {} unless page
|
76
|
+
{
|
77
|
+
page: page,
|
78
|
+
per_page: per_page,
|
79
|
+
}
|
80
|
+
end
|
81
|
+
|
82
|
+
def add_query_to_self
|
83
|
+
serialized[:_links][:self][:href] = (self_uri + query(current_page)).to_s
|
84
|
+
end
|
85
|
+
|
86
|
+
def add_prev_link
|
87
|
+
return if serialized[:_links][:prev] || !prev_page
|
88
|
+
serialized[:_links][:prev] = {
|
89
|
+
href: (self_uri + query(prev_page)).to_s
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
def add_next_link
|
94
|
+
return if serialized[:_links][:next] || !next_page
|
95
|
+
serialized[:_links][:next] = {
|
96
|
+
href: (self_uri + query(next_page)).to_s
|
97
|
+
}
|
98
|
+
end
|
99
|
+
|
100
|
+
# Supported by Sequel, Kaminari and will_paginate
|
101
|
+
def current_page
|
102
|
+
collection.respond_to?(:current_page) && collection.current_page
|
103
|
+
end
|
104
|
+
|
105
|
+
def prev_page
|
106
|
+
return collection.prev_page if collection.respond_to?(:prev_page) # Sequel and Kaminari
|
107
|
+
return collection.previous_page if collection.respond_to?(:previous_page) # will_paginate
|
108
|
+
end
|
109
|
+
|
110
|
+
# Supported by Sequel, Kaminari and will_paginate
|
111
|
+
def next_page
|
112
|
+
collection.respond_to?(:next_page) && collection.next_page
|
113
|
+
end
|
114
|
+
|
115
|
+
def per_page
|
116
|
+
if collection.respond_to?(:page_size)
|
117
|
+
collection.page_size # Supported by Sequel
|
118
|
+
elsif collection.respond_to?(:max_per_page)
|
119
|
+
collection.max_per_page # Supported by Kaminari
|
120
|
+
elsif collection.respond_to?(:per_page)
|
121
|
+
collection.per_page # Supported by will_paginate
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'json'
|
2
|
+
require 'hal_decorator/pagination'
|
2
3
|
|
3
4
|
module HALDecorator
|
4
5
|
|
@@ -28,20 +29,14 @@ module HALDecorator
|
|
28
29
|
end
|
29
30
|
|
30
31
|
def to_collection(resources = [], options = {})
|
31
|
-
|
32
|
-
if parameters.nil?
|
32
|
+
unless can_serialize_collection?
|
33
33
|
raise Error,
|
34
34
|
"Trying to serialize a collection using #{self} which has no collection info. " \
|
35
35
|
"Add a 'collection' spec to the serializer or use another serializer"
|
36
36
|
end
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
serialized.merge! _serialize_links(links, curies, resources, nil, options)
|
41
|
-
|
42
|
-
serialized_resources = resources.map { |resource| to_hash(resource, options) }
|
43
|
-
serialized[:_embedded] = { parameters.name => serialized_resources }
|
44
|
-
JSON.generate(serialized)
|
37
|
+
options[:paginate] = HALDecorator.paginate unless options.key? :paginate
|
38
|
+
hash = to_collection_hash(resources, options)
|
39
|
+
JSON.generate(hash)
|
45
40
|
end
|
46
41
|
|
47
42
|
protected
|
@@ -58,6 +53,20 @@ module HALDecorator
|
|
58
53
|
end
|
59
54
|
end
|
60
55
|
|
56
|
+
def to_collection_hash(resources, options)
|
57
|
+
parameters = collection_parameters
|
58
|
+
links = parameters.links
|
59
|
+
curies = parameters.curies
|
60
|
+
{}.tap do |serialized|
|
61
|
+
serialized.merge! _serialize_attributes(parameters.attributes, resources, nil, options)
|
62
|
+
serialized.merge! _serialize_links(links, curies, resources, nil, options)
|
63
|
+
Pagination.paginate!(serialized, resources) if options[:paginate]
|
64
|
+
|
65
|
+
serialized_resources = resources.map { |resource| to_hash(resource, options) }
|
66
|
+
serialized[:_embedded] = { parameters.name => serialized_resources }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
61
70
|
def serialize_attributes(resource, policy, options)
|
62
71
|
_serialize_attributes(attributes, resource, policy, options)
|
63
72
|
end
|
@@ -121,10 +130,7 @@ module HALDecorator
|
|
121
130
|
decorator = embed.decorator_class
|
122
131
|
hash[embed.name] =
|
123
132
|
if resource.respond_to? :each
|
124
|
-
decorator
|
125
|
-
resource.map do |resrc|
|
126
|
-
decorator.to_hash(resrc, options)
|
127
|
-
end
|
133
|
+
_serialize_embedded_collection(resource, decorator, options)
|
128
134
|
else
|
129
135
|
decorator ||= HALDecorator.lookup_decorator(resource).first
|
130
136
|
decorator.to_hash(resource, options)
|
@@ -133,6 +139,23 @@ module HALDecorator
|
|
133
139
|
return {} if serialized.empty?
|
134
140
|
{ _embedded: serialized }
|
135
141
|
end
|
142
|
+
|
143
|
+
def _serialize_embedded_collection(resources, decorator, options)
|
144
|
+
clazz = resources.first.class
|
145
|
+
decorator ||= HALDecorator.lookup_decorator(clazz)&.first
|
146
|
+
if decorator.nil?
|
147
|
+
raise Serializer::Error,
|
148
|
+
"No decorator specified to handle serializing embedded #{clazz}"
|
149
|
+
end
|
150
|
+
if decorator.respond_to?(:can_serialize_collection?, true) &&
|
151
|
+
decorator.can_serialize_collection?
|
152
|
+
decorator.to_collection_hash(resources, options)
|
153
|
+
else
|
154
|
+
resources.map do |resrc|
|
155
|
+
decorator.to_hash(resrc, options)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
136
159
|
end
|
137
160
|
end
|
138
161
|
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hal_decorator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sammy Henningsson
|
@@ -31,7 +31,7 @@ cert_chain:
|
|
31
31
|
CNZdF8Vavp6xMQbPHZwqjaeZz2WRXYS7jyYSvCunjwa3OtvXtfbIEGEWE6IM+t9k
|
32
32
|
H1g6Q+B6qk9O6g==
|
33
33
|
-----END CERTIFICATE-----
|
34
|
-
date: 2017-
|
34
|
+
date: 2017-11-22 00:00:00.000000000 Z
|
35
35
|
dependencies:
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
@@ -59,7 +59,7 @@ dependencies:
|
|
59
59
|
requirements:
|
60
60
|
- - "~>"
|
61
61
|
- !ruby/object:Gem::Version
|
62
|
-
version: '5.
|
62
|
+
version: '5.0'
|
63
63
|
- - ">="
|
64
64
|
- !ruby/object:Gem::Version
|
65
65
|
version: '4.0'
|
@@ -69,7 +69,7 @@ dependencies:
|
|
69
69
|
requirements:
|
70
70
|
- - "~>"
|
71
71
|
- !ruby/object:Gem::Version
|
72
|
-
version: '5.
|
72
|
+
version: '5.0'
|
73
73
|
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '4.0'
|
@@ -113,6 +113,46 @@ dependencies:
|
|
113
113
|
- - ">="
|
114
114
|
- !ruby/object:Gem::Version
|
115
115
|
version: '9.0'
|
116
|
+
- !ruby/object:Gem::Dependency
|
117
|
+
name: kaminari
|
118
|
+
requirement: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - "~>"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '1.1'
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 1.1.1
|
126
|
+
type: :development
|
127
|
+
prerelease: false
|
128
|
+
version_requirements: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - "~>"
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '1.1'
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: 1.1.1
|
136
|
+
- !ruby/object:Gem::Dependency
|
137
|
+
name: will_paginate
|
138
|
+
requirement: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - "~>"
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '3.1'
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 3.1.6
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '3.1'
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: 3.1.6
|
116
156
|
description: |
|
117
157
|
A DSL for serializing resources according to
|
118
158
|
HypertextApplicationLanguage.
|
@@ -129,6 +169,7 @@ files:
|
|
129
169
|
- lib/hal_decorator/embedded.rb
|
130
170
|
- lib/hal_decorator/links.rb
|
131
171
|
- lib/hal_decorator/model.rb
|
172
|
+
- lib/hal_decorator/pagination.rb
|
132
173
|
- lib/hal_decorator/policy.rb
|
133
174
|
- lib/hal_decorator/policy/dsl.rb
|
134
175
|
- lib/hal_decorator/property.rb
|
metadata.gz.sig
CHANGED
Binary file
|