collection_json_serializer 0.2.0 → 0.3.0
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 +29 -19
- data/lib/collection_json_serializer.rb +1 -0
- data/lib/collection_json_serializer/builder.rb +24 -7
- data/lib/collection_json_serializer/items.rb +39 -0
- data/lib/collection_json_serializer/objects/item.rb +20 -18
- data/lib/collection_json_serializer/serializer.rb +26 -10
- data/lib/collection_json_serializer/support.rb +8 -0
- data/lib/collection_json_serializer/validator.rb +2 -2
- data/lib/collection_json_serializer/version.rb +1 -1
- data/test/builder/builder_test.rb +23 -9
- data/test/fixtures/serializers/custom_item_links_serializer.rb +6 -4
- data/test/fixtures/serializers/custom_item_serializer.rb +3 -1
- data/test/fixtures/serializers/user_serializer.rb +7 -3
- data/test/minitest_helper.rb +2 -2
- data/test/objects/item_test.rb +16 -12
- data/test/serializer/href_test.rb +5 -18
- data/test/serializer/{data_test.rb → items/attributes_test.rb} +3 -3
- data/test/serializer/items/href_test.rb +45 -0
- data/test/serializer/items/links_test.rb +20 -0
- data/test/serializer/items_test.rb +24 -0
- data/test/serializer/links_test.rb +11 -3
- data/test/validator/invalid_test.rb +2 -2
- metadata +11 -8
- data/test/fixtures/serializers/multiple_href_serializer.rb +0 -3
- data/test/fixtures/serializers/unknown_attribute_serializer.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a38b7d4dc38ed5c1bed4033818d2e69968be6720
|
4
|
+
data.tar.gz: b52f5ce2efdc98b2ed6c18a75d634c61592ffff4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56aa051a9b31a5b8da6e218c62a75dde366d1a92fdffe55ba55340fe480954a74c212aea2715f3379a308e549f60cec1ecfad4736cb7671b2132d6954adf9d7f
|
7
|
+
data.tar.gz: 4946c642a0ed91dca00879ef01bf5d73c998912747434be345d7bc5c1dc7137e1673f8b4700e3b5966713964b196dd7305e94f8897ac2e5ff6880c84d499b9cd
|
data/README.md
CHANGED
@@ -5,11 +5,9 @@
|
|
5
5
|
| :warning: This is _not finished_ yet, so use it at your own risk. |
|
6
6
|
--------------------------------------------------------------------
|
7
7
|
|
8
|
-
|
8
|
+
CollectionJson::Serializer serializes Ruby objects to Collection+JSON, the hypermedia type by Mike Amudsen.
|
9
9
|
|
10
|
-
CollectionJson::Serializer
|
11
|
-
|
12
|
-
Please note that CollectionJson::Serializer only serializes data. You still need to set the proper Headers or media-types in your app.
|
10
|
+
Please note that CollectionJson::Serializer _only serializes data_. You still need to set the proper Headers or media-types in your app.
|
13
11
|
|
14
12
|
## Installation
|
15
13
|
|
@@ -23,9 +21,6 @@ And then execute:
|
|
23
21
|
|
24
22
|
$ bundle
|
25
23
|
|
26
|
-
Or install it yourself as:
|
27
|
-
|
28
|
-
$ gem install collection_json_serializer
|
29
24
|
|
30
25
|
## Usage
|
31
26
|
|
@@ -33,10 +28,7 @@ As this gem user, you will be mainly writing/generating and mantaining serialize
|
|
33
28
|
|
34
29
|
```ruby
|
35
30
|
class UserSerializer < CollectionJson::Serializer
|
36
|
-
href
|
37
|
-
collection: "http://example.com/users"
|
38
|
-
|
39
|
-
attributes :id, name: { prompt: "Your full name" }, :email
|
31
|
+
href "http://example.com/users",
|
40
32
|
|
41
33
|
template :name, email: { prompt: "My email" }
|
42
34
|
|
@@ -54,6 +46,12 @@ class UserSerializer < CollectionJson::Serializer
|
|
54
46
|
{ name: "page" }
|
55
47
|
]
|
56
48
|
}
|
49
|
+
|
50
|
+
item do
|
51
|
+
attributes :id, name: { prompt: "Your full name" }, :email
|
52
|
+
href "http://example.com/users/{id}"
|
53
|
+
links avatar: { href: "http://assets.example.com/avatar.jpg", render: "image" }
|
54
|
+
end
|
57
55
|
end
|
58
56
|
```
|
59
57
|
|
@@ -84,6 +82,9 @@ This will generate this Collection+JSON response:
|
|
84
82
|
{
|
85
83
|
"version" : "1.0",
|
86
84
|
"href" : "http://example.com/users",
|
85
|
+
"links": [
|
86
|
+
{ "name": "dashboard", "href": "http://example.com/my-dashboard" }
|
87
|
+
],
|
87
88
|
"items" : [{
|
88
89
|
"href": "http://example.com/users/1",
|
89
90
|
"data": [
|
@@ -92,7 +93,8 @@ This will generate this Collection+JSON response:
|
|
92
93
|
{ "name": "email", "value": "email@example.com" },
|
93
94
|
],
|
94
95
|
"links": [
|
95
|
-
{ "name": "
|
96
|
+
{ "name": "avatar", "href": "http://assets.example.com/avatar.jpg",
|
97
|
+
"render": "image" }
|
96
98
|
]
|
97
99
|
}],
|
98
100
|
"template" : {
|
@@ -119,11 +121,13 @@ This will generate this Collection+JSON response:
|
|
119
121
|
|
120
122
|
#### URL placeholders
|
121
123
|
|
122
|
-
URLs can be generated dinamically with placeholder. A placeholder is a URL segment wrapped in curly braces. A placeholder can be any method that can be called on the object that the serializer takes (i.e. `id`, `username`, etc.).
|
124
|
+
Items' URLs can be generated dinamically with a placeholder. A placeholder is a URL segment wrapped in curly braces. A placeholder can be any method that can be called on the object that the serializer takes (i.e. `id`, `username`, etc.).
|
123
125
|
|
124
126
|
```ruby
|
125
127
|
class UserSerializer < CollectionJson::Serializer
|
126
|
-
|
128
|
+
items do
|
129
|
+
href "http://example.com/users/{id}"
|
130
|
+
end
|
127
131
|
end
|
128
132
|
```
|
129
133
|
|
@@ -131,21 +135,27 @@ All placeholders will be called, so you can use more than one if necessary, but
|
|
131
135
|
|
132
136
|
```ruby
|
133
137
|
class UserSerializer < CollectionJson::Serializer
|
134
|
-
|
135
|
-
|
138
|
+
items do
|
139
|
+
# This is ok
|
140
|
+
href "http://example.com/users/{id}/{username}"
|
136
141
|
|
137
|
-
|
138
|
-
|
142
|
+
# This is wrong
|
143
|
+
href "http://example.com/users/{id}-{username}"
|
144
|
+
end
|
139
145
|
end
|
140
146
|
```
|
141
147
|
|
148
|
+
Please, notice that placeholders can _only_ be used within the `items` block.
|
149
|
+
|
142
150
|
#### Open Attributes Policy
|
143
151
|
|
144
152
|
Collection+JSON serializer has an __open attributes policy__, which means that objects' attributes can be extended at will. That is good if you want to use many of the [extensions available](https://github.com/collection-json/extensions), and also if you need to add custom extensions to suit your particular needs. Be aware that, [as the specs say](https://github.com/collection-json/spec#7-extensibility), you must only extend attributes in a way that won't break clients that are not aware of them.
|
145
153
|
|
146
154
|
```ruby
|
147
155
|
class UserSerializer < CollectionJson::Serializer
|
148
|
-
|
156
|
+
items do
|
157
|
+
attributes :id, name: { css_class: "people" }
|
158
|
+
end
|
149
159
|
|
150
160
|
template name: { regex: "/\A[a-zA-Z0-9_]*\z/" }
|
151
161
|
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module CollectionJson
|
2
2
|
class Serializer
|
3
3
|
class Builder
|
4
|
+
include Support
|
5
|
+
|
4
6
|
def initialize(serializer)
|
5
7
|
@serializer = serializer
|
6
8
|
@collection = { version: "1.0" }
|
@@ -25,16 +27,15 @@ module CollectionJson
|
|
25
27
|
|
26
28
|
def build
|
27
29
|
# There might be a more elegant way to do it, yes
|
28
|
-
add_href
|
29
|
-
add_items
|
30
|
-
|
31
|
-
|
30
|
+
add_href if @serializer.href.present?
|
31
|
+
add_items if @serializer.items? && @serializer.items.attributes?
|
32
|
+
add_links if @serializer.links?
|
33
|
+
add_template if @serializer.template?
|
34
|
+
add_queries if @serializer.queries?
|
32
35
|
end
|
33
36
|
|
34
37
|
def add_href
|
35
|
-
if @serializer.href.
|
36
|
-
@collection.store :href, @serializer.href[:collection]
|
37
|
-
end
|
38
|
+
@collection.store(:href, @serializer.href) if @serializer.href.present?
|
38
39
|
end
|
39
40
|
|
40
41
|
def add_items
|
@@ -61,6 +62,22 @@ module CollectionJson
|
|
61
62
|
@collection[:queries] << query.create
|
62
63
|
end
|
63
64
|
end
|
65
|
+
|
66
|
+
def add_links
|
67
|
+
@collection.store :links, Array.new
|
68
|
+
|
69
|
+
@serializer.links.each do |attr|
|
70
|
+
params = attr.extract_params
|
71
|
+
|
72
|
+
next unless params.key? :properties
|
73
|
+
|
74
|
+
@collection[:links] << {
|
75
|
+
rel: set_rel(params),
|
76
|
+
href: params[:properties][:href],
|
77
|
+
name: params[:name].to_s
|
78
|
+
}.merge!(params[:properties])
|
79
|
+
end
|
80
|
+
end
|
64
81
|
end
|
65
82
|
end
|
66
83
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module CollectionJson
|
2
|
+
class Serializer
|
3
|
+
class Items
|
4
|
+
attr_accessor :href
|
5
|
+
attr_accessor :attributes
|
6
|
+
attr_accessor :links
|
7
|
+
|
8
|
+
def href(*args)
|
9
|
+
url = if args.first.is_a?(Array)
|
10
|
+
args.first.first
|
11
|
+
else
|
12
|
+
args.first
|
13
|
+
end
|
14
|
+
@href ||= url
|
15
|
+
end
|
16
|
+
|
17
|
+
def attributes(*args)
|
18
|
+
@attributes ||= args
|
19
|
+
end
|
20
|
+
|
21
|
+
def links(*args)
|
22
|
+
@links ||= args
|
23
|
+
end
|
24
|
+
|
25
|
+
def href?
|
26
|
+
@href.present?
|
27
|
+
end
|
28
|
+
|
29
|
+
def attributes?
|
30
|
+
@attributes.present?
|
31
|
+
end
|
32
|
+
|
33
|
+
def links?
|
34
|
+
@links.present?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
@@ -2,7 +2,7 @@ module CollectionJson
|
|
2
2
|
class Serializer
|
3
3
|
class Objects
|
4
4
|
class Item
|
5
|
-
include
|
5
|
+
include Support
|
6
6
|
|
7
7
|
def initialize(serializer, item: 0)
|
8
8
|
@serializer = serializer
|
@@ -12,9 +12,9 @@ module CollectionJson
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def create
|
15
|
-
add_href if @serializer.href
|
16
|
-
add_data if @serializer.attributes
|
17
|
-
add_links if @serializer.links
|
15
|
+
add_href if @serializer.items.href?
|
16
|
+
add_data if @serializer.items? && @serializer.items.attributes?
|
17
|
+
add_links if @serializer.items.links?
|
18
18
|
|
19
19
|
@item
|
20
20
|
end
|
@@ -22,13 +22,11 @@ module CollectionJson
|
|
22
22
|
private
|
23
23
|
|
24
24
|
def add_href
|
25
|
-
if @serializer.href
|
26
|
-
@item.store :href, set_href
|
27
|
-
end
|
25
|
+
@item.store(:href, set_href) if @serializer.items.href?
|
28
26
|
end
|
29
27
|
|
30
28
|
def add_data
|
31
|
-
|
29
|
+
attributes.each do |attr|
|
32
30
|
params = attr.extract_params
|
33
31
|
value = extract_value_from(@resource, params[:name])
|
34
32
|
|
@@ -39,11 +37,11 @@ module CollectionJson
|
|
39
37
|
|
40
38
|
start_object :data, Array.new
|
41
39
|
@item[:data] << c
|
42
|
-
end if @serializer.attributes
|
40
|
+
end if @serializer.items.attributes?
|
43
41
|
end
|
44
42
|
|
45
43
|
def add_links
|
46
|
-
@serializer.links.each do |attr|
|
44
|
+
@serializer.items.links.each do |attr|
|
47
45
|
params = attr.extract_params
|
48
46
|
|
49
47
|
next unless params.key? :properties
|
@@ -54,7 +52,7 @@ module CollectionJson
|
|
54
52
|
href: params[:properties][:href],
|
55
53
|
name: params[:name].to_s
|
56
54
|
}.merge!(params[:properties])
|
57
|
-
end if @serializer.links.present?
|
55
|
+
end if @serializer.items.links.present?
|
58
56
|
end
|
59
57
|
|
60
58
|
def start_object(name, type)
|
@@ -62,16 +60,20 @@ module CollectionJson
|
|
62
60
|
end
|
63
61
|
|
64
62
|
def set_href
|
65
|
-
url = @serializer.href
|
63
|
+
url = @serializer.items.href || @serializer.href
|
66
64
|
parse_url(url, @resource)
|
67
65
|
end
|
68
66
|
|
69
|
-
def
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
67
|
+
def href?
|
68
|
+
@serializer.href.present?
|
69
|
+
end
|
70
|
+
|
71
|
+
def items?
|
72
|
+
!@serializer.items.nil?
|
73
|
+
end
|
74
|
+
|
75
|
+
def attributes
|
76
|
+
@serializer.items.attributes if @serializer.items.attributes?
|
75
77
|
end
|
76
78
|
end
|
77
79
|
end
|
@@ -2,15 +2,14 @@ module CollectionJson
|
|
2
2
|
class Serializer
|
3
3
|
class << self
|
4
4
|
attr_accessor :href
|
5
|
-
attr_accessor :attributes
|
6
5
|
attr_accessor :template
|
7
6
|
attr_accessor :links
|
8
7
|
attr_accessor :queries
|
8
|
+
attr_accessor :_items
|
9
9
|
end
|
10
10
|
|
11
11
|
def self.inherited(base)
|
12
12
|
base.href = []
|
13
|
-
base.attributes = []
|
14
13
|
base.template = []
|
15
14
|
base.links = []
|
16
15
|
base.queries = []
|
@@ -20,10 +19,6 @@ module CollectionJson
|
|
20
19
|
@href.concat attrs
|
21
20
|
end
|
22
21
|
|
23
|
-
def self.attributes(*attrs)
|
24
|
-
@attributes.concat attrs
|
25
|
-
end
|
26
|
-
|
27
22
|
def self.template(*attrs)
|
28
23
|
@template.concat attrs
|
29
24
|
end
|
@@ -36,6 +31,11 @@ module CollectionJson
|
|
36
31
|
@queries.concat attrs
|
37
32
|
end
|
38
33
|
|
34
|
+
def self.items(&block)
|
35
|
+
@_items = Items.new
|
36
|
+
@_items.instance_eval(&block)
|
37
|
+
end
|
38
|
+
|
39
39
|
attr_accessor :resources
|
40
40
|
|
41
41
|
def initialize(resource)
|
@@ -50,22 +50,38 @@ module CollectionJson
|
|
50
50
|
self.class.href.first
|
51
51
|
end
|
52
52
|
|
53
|
-
def attributes
|
54
|
-
self.class.attributes
|
55
|
-
end
|
56
|
-
|
57
53
|
def template
|
58
54
|
self.class.template
|
59
55
|
end
|
60
56
|
|
57
|
+
def template?
|
58
|
+
self.class.template.present?
|
59
|
+
end
|
60
|
+
|
61
61
|
def links
|
62
62
|
self.class.links
|
63
63
|
end
|
64
64
|
|
65
|
+
def links?
|
66
|
+
self.class.links.present?
|
67
|
+
end
|
68
|
+
|
65
69
|
def queries
|
66
70
|
self.class.queries
|
67
71
|
end
|
68
72
|
|
73
|
+
def queries?
|
74
|
+
self.class.queries.present?
|
75
|
+
end
|
76
|
+
|
77
|
+
def items
|
78
|
+
self.class._items
|
79
|
+
end
|
80
|
+
|
81
|
+
def items?
|
82
|
+
self.class._items.present?
|
83
|
+
end
|
84
|
+
|
69
85
|
def invalid?
|
70
86
|
Validator.new(self).invalid?
|
71
87
|
end
|
@@ -31,6 +31,14 @@ module CollectionJson
|
|
31
31
|
def has_placeholder?(string)
|
32
32
|
string.chars.first.eql?("{") && string.chars.last.eql?("}")
|
33
33
|
end
|
34
|
+
|
35
|
+
def set_rel(params)
|
36
|
+
if params[:properties].key?(:rel)
|
37
|
+
params[:properties][:rel].to_s
|
38
|
+
else
|
39
|
+
params[:name].to_s
|
40
|
+
end
|
41
|
+
end
|
34
42
|
end
|
35
43
|
end
|
36
44
|
end
|
@@ -32,7 +32,7 @@ module CollectionJson
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def validate_attributes
|
35
|
-
@serializer.attributes.each do |attr|
|
35
|
+
@serializer.items.attributes.each do |attr|
|
36
36
|
params = attr.extract_params
|
37
37
|
|
38
38
|
@serializer.resources.each do |resource|
|
@@ -48,7 +48,7 @@ module CollectionJson
|
|
48
48
|
end
|
49
49
|
end if params[:properties]
|
50
50
|
|
51
|
-
end if @serializer.attributes.any?
|
51
|
+
end if @serializer.items && @serializer.items.attributes.any?
|
52
52
|
end
|
53
53
|
|
54
54
|
def validate_href
|
@@ -33,13 +33,20 @@ module CollectionJson
|
|
33
33
|
],
|
34
34
|
links: [
|
35
35
|
{
|
36
|
-
rel: "
|
37
|
-
href: "http://example.com/
|
38
|
-
name: "
|
36
|
+
rel: "avatar",
|
37
|
+
href: "http://assets.example.com/avatar.jpg",
|
38
|
+
name: "avatar"
|
39
39
|
}
|
40
40
|
]
|
41
41
|
}
|
42
42
|
],
|
43
|
+
links: [
|
44
|
+
{
|
45
|
+
rel: "dashboard",
|
46
|
+
href: "http://example.com/my-dashboard",
|
47
|
+
name: "dashboard"
|
48
|
+
}
|
49
|
+
],
|
43
50
|
template: {
|
44
51
|
data: [
|
45
52
|
{
|
@@ -87,9 +94,9 @@ module CollectionJson
|
|
87
94
|
],
|
88
95
|
links: [
|
89
96
|
{
|
90
|
-
rel: "
|
91
|
-
href: "http://example.com/
|
92
|
-
name: "
|
97
|
+
rel: "avatar",
|
98
|
+
href: "http://assets.example.com/avatar.jpg",
|
99
|
+
name: "avatar"
|
93
100
|
}
|
94
101
|
]
|
95
102
|
},
|
@@ -107,13 +114,20 @@ module CollectionJson
|
|
107
114
|
],
|
108
115
|
links: [
|
109
116
|
{
|
110
|
-
rel: "
|
111
|
-
href: "http://example.com/
|
112
|
-
name: "
|
117
|
+
rel: "avatar",
|
118
|
+
href: "http://assets.example.com/avatar.jpg",
|
119
|
+
name: "avatar"
|
113
120
|
}
|
114
121
|
]
|
115
122
|
}
|
116
123
|
],
|
124
|
+
links: [
|
125
|
+
{
|
126
|
+
rel: "dashboard",
|
127
|
+
href: "http://example.com/my-dashboard",
|
128
|
+
name: "dashboard"
|
129
|
+
}
|
130
|
+
],
|
117
131
|
template: {
|
118
132
|
data: [
|
119
133
|
{
|
@@ -1,7 +1,9 @@
|
|
1
1
|
class CustomItemLinksSerializer < CollectionJson::Serializer
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
2
|
+
items do
|
3
|
+
links dashboard: {
|
4
|
+
href: "/my-dashboard",
|
5
|
+
anything: "at all",
|
6
|
+
whatever: "really"
|
6
7
|
}
|
8
|
+
end
|
7
9
|
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
class UserSerializer < CollectionJson::Serializer
|
2
|
-
href
|
3
|
-
collection: "http://example.com/users"
|
4
|
-
attributes :name, :email
|
2
|
+
href "http://example.com/users"
|
5
3
|
template :name, email: { prompt: "My email" }
|
6
4
|
links dashboard: { href: "http://example.com/my-dashboard" }
|
7
5
|
queries search: {
|
@@ -15,4 +13,10 @@ class UserSerializer < CollectionJson::Serializer
|
|
15
13
|
{ name: "page" }
|
16
14
|
]
|
17
15
|
}
|
16
|
+
|
17
|
+
items do
|
18
|
+
href "http://example.com/users/{id}"
|
19
|
+
attributes :name, :email
|
20
|
+
links avatar: { href: "http://assets.example.com/avatar.jpg" }
|
21
|
+
end
|
18
22
|
end
|
data/test/minitest_helper.rb
CHANGED
@@ -13,12 +13,12 @@ require "active_support/inflector"
|
|
13
13
|
module TestHelper
|
14
14
|
def empty_serializer_for(object)
|
15
15
|
serializer = CollectionJson::Serializer.new(object)
|
16
|
-
serializer.class.attributes = []
|
17
16
|
serializer.class.href = []
|
18
17
|
serializer.class.links = []
|
19
18
|
serializer.class.template = []
|
20
19
|
serializer.class.queries = []
|
21
|
-
|
20
|
+
serializer.class.items {}
|
21
|
+
serializer.items.attributes = []
|
22
22
|
serializer
|
23
23
|
end
|
24
24
|
end
|
data/test/objects/item_test.rb
CHANGED
@@ -14,15 +14,17 @@ module CollectionJson
|
|
14
14
|
@item = Item.new(@user_serializer)
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
17
|
+
def test_that_rel_will_be_added_from_the_name_when_missing
|
18
18
|
serializer = empty_serializer_for(@user1)
|
19
|
-
serializer.
|
20
|
-
serializer.
|
19
|
+
serializer.items.attributes = [:name]
|
20
|
+
serializer.items.links = [avatar: {
|
21
|
+
href: "http://assets.example.com/avatar.jpg"
|
22
|
+
}]
|
21
23
|
item = Item.new(serializer)
|
22
24
|
actual = item.create[:links].first
|
23
25
|
|
24
26
|
assert actual.include? :rel
|
25
|
-
assert_equal "
|
27
|
+
assert_equal "avatar", actual[:rel]
|
26
28
|
end
|
27
29
|
|
28
30
|
def test_that_an_item_can_be_build
|
@@ -34,9 +36,9 @@ module CollectionJson
|
|
34
36
|
],
|
35
37
|
links: [
|
36
38
|
{
|
37
|
-
rel: "
|
38
|
-
href: "http://example.com/
|
39
|
-
name: "
|
39
|
+
rel: "avatar",
|
40
|
+
href: "http://assets.example.com/avatar.jpg",
|
41
|
+
name: "avatar"
|
40
42
|
}
|
41
43
|
]
|
42
44
|
}
|
@@ -56,9 +58,9 @@ module CollectionJson
|
|
56
58
|
],
|
57
59
|
links: [
|
58
60
|
{
|
59
|
-
rel: "
|
60
|
-
href: "http://example.com/
|
61
|
-
name: "
|
61
|
+
rel: "avatar",
|
62
|
+
href: "http://assets.example.com/avatar.jpg",
|
63
|
+
name: "avatar"
|
62
64
|
}
|
63
65
|
]
|
64
66
|
}
|
@@ -99,8 +101,10 @@ module CollectionJson
|
|
99
101
|
end
|
100
102
|
|
101
103
|
def test_that_unknown_attributes_are_silently_ignored
|
102
|
-
|
103
|
-
|
104
|
+
serializer = empty_serializer_for(@user1)
|
105
|
+
serializer.items.attributes(:unknown)
|
106
|
+
item = Item.new(serializer)
|
107
|
+
|
104
108
|
refute item.create.include?(:unknown)
|
105
109
|
end
|
106
110
|
end
|
@@ -11,28 +11,15 @@ module CollectionJson
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_href_object
|
14
|
-
|
15
|
-
self: "http://example.com/users/{id}",
|
16
|
-
collection: "http://example.com/users"
|
17
|
-
}]
|
18
|
-
assert_equal expected, @user_serializer.class.href
|
14
|
+
assert_equal ["http://example.com/users"], @user_serializer.class.href
|
19
15
|
end
|
20
16
|
|
21
17
|
def test_that_only_one_href_value_is_passed_to_builder
|
22
|
-
|
23
|
-
|
24
|
-
assert_equal "/a", multiple_href_serializer.href
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_that_a_placeholder_can_be_used_for_urls
|
28
|
-
user_serializer = empty_serializer_for(@user)
|
29
|
-
user_serializer.class.attributes = [:name]
|
30
|
-
user_serializer.class.href = [self: "http://example.com/users/{id}"]
|
31
|
-
builder = Builder.new(user_serializer)
|
18
|
+
serializer = empty_serializer_for(@user)
|
19
|
+
serializer.class.href = %w(/a /b /c)
|
32
20
|
|
33
|
-
|
34
|
-
|
35
|
-
assert_equal expected, actual
|
21
|
+
assert_equal %w(/a /b /c), serializer.class.href
|
22
|
+
assert_equal "/a", serializer.href
|
36
23
|
end
|
37
24
|
end
|
38
25
|
end
|
@@ -2,14 +2,14 @@ require "minitest_helper"
|
|
2
2
|
|
3
3
|
module CollectionJson
|
4
4
|
class Serializer
|
5
|
-
class
|
5
|
+
class TestAttributes < Minitest::Test
|
6
6
|
def setup
|
7
7
|
@user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
|
8
8
|
@user_serializer = UserSerializer.new(@user)
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_attributes_properties
|
12
|
-
assert_equal [:name, :email], @user_serializer.
|
12
|
+
assert_equal [:name, :email], @user_serializer.items.attributes
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_that_any_attributes_can_be_passed
|
@@ -22,7 +22,7 @@ module CollectionJson
|
|
22
22
|
}
|
23
23
|
]
|
24
24
|
|
25
|
-
assert_equal expected, custom_serializer.
|
25
|
+
assert_equal expected, custom_serializer.items.attributes
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "minitest_helper"
|
2
|
+
|
3
|
+
module CollectionJson
|
4
|
+
class Serializer
|
5
|
+
class TestItemsHref < Minitest::Test
|
6
|
+
include TestHelper
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
|
10
|
+
@user_serializer = UserSerializer.new(@user)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_href_object
|
14
|
+
expected = "http://example.com/users/{id}"
|
15
|
+
assert_equal expected, @user_serializer.items.href
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_that_only_one_href_is_passed_if_multiple_strings
|
19
|
+
multiple_href_serializer = empty_serializer_for(@user)
|
20
|
+
multiple_href_serializer.items.href("/a", "/b", "/c")
|
21
|
+
|
22
|
+
assert_equal "/a", multiple_href_serializer.items.href
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_that_only_one_href_is_passed_if_an_array
|
26
|
+
multiple_href_serializer = empty_serializer_for(@user)
|
27
|
+
multiple_href_serializer.items.href(%w(/a /b /c))
|
28
|
+
|
29
|
+
assert_equal "/a", multiple_href_serializer.items.href
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_that_a_placeholder_can_be_used_for_urls
|
33
|
+
user_serializer = empty_serializer_for(@user)
|
34
|
+
user_serializer.items.attributes = [:name]
|
35
|
+
user_serializer.items.href("http://example.com/users/{id}")
|
36
|
+
builder = Builder.new(user_serializer)
|
37
|
+
|
38
|
+
expected = "http://example.com/users/#{@user.id}"
|
39
|
+
actual = builder.pack[:collection][:items].first[:href]
|
40
|
+
|
41
|
+
assert_equal expected, actual
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require "minitest_helper"
|
2
|
+
|
3
|
+
module CollectionJson
|
4
|
+
class Serializer
|
5
|
+
class TestLinks < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
|
8
|
+
@user_serializer = UserSerializer.new(@user)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_links_properties
|
12
|
+
expected = { avatar: {
|
13
|
+
href: "http://assets.example.com/avatar.jpg"
|
14
|
+
} }
|
15
|
+
|
16
|
+
assert_equal [expected], @user_serializer.items.links
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "minitest_helper"
|
2
|
+
|
3
|
+
module CollectionJson
|
4
|
+
class Serializer
|
5
|
+
class TestItems < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
|
8
|
+
@user_serializer = UserSerializer.new(@user)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_an_items_instance_is_created
|
12
|
+
assert_equal Items, @user_serializer.items.class
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_that_items_has_an_attributes_dsl
|
16
|
+
assert @user_serializer.items.respond_to?(:attributes)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_that_items_has_a_links_dsl
|
20
|
+
assert @user_serializer.items.respond_to?(:links)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -3,6 +3,8 @@ require "minitest_helper"
|
|
3
3
|
module CollectionJson
|
4
4
|
class Serializer
|
5
5
|
class TestLinks < Minitest::Test
|
6
|
+
include TestHelper
|
7
|
+
|
6
8
|
def setup
|
7
9
|
@user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
|
8
10
|
@user_serializer = UserSerializer.new(@user)
|
@@ -14,9 +16,15 @@ module CollectionJson
|
|
14
16
|
end
|
15
17
|
|
16
18
|
def test_item_links_attributes_can_take_unlimited_properties
|
17
|
-
custom_serializer =
|
18
|
-
|
19
|
-
|
19
|
+
custom_serializer = empty_serializer_for(@user)
|
20
|
+
links = { dashboard: {
|
21
|
+
href: "/my-dashboard",
|
22
|
+
anything: "at all",
|
23
|
+
whatever: "really"
|
24
|
+
} }
|
25
|
+
custom_serializer.class.links links
|
26
|
+
|
27
|
+
assert_equal [links], custom_serializer.class.links
|
20
28
|
end
|
21
29
|
end
|
22
30
|
end
|
@@ -77,7 +77,7 @@ module CollectionJson
|
|
77
77
|
|
78
78
|
# Attributes
|
79
79
|
def test_that_invalid_attributes_return_values_generate_errors
|
80
|
-
@invalid.
|
80
|
+
@invalid.items.attributes = [:name]
|
81
81
|
|
82
82
|
@invalid_value_types.each do |invalidate|
|
83
83
|
@user.name = invalidate
|
@@ -92,7 +92,7 @@ module CollectionJson
|
|
92
92
|
|
93
93
|
def test_that_invalid_attributes_properties_values_generate_errors
|
94
94
|
@invalid_value_types.each do |invalidate|
|
95
|
-
@invalid.
|
95
|
+
@invalid.items.attributes = [
|
96
96
|
name: {
|
97
97
|
prompt: invalidate,
|
98
98
|
test: invalidate
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: collection_json_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carles Jove i Buxeda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -85,6 +85,7 @@ files:
|
|
85
85
|
- lib/collection_json_serializer/builder.rb
|
86
86
|
- lib/collection_json_serializer/core_ext/hash.rb
|
87
87
|
- lib/collection_json_serializer/core_ext/symbol.rb
|
88
|
+
- lib/collection_json_serializer/items.rb
|
88
89
|
- lib/collection_json_serializer/objects/item.rb
|
89
90
|
- lib/collection_json_serializer/objects/query.rb
|
90
91
|
- lib/collection_json_serializer/objects/template.rb
|
@@ -101,16 +102,17 @@ files:
|
|
101
102
|
- test/fixtures/serializers/custom_item_serializer.rb
|
102
103
|
- test/fixtures/serializers/custom_template_serializer.rb
|
103
104
|
- test/fixtures/serializers/invalid_serializer.rb
|
104
|
-
- test/fixtures/serializers/multiple_href_serializer.rb
|
105
|
-
- test/fixtures/serializers/unknown_attribute_serializer.rb
|
106
105
|
- test/fixtures/serializers/user_serializer.rb
|
107
106
|
- test/fixtures/serializers/valid_serializer.rb
|
108
107
|
- test/minitest_helper.rb
|
109
108
|
- test/objects/item_test.rb
|
110
109
|
- test/objects/queries_test.rb
|
111
110
|
- test/objects/template_test.rb
|
112
|
-
- test/serializer/data_test.rb
|
113
111
|
- test/serializer/href_test.rb
|
112
|
+
- test/serializer/items/attributes_test.rb
|
113
|
+
- test/serializer/items/href_test.rb
|
114
|
+
- test/serializer/items/links_test.rb
|
115
|
+
- test/serializer/items_test.rb
|
114
116
|
- test/serializer/links_test.rb
|
115
117
|
- test/serializer/queries_test.rb
|
116
118
|
- test/serializer/template_test.rb
|
@@ -152,16 +154,17 @@ test_files:
|
|
152
154
|
- test/fixtures/serializers/custom_item_serializer.rb
|
153
155
|
- test/fixtures/serializers/custom_template_serializer.rb
|
154
156
|
- test/fixtures/serializers/invalid_serializer.rb
|
155
|
-
- test/fixtures/serializers/multiple_href_serializer.rb
|
156
|
-
- test/fixtures/serializers/unknown_attribute_serializer.rb
|
157
157
|
- test/fixtures/serializers/user_serializer.rb
|
158
158
|
- test/fixtures/serializers/valid_serializer.rb
|
159
159
|
- test/minitest_helper.rb
|
160
160
|
- test/objects/item_test.rb
|
161
161
|
- test/objects/queries_test.rb
|
162
162
|
- test/objects/template_test.rb
|
163
|
-
- test/serializer/data_test.rb
|
164
163
|
- test/serializer/href_test.rb
|
164
|
+
- test/serializer/items/attributes_test.rb
|
165
|
+
- test/serializer/items/href_test.rb
|
166
|
+
- test/serializer/items/links_test.rb
|
167
|
+
- test/serializer/items_test.rb
|
165
168
|
- test/serializer/links_test.rb
|
166
169
|
- test/serializer/queries_test.rb
|
167
170
|
- test/serializer/template_test.rb
|