conglomerate 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9008dac9ea7c50dfb488faa1eb897070f62499f4
4
- data.tar.gz: f4260d896b2b80ca6e5581fb79eb08d212914f27
3
+ metadata.gz: e38daf11ae81750e9556260e06001b9c8510a7d5
4
+ data.tar.gz: 0c7edf5b484cbd0d4c3d3953fef6ab1cbffda34f
5
5
  SHA512:
6
- metadata.gz: e8a62475f4429d00aa29ab15dcc08affc5bf18444f57e50e2efa37c892111de6b173008f4504bf3ddd76931e3999a7450cbdce972b7f9bba2185dbacf5c24c82
7
- data.tar.gz: e4dc81daac27fed5f17bdc1b3796a1f638c164bffe02a30f9cc13bdb66bb4e785c22aa8dc5e797793d4d4fff48d4f7004855a51cd656f0ff640cfa71ce63db25
6
+ metadata.gz: 97da55cdc7a6e5128d2317779d3e44a4339a7fc11e7d09c1e2d34cf56449778062adec8dc8ed5a1cfa838e78803ce3669626ab73efb705a3f55b74f5770c669c
7
+ data.tar.gz: 3a7d31e8647b1d367b8cebc6039dd1547d1fe03dad3fca2fd32f04e8521b10947eefc1db46310fa8a8b6d60f4ae62804418faba9755c4db284fd15a63b2fe879
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
1
  source "https://rubygems.org"
2
2
 
3
+ gem "coveralls", :require => false
4
+
3
5
  gemspec
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # Conglomerate
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/conglomerate.png)](http://badge.fury.io/rb/conglomerate)
4
+ [![Semaphore](https://semaphoreapp.com/api/v1/projects/7b914dac-6aa6-43dd-94a9-53698ef1922a/120842/shields_badge.png
5
+ )](https://semaphoreapp.com/minter/conglomerate)
4
6
  [![Code Climate](https://codeclimate.com/github/teamsnap/conglomerate.png)](https://codeclimate.com/github/teamsnap/conglomerate)
5
7
  [![Coverage Status](https://coveralls.io/repos/teamsnap/conglomerate/badge.png?branch=master)](https://coveralls.io/r/teamsnap/conglomerate?branch=master)
6
8
  [![Dependency Status](https://gemnasium.com/teamsnap/conglomerate.png)](https://gemnasium.com/teamsnap/conglomerate)
@@ -10,6 +12,11 @@ A library to serialize Ruby objects into collection+json.
10
12
 
11
13
  ![conglomerate](http://i.imgur.com/QkKZ0ru.jpg)
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
- # TODO
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
@@ -1,3 +1,3 @@
1
1
  module Conglomerate
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -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
@@ -1,3 +1,6 @@
1
+ require "coveralls"
2
+ Coveralls.wear!
3
+
1
4
  RSpec.configure do |config|
2
5
  config.order = "random"
3
6
  end
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.0
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-30 00:00:00.000000000 Z
11
+ date: 2013-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler