collection_json_serializer 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +137 -0
- data/Rakefile +10 -0
- data/collection_json_serializer.gemspec +26 -0
- data/lib/collection_json_serializer/builder.rb +56 -0
- data/lib/collection_json_serializer/core_ext/hash.rb +9 -0
- data/lib/collection_json_serializer/core_ext/symbol.rb +8 -0
- data/lib/collection_json_serializer/objects/item.rb +70 -0
- data/lib/collection_json_serializer/objects/template.rb +25 -0
- data/lib/collection_json_serializer/serializer.rb +71 -0
- data/lib/collection_json_serializer/support.rb +36 -0
- data/lib/collection_json_serializer/validator/url.rb +22 -0
- data/lib/collection_json_serializer/validator/value.rb +28 -0
- data/lib/collection_json_serializer/validator.rb +131 -0
- data/lib/collection_json_serializer/version.rb +5 -0
- data/lib/collection_json_serializer.rb +18 -0
- data/test/builder/builder_test.rb +157 -0
- data/test/fixtures/models.rb +3 -0
- data/test/fixtures/poro.rb +26 -0
- data/test/fixtures/serializers/custom_item_links_serializer.rb +7 -0
- data/test/fixtures/serializers/custom_item_serializer.rb +3 -0
- data/test/fixtures/serializers/custom_template_serializer.rb +5 -0
- data/test/fixtures/serializers/invalid_serializer.rb +4 -0
- data/test/fixtures/serializers/multiple_href_serializer.rb +3 -0
- data/test/fixtures/serializers/unknown_attribute_serializer.rb +3 -0
- data/test/fixtures/serializers/user_serializer.rb +7 -0
- data/test/fixtures/serializers/valid_serializer.rb +5 -0
- data/test/minitest_helper.rb +11 -0
- data/test/objects/item_test.rb +82 -0
- data/test/objects/template_test.rb +40 -0
- data/test/serializer/data_test.rb +29 -0
- data/test/serializer/href_test.rb +39 -0
- data/test/serializer/links_test.rb +23 -0
- data/test/serializer/template_test.rb +30 -0
- data/test/support/ext_test.rb +23 -0
- data/test/support/support_test.rb +29 -0
- data/test/validator/invalid_test.rb +138 -0
- data/test/validator/url_validator_test.rb +24 -0
- data/test/validator/validator_test.rb +64 -0
- data/test/validator/value_validator_test.rb +42 -0
- metadata +169 -0
@@ -0,0 +1,7 @@
|
|
1
|
+
class UserSerializer < CollectionJson::Serializer
|
2
|
+
href self: "http://example.com/users/{id}",
|
3
|
+
collection: "http://example.com/users"
|
4
|
+
attributes :name, :email
|
5
|
+
template :name, email: { prompt: "My email" }
|
6
|
+
links dashboard: { href: "http://example.com/my-dashboard" }
|
7
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
|
2
|
+
require "collection_json_serializer"
|
3
|
+
|
4
|
+
require "minitest/autorun"
|
5
|
+
|
6
|
+
require "fixtures/poro"
|
7
|
+
require "fixtures/models"
|
8
|
+
Dir.glob(File.dirname(__FILE__) + "/fixtures/serializers/**/*.rb") { |file| require file }
|
9
|
+
|
10
|
+
require "active_support/json"
|
11
|
+
require "active_support/inflector"
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require "minitest_helper"
|
2
|
+
|
3
|
+
module CollectionJson
|
4
|
+
class Serializer
|
5
|
+
class Objects
|
6
|
+
class Item
|
7
|
+
class TestItem < Minitest::Test
|
8
|
+
def setup
|
9
|
+
@user1 = User.new(name: "Carles Jove", email: "hola@carlus.cat")
|
10
|
+
@user2 = User.new(name: "Aina Jove", email: "hola@example.com")
|
11
|
+
@user_serializer = UserSerializer.new(@user1)
|
12
|
+
@item = Item.new(@user_serializer)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_that_an_item_can_be_build
|
16
|
+
expected = {
|
17
|
+
href: "http://example.com/users/#{@user1.id}",
|
18
|
+
data: [
|
19
|
+
{ name: "name", value: "Carles Jove" },
|
20
|
+
{ name: "email", value: "hola@carlus.cat" }
|
21
|
+
],
|
22
|
+
links: [
|
23
|
+
{ name: "dashboard", href: "http://example.com/my-dashboard" }
|
24
|
+
]
|
25
|
+
}
|
26
|
+
|
27
|
+
assert_equal expected.to_json, @item.create.to_json
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_that_an_item_can_be_built_passing_an_index
|
31
|
+
user_serializer = UserSerializer.new([@user1, @user2])
|
32
|
+
item = Item.new(user_serializer, item: 1)
|
33
|
+
|
34
|
+
expected = {
|
35
|
+
href: "http://example.com/users/#{@user2.id}",
|
36
|
+
data: [
|
37
|
+
{ name: "name", value: "Aina Jove" },
|
38
|
+
{ name: "email", value: "hola@example.com" }
|
39
|
+
],
|
40
|
+
links: [
|
41
|
+
{ name: "dashboard", href: "http://example.com/my-dashboard" }
|
42
|
+
]
|
43
|
+
}
|
44
|
+
|
45
|
+
assert_equal expected.to_json, item.create.to_json
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_that_an_item_can_be_build_with_random_attributes
|
49
|
+
custom_serializer = CustomItemSerializer.new(@user1)
|
50
|
+
item = Item.new(custom_serializer)
|
51
|
+
|
52
|
+
expected = {
|
53
|
+
data: [
|
54
|
+
{ name: "name", value: "Carles Jove", anything: "at all", whatever: "really" },
|
55
|
+
]
|
56
|
+
}
|
57
|
+
assert_equal expected.to_json, item.create.to_json
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_that_an_item_link_can_be_build_with_unlimited_attributes
|
61
|
+
custom_serializer = CustomItemLinksSerializer.new(@user1)
|
62
|
+
item = Item.new(custom_serializer)
|
63
|
+
|
64
|
+
expected = {
|
65
|
+
links: [
|
66
|
+
{ name: "dashboard", href: "/my-dashboard", anything: "at all", whatever: "really" }
|
67
|
+
]
|
68
|
+
}
|
69
|
+
|
70
|
+
assert_equal expected[:links], item.create[:links]
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_that_unknown_attributes_are_silently_ignored
|
74
|
+
serializer_with_unknown_attr = UnknownAttributeSerializer.new(@user1)
|
75
|
+
item = Item.new(serializer_with_unknown_attr)
|
76
|
+
refute item.create.include?(:unknown)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "minitest_helper"
|
2
|
+
|
3
|
+
module CollectionJson
|
4
|
+
class Serializer
|
5
|
+
class Objects
|
6
|
+
class Template
|
7
|
+
class TestTemplate < Minitest::Test
|
8
|
+
def setup
|
9
|
+
@user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
|
10
|
+
@account = Account.new(id: 1, name: "My Account", created_at: Time.now)
|
11
|
+
@user.account = @account
|
12
|
+
@user_serializer = UserSerializer.new(@user)
|
13
|
+
@template = Template.new(@user_serializer)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_that_a_template_can_be_build
|
17
|
+
expected = [
|
18
|
+
{ name: "name", value: "" },
|
19
|
+
{ name: "email", value: "", prompt: "My email" }
|
20
|
+
]
|
21
|
+
|
22
|
+
assert_equal expected.to_json, @template.create.to_json
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_that_a_template_can_be_build_with_random_attributes
|
26
|
+
custom_serializer = CustomTemplateSerializer.new(@user)
|
27
|
+
template = Template.new(custom_serializer)
|
28
|
+
|
29
|
+
expected = [
|
30
|
+
{ name: "name", value: "" },
|
31
|
+
{ name: "email", value: "", prompt: "My email", anything: "at all", whatever: "really" }
|
32
|
+
]
|
33
|
+
|
34
|
+
assert_equal expected.to_json, template.create.to_json
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "minitest_helper"
|
2
|
+
|
3
|
+
module CollectionJson
|
4
|
+
class Serializer
|
5
|
+
class TestData < 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_attributes_properties
|
12
|
+
assert_equal [:name, :email], @user_serializer.class.attributes
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_that_any_attributes_can_be_passed
|
16
|
+
custom_serializer = CustomItemSerializer.new(@user)
|
17
|
+
expected = [
|
18
|
+
name:
|
19
|
+
{
|
20
|
+
anything: "at all",
|
21
|
+
whatever: "really"
|
22
|
+
}
|
23
|
+
]
|
24
|
+
|
25
|
+
assert_equal expected, custom_serializer.class.attributes
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "minitest_helper"
|
2
|
+
|
3
|
+
module CollectionJson
|
4
|
+
class Serializer
|
5
|
+
class TestHref < 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_href_object
|
12
|
+
expected = [{
|
13
|
+
self: "http://example.com/users/{id}",
|
14
|
+
collection: "http://example.com/users"
|
15
|
+
}]
|
16
|
+
assert_equal expected, @user_serializer.class.href
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_that_only_one_href_value_is_passed_to_builder
|
20
|
+
multiple_href_serializer = MultipleHrefSerializer.new(@user)
|
21
|
+
assert_equal %w(/a /b /c), multiple_href_serializer.class.href
|
22
|
+
assert_equal "/a", multiple_href_serializer.href
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_that_a_placeholder_can_be_used_for_urls
|
26
|
+
user_serializer = CollectionJson::Serializer.new(@user)
|
27
|
+
user_serializer.class.attributes = [:name]
|
28
|
+
user_serializer.class.links = []
|
29
|
+
user_serializer.class.template = []
|
30
|
+
user_serializer.class.href = [self: "http://example.com/users/{id}"]
|
31
|
+
builder = Builder.new(user_serializer)
|
32
|
+
|
33
|
+
expected = "http://example.com/users/#{@user.id}"
|
34
|
+
actual = builder.pack[:collection][:items].first[:href]
|
35
|
+
assert_equal expected, actual
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,23 @@
|
|
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_item_links_attributes
|
12
|
+
expected = [dashboard: { href: "http://example.com/my-dashboard" }]
|
13
|
+
assert_equal expected, @user_serializer.class.links
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_item_links_attributes_can_take_unlimited_properties
|
17
|
+
custom_serializer = CustomItemLinksSerializer.new(@user)
|
18
|
+
expected = [dashboard: { href: "/my-dashboard", anything: "at all", whatever: "really" }]
|
19
|
+
assert_equal expected, custom_serializer.class.links
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require "minitest_helper"
|
2
|
+
|
3
|
+
module CollectionJson
|
4
|
+
class Serializer
|
5
|
+
class TestTemplate < 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_template_attributes
|
12
|
+
assert_equal [:name, email: { prompt: "My email" }], @user_serializer.class.template
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_that_any_attributes_can_be_passed
|
16
|
+
custom_serializer = CustomTemplateSerializer.new(@user)
|
17
|
+
expected = [
|
18
|
+
:name,
|
19
|
+
email: {
|
20
|
+
prompt: "My email",
|
21
|
+
anything: "at all",
|
22
|
+
whatever: "really"
|
23
|
+
}
|
24
|
+
]
|
25
|
+
|
26
|
+
assert_equal expected, custom_serializer.class.template
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "minitest_helper"
|
2
|
+
|
3
|
+
module CollectionJson
|
4
|
+
class Serializer
|
5
|
+
class TestExtractParams < Minitest::Test
|
6
|
+
def test_extract_params_for_hash
|
7
|
+
attrs = { test: { prompt: "works" } }
|
8
|
+
|
9
|
+
params = attrs.extract_params
|
10
|
+
assert_equal :test, params[:name]
|
11
|
+
assert_equal attrs[:test], params[:properties]
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_extract_params_for_symbol
|
15
|
+
attrs = :test
|
16
|
+
|
17
|
+
params = attrs.extract_params
|
18
|
+
assert_equal :test, params[:name]
|
19
|
+
assert params[:properties].nil?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "minitest_helper"
|
2
|
+
|
3
|
+
module CollectionJson
|
4
|
+
class Serializer
|
5
|
+
module Support
|
6
|
+
class TestSupport < Minitest::Test
|
7
|
+
include CollectionJson::Serializer::Support
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@user = User.new(name: "Carles Jove", email: "hola@carlus.cat")
|
11
|
+
@user_serializer = UserSerializer.new(@user)
|
12
|
+
@builder = Builder.new(@user_serializer)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_extract_value
|
16
|
+
actual = extract_value_from(@user_serializer.resources.first, :name)
|
17
|
+
assert_equal "Carles Jove", actual
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_that_an_url_with_placeholder_can_be_parsed
|
21
|
+
url = "http://example.com/users/{id}"
|
22
|
+
expected = "http://example.com/users/#{@user.id}"
|
23
|
+
|
24
|
+
assert_equal expected, parse_url(url, @user)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
require "minitest_helper"
|
2
|
+
|
3
|
+
module CollectionJson
|
4
|
+
class Serializer
|
5
|
+
class Validator
|
6
|
+
class TestInvalid < Minitest::Test
|
7
|
+
def setup
|
8
|
+
@user = User.new(
|
9
|
+
name: "Carles Jove",
|
10
|
+
email: "hola@carlus.cat"
|
11
|
+
)
|
12
|
+
@account = Account.new(
|
13
|
+
id: 1,
|
14
|
+
name: "My Account",
|
15
|
+
created_at: Time.now
|
16
|
+
)
|
17
|
+
@user.account = @account
|
18
|
+
|
19
|
+
@invalid_value_types = [
|
20
|
+
/regex/,
|
21
|
+
:symbol,
|
22
|
+
{},
|
23
|
+
[]
|
24
|
+
]
|
25
|
+
|
26
|
+
@invalid = CollectionJson::Serializer.new(@user)
|
27
|
+
@invalid.class.attributes = []
|
28
|
+
@invalid.class.href = []
|
29
|
+
@invalid.class.links = []
|
30
|
+
@invalid.class.template = []
|
31
|
+
end
|
32
|
+
|
33
|
+
# href
|
34
|
+
def test_that_href_generates_errors
|
35
|
+
@invalid.class.href = [self: "/users/1", collection: "www.users.com"]
|
36
|
+
|
37
|
+
assert @invalid.invalid?
|
38
|
+
assert @invalid.errors.include? :href
|
39
|
+
assert @invalid.errors[:href][0].
|
40
|
+
include? "href:self is an invalid URL"
|
41
|
+
assert @invalid.errors[:href][1].
|
42
|
+
include? "href:collection is an invalid URL"
|
43
|
+
|
44
|
+
@invalid.class.href = ["/users/1"]
|
45
|
+
|
46
|
+
assert @invalid.invalid?
|
47
|
+
assert @invalid.errors.include? :href
|
48
|
+
assert @invalid.errors[:href][0].
|
49
|
+
include? "href is an invalid URL"
|
50
|
+
end
|
51
|
+
|
52
|
+
# links
|
53
|
+
def test_that_links_generates_errors
|
54
|
+
@invalid.class.links = [dashboard: { href: "/my-dashboard" }]
|
55
|
+
assert @invalid.invalid?
|
56
|
+
assert @invalid.errors.include? :links
|
57
|
+
assert @invalid.errors[:links].first.
|
58
|
+
include? "links:dashboard:href is an invalid URL"
|
59
|
+
|
60
|
+
@invalid.class.links = [
|
61
|
+
dashboard: {
|
62
|
+
href: "http://valid.url.com",
|
63
|
+
prompt: /invalid/
|
64
|
+
}
|
65
|
+
]
|
66
|
+
assert @invalid.invalid?
|
67
|
+
assert @invalid.errors.include? :links
|
68
|
+
assert @invalid.errors[:links].first.
|
69
|
+
include? "links:dashboard:prompt is an invalid value"
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_that_links_missing_href_generates_error
|
73
|
+
@invalid.class.links = [dashboard: {}]
|
74
|
+
assert @invalid.invalid?
|
75
|
+
assert @invalid.errors.include? :links
|
76
|
+
assert @invalid.errors[:links].first.
|
77
|
+
include? "links:dashboard:href is missing"
|
78
|
+
end
|
79
|
+
|
80
|
+
# attributes
|
81
|
+
def test_that_invalid_attributes_return_values_generate_errors
|
82
|
+
@invalid.class.attributes = [:name]
|
83
|
+
|
84
|
+
@invalid_value_types.each do |invalidate|
|
85
|
+
@user.name = invalidate
|
86
|
+
assert @invalid.invalid?,
|
87
|
+
"#{invalidate} should be invalid"
|
88
|
+
assert @invalid.errors.include?(:attributes),
|
89
|
+
"#{invalidate} should be invalid"
|
90
|
+
assert @invalid.errors[:attributes].
|
91
|
+
first.include? "attributes:name is an invalid value"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_that_invalid_attributes_properties_values_generate_errors
|
96
|
+
@invalid_value_types.each do |invalidate|
|
97
|
+
@invalid.class.attributes = [
|
98
|
+
name: {
|
99
|
+
prompt: invalidate,
|
100
|
+
test: invalidate
|
101
|
+
}
|
102
|
+
]
|
103
|
+
|
104
|
+
assert @invalid.invalid?,
|
105
|
+
"#{invalidate} should be invalid"
|
106
|
+
assert @invalid.errors.include?(:attributes),
|
107
|
+
"#{invalidate} should be invalid"
|
108
|
+
assert @invalid.errors[:attributes][0].
|
109
|
+
include? "attributes:name:prompt is an invalid value"
|
110
|
+
assert @invalid.errors[:attributes][1].
|
111
|
+
include? "attributes:name:test is an invalid value"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# template
|
116
|
+
def test_that_template_values_validate
|
117
|
+
@invalid_value_types.each do |invalidate|
|
118
|
+
@invalid.class.template = [
|
119
|
+
name: {
|
120
|
+
prompt: invalidate,
|
121
|
+
name: invalidate
|
122
|
+
}
|
123
|
+
]
|
124
|
+
|
125
|
+
assert @invalid.invalid?,
|
126
|
+
"#{invalidate} should be invalid"
|
127
|
+
assert @invalid.errors.include?(:template),
|
128
|
+
"#{invalidate} should be invalid"
|
129
|
+
assert @invalid.errors[:template][0].
|
130
|
+
include? "template:name:prompt is an invalid value"
|
131
|
+
assert @invalid.errors[:template][1].
|
132
|
+
include? "template:name:name is an invalid value"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|