conglomerate 0.1.0 → 0.1.1
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/Gemfile +2 -0
- data/README.md +89 -1
- data/lib/conglomerate/serializer.rb +13 -1
- data/lib/conglomerate/version.rb +1 -1
- data/spec/conglomerate_spec.rb +7 -2
- data/spec/spec_helper.rb +3 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e38daf11ae81750e9556260e06001b9c8510a7d5
|
4
|
+
data.tar.gz: 0c7edf5b484cbd0d4c3d3953fef6ab1cbffda34f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97da55cdc7a6e5128d2317779d3e44a4339a7fc11e7d09c1e2d34cf56449778062adec8dc8ed5a1cfa838e78803ce3669626ab73efb705a3f55b74f5770c669c
|
7
|
+
data.tar.gz: 3a7d31e8647b1d367b8cebc6039dd1547d1fe03dad3fca2fd32f04e8521b10947eefc1db46310fa8a8b6d60f4ae62804418faba9755c4db284fd15a63b2fe879
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Conglomerate
|
2
2
|
|
3
3
|
[](http://badge.fury.io/rb/conglomerate)
|
4
|
+
[](https://semaphoreapp.com/minter/conglomerate)
|
4
6
|
[](https://codeclimate.com/github/teamsnap/conglomerate)
|
5
7
|
[](https://coveralls.io/r/teamsnap/conglomerate?branch=master)
|
6
8
|
[](https://gemnasium.com/teamsnap/conglomerate)
|
@@ -10,6 +12,11 @@ A library to serialize Ruby objects into collection+json.
|
|
10
12
|
|
11
13
|

|
12
14
|
|
15
|
+
This library focuses just on converting Ruby objects into Collection+JSON. It
|
16
|
+
aims to have the simplest format possible when constructing serializers
|
17
|
+
specific to Collection+JSON. It also tries to provide all common
|
18
|
+
Collection+JSON extensions to make it easy to create your API.
|
19
|
+
|
13
20
|
## Installation
|
14
21
|
|
15
22
|
Add this line to your application's Gemfile:
|
@@ -27,9 +34,90 @@ Or install it yourself as:
|
|
27
34
|
## Usage
|
28
35
|
|
29
36
|
```ruby
|
30
|
-
#
|
37
|
+
# Step 1: Create a serializer
|
38
|
+
class TeamSerializer
|
39
|
+
include Conglomerate.serializer
|
40
|
+
|
41
|
+
href { teams_url }
|
42
|
+
item_href { |item| team_url(item.id) }
|
43
|
+
|
44
|
+
attribute :id
|
45
|
+
attribute :name, :template => true
|
46
|
+
attribute :event_ids, :rel => :events { |item| event_url(item.event_ids.join(",")) }
|
47
|
+
|
48
|
+
link :root { root_url }
|
49
|
+
|
50
|
+
query :search, :data => :id { search_teams_url }
|
51
|
+
end
|
52
|
+
|
53
|
+
# Step 2: Serialize any object
|
54
|
+
|
55
|
+
class TeamsController < ApplicationController
|
56
|
+
def index
|
57
|
+
teams = [
|
58
|
+
OpenStruct.new(:id => 1, :name => "Team 01", :event_ids => [1,2,3]),
|
59
|
+
OpenStruct.new(:id => 2, :name => "Team 02", :event_ids => [4,5,6]),
|
60
|
+
]
|
61
|
+
render :json => TeamSerializer.new(teams, :context => self).serialize
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# Note, context is optional. It allows you to call helper methods such as url helpers easily inside the serializer.
|
66
|
+
```
|
67
|
+
|
68
|
+
```json
|
69
|
+
{
|
70
|
+
"collection": {
|
71
|
+
"href": "http://example.com/teams",
|
72
|
+
"items": [
|
73
|
+
{
|
74
|
+
"href": "http://example.com/teams/1",
|
75
|
+
"data": [
|
76
|
+
{"name": "id", "value": 1},
|
77
|
+
{"name": "name", "value": "Team 01"},
|
78
|
+
{"name": "event_ids", "value": [1,2,3]}
|
79
|
+
],
|
80
|
+
"links": [
|
81
|
+
{"rel": "events", "href": "http://example.com/events/1,2,3"}
|
82
|
+
]
|
83
|
+
},
|
84
|
+
{
|
85
|
+
"href": "http://example.com/teams/2",
|
86
|
+
"data": [
|
87
|
+
{"name": "id", "value": 2},
|
88
|
+
{"name": "name", "value": "Team 2"},
|
89
|
+
{"name": "event_ids", "value": [4,5,6]}
|
90
|
+
],
|
91
|
+
"links": [
|
92
|
+
{"rel": "events", "href": "http://example.com/events/4,5,6"}
|
93
|
+
]
|
94
|
+
}
|
95
|
+
],
|
96
|
+
"links": [
|
97
|
+
{"rel": "root", "href": "http://example.com"}
|
98
|
+
],
|
99
|
+
"queries": [
|
100
|
+
{
|
101
|
+
"rel": "search",
|
102
|
+
"href": "http://example.com/teams/search",
|
103
|
+
"data": [
|
104
|
+
{"name": "id", "value": ""}
|
105
|
+
]
|
106
|
+
}
|
107
|
+
],
|
108
|
+
"template": {
|
109
|
+
"data": [
|
110
|
+
{"name": "name", "value": ""}
|
111
|
+
]
|
112
|
+
}
|
113
|
+
}
|
114
|
+
}
|
31
115
|
```
|
32
116
|
|
117
|
+
## Roadmap
|
118
|
+
|
119
|
+
- Use more defaults to simplify the creation of serializers.
|
120
|
+
|
33
121
|
## Contributing
|
34
122
|
|
35
123
|
1. Fork it ( http://github.com/teamsnap/conglomerate/fork )
|
@@ -106,7 +106,7 @@ module Conglomerate
|
|
106
106
|
def apply_links(collection, links: self.class._links, object: nil)
|
107
107
|
if object && !links.empty?
|
108
108
|
links = links.map do |link|
|
109
|
-
if object.send(link[:name])
|
109
|
+
if present?(object.send(link[:name]))
|
110
110
|
build_item_link(
|
111
111
|
link[:rel], :proc => link[:block], :object => object
|
112
112
|
)
|
@@ -147,6 +147,18 @@ module Conglomerate
|
|
147
147
|
apply_href(link, :proc => proc, :object => object)
|
148
148
|
end
|
149
149
|
|
150
|
+
def blank?(value)
|
151
|
+
if value.is_a?(String)
|
152
|
+
value !~ /[^[:space:]]/
|
153
|
+
else
|
154
|
+
value.respond_to?(:empty?) ? value.empty? : !value
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def present?(value)
|
159
|
+
!blank?(value)
|
160
|
+
end
|
161
|
+
|
150
162
|
module ClassMethods
|
151
163
|
def href(&block)
|
152
164
|
self._href = block
|
data/lib/conglomerate/version.rb
CHANGED
data/spec/conglomerate_spec.rb
CHANGED
@@ -18,6 +18,9 @@ class ConglomerateTestSerializer
|
|
18
18
|
attribute :team_ids, :rel => :teams do |item|
|
19
19
|
team_url(item.team_ids.join(","))
|
20
20
|
end
|
21
|
+
attribute :user_ids, :rel => :users do |item|
|
22
|
+
user_url(item.user_ids.join(","))
|
23
|
+
end
|
21
24
|
|
22
25
|
link :events do
|
23
26
|
events_url
|
@@ -46,7 +49,8 @@ describe Conglomerate do
|
|
46
49
|
:description => "Tasty Burgers",
|
47
50
|
:event_id => 2,
|
48
51
|
:roster_id => nil,
|
49
|
-
:team_ids => [1,2]
|
52
|
+
:team_ids => [1,2],
|
53
|
+
:user_ids => []
|
50
54
|
)
|
51
55
|
end
|
52
56
|
|
@@ -152,7 +156,8 @@ describe Conglomerate do
|
|
152
156
|
{"name" => "id", "value" => 1},
|
153
157
|
{"name" => "event_id", "value" => 2},
|
154
158
|
{"name" => "roster_id", "value" => nil},
|
155
|
-
{"name" => "team_ids", "value" => [1,2]}
|
159
|
+
{"name" => "team_ids", "value" => [1,2]},
|
160
|
+
{"name" => "user_ids", "value" => []}
|
156
161
|
],
|
157
162
|
"links" => [
|
158
163
|
{"rel" => "event", "href" => "https://example.com/events/2"},
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: conglomerate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shane Emmons
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|