collection-json 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -9
- data/lib/collection-json.rb +6 -15
- data/lib/collection-json/attribute.rb +3 -3
- data/lib/collection-json/attributes/collection.rb +1 -1
- data/lib/collection-json/builder.rb +5 -7
- data/lib/collection-json/version.rb +1 -1
- data/spec/collection-json/transformers/uri_spec.rb +24 -0
- data/spec/collection-json_spec.rb +9 -31
- metadata +5 -3
data/README.md
CHANGED
@@ -18,20 +18,20 @@ collection = CollectionJSON.generate_for('/friends/') do |builder|
|
|
18
18
|
builder.add_link '/friends/rss', 'feed'
|
19
19
|
user.friends.each do |friend|
|
20
20
|
builder.add_item("/friends/#{friend.id}") do |item|
|
21
|
-
item.add_data "full-name",
|
22
|
-
item.add_data "email",
|
23
|
-
item.add_link "/blogs/#{friend.id}", "blog",
|
24
|
-
item.add_link "/blogs/#{friend.id}", "avatar",
|
21
|
+
item.add_data "full-name", value: friend.full_name
|
22
|
+
item.add_data "email", value: friend.email
|
23
|
+
item.add_link "/blogs/#{friend.id}", "blog", prompt: "Blog"
|
24
|
+
item.add_link "/blogs/#{friend.id}", "avatar", prompt: "Avatar", render: "image"
|
25
25
|
end
|
26
26
|
end
|
27
|
-
builder.add_query("/friends/search", "search",
|
27
|
+
builder.add_query("/friends/search", "search", prompt: "Search") do |query|
|
28
28
|
query.add_data "search"
|
29
29
|
end
|
30
30
|
builder.set_template do |template|
|
31
|
-
template.add_data "full-name",
|
32
|
-
template.add_data "email",
|
33
|
-
template.add_data "blog",
|
34
|
-
template.add_data "avatar",
|
31
|
+
template.add_data "full-name", prompt: "Full Name"
|
32
|
+
template.add_data "email", prompt: "Email"
|
33
|
+
template.add_data "blog", prompt: "Blog"
|
34
|
+
template.add_data "avatar", prompt: "Avatar"
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
data/lib/collection-json.rb
CHANGED
@@ -8,21 +8,12 @@ ROOT_NODE = 'collection'
|
|
8
8
|
|
9
9
|
module CollectionJSON
|
10
10
|
def self.generate_for(href, &block)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
response
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.add_host(href)
|
22
|
-
if ENV['COLLECTION_JSON_HOST'] && !href[/^http/]
|
23
|
-
ENV['COLLECTION_JSON_HOST'] + href
|
24
|
-
else
|
25
|
-
href
|
11
|
+
Collection.new.tap do |response|
|
12
|
+
response.href href
|
13
|
+
if block_given?
|
14
|
+
builder = Builder.new(response)
|
15
|
+
yield(builder)
|
16
|
+
end
|
26
17
|
end
|
27
18
|
end
|
28
19
|
|
@@ -20,10 +20,10 @@ module CollectionJSON
|
|
20
20
|
instance_variable_set(:"@#{name}", arg)
|
21
21
|
end
|
22
22
|
else
|
23
|
-
|
24
|
-
|
23
|
+
if instance_variable_get(:"@#{name}").nil? && opts[:default]
|
24
|
+
instance_variable_set(:"@#{name}", opts[:default].dup)
|
25
25
|
else
|
26
|
-
|
26
|
+
instance_variable_get(:"@#{name}")
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
@@ -20,6 +20,6 @@ module CollectionJSON
|
|
20
20
|
transform: lambda { |queries| queries.each.map { |q| Query.from_hash(q) }},
|
21
21
|
default: []
|
22
22
|
attribute :template, transform: lambda { |template| Template.from_hash(template) }
|
23
|
-
attribute :error, transform: lambda { |
|
23
|
+
attribute :error, transform: lambda { |error| Error.from_hash(error) }
|
24
24
|
end
|
25
25
|
end
|
@@ -1,23 +1,21 @@
|
|
1
1
|
module CollectionJSON
|
2
2
|
class Builder
|
3
|
-
attr_reader :collection
|
4
|
-
|
5
3
|
def initialize(collection)
|
6
4
|
@collection = collection
|
7
5
|
end
|
8
6
|
|
9
7
|
def set_error(params = {})
|
10
|
-
collection.error = params
|
8
|
+
@collection.error = params
|
11
9
|
end
|
12
10
|
|
13
11
|
def add_link(href, rel, params = {})
|
14
12
|
params.merge!({'rel' => rel, 'href' => href})
|
15
|
-
collection.links << Link.from_hash(params)
|
13
|
+
@collection.links << Link.from_hash(params)
|
16
14
|
end
|
17
15
|
|
18
16
|
def add_item(href, params = {}, &block)
|
19
17
|
params.merge!({'href' => href})
|
20
|
-
collection.items << Item.from_hash(params).tap do |item|
|
18
|
+
@collection.items << Item.from_hash(params).tap do |item|
|
21
19
|
if block_given?
|
22
20
|
data = []
|
23
21
|
links = []
|
@@ -31,7 +29,7 @@ module CollectionJSON
|
|
31
29
|
|
32
30
|
def add_query(href, rel, params = {}, &block)
|
33
31
|
params.merge!({'href' => href, 'rel' => rel})
|
34
|
-
collection.queries << Query.from_hash(params).tap do |query|
|
32
|
+
@collection.queries << Query.from_hash(params).tap do |query|
|
35
33
|
data = []
|
36
34
|
query_builder = QueryBuilder.new(data)
|
37
35
|
yield(query_builder) if block_given?
|
@@ -46,7 +44,7 @@ module CollectionJSON
|
|
46
44
|
yield(template_builder)
|
47
45
|
params.merge!({'data' => data})
|
48
46
|
end
|
49
|
-
collection.template params
|
47
|
+
@collection.template params
|
50
48
|
end
|
51
49
|
end
|
52
50
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'collection-json/transformers/uri'
|
3
|
+
|
4
|
+
describe CollectionJSON::URI do
|
5
|
+
before :each do
|
6
|
+
@href = '/friends'
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'with COLLECTION_JSON_HOST set' do
|
10
|
+
it 'returns full uri' do
|
11
|
+
ENV['COLLECTION_JSON_HOST'] = EXAMPLE_HOST
|
12
|
+
uri = CollectionJSON::URI.call(@href)
|
13
|
+
uri.should eq("http://localhost/friends")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'without COLLECTION_JSON_HOST set' do
|
18
|
+
it 'returns partial uri' do
|
19
|
+
ENV['COLLECTION_JSON_HOST'] = nil
|
20
|
+
uri = CollectionJSON::URI.call(@href)
|
21
|
+
uri.should eq("/friends")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -2,28 +2,6 @@ require 'spec_helper'
|
|
2
2
|
require 'collection-json'
|
3
3
|
|
4
4
|
describe CollectionJSON do
|
5
|
-
describe :add_host do
|
6
|
-
before :each do
|
7
|
-
@href = '/friends'
|
8
|
-
end
|
9
|
-
|
10
|
-
context 'with COLLECTION_JSON_HOST set' do
|
11
|
-
it 'returns full uri' do
|
12
|
-
ENV['COLLECTION_JSON_HOST'] = EXAMPLE_HOST
|
13
|
-
uri = CollectionJSON.add_host(@href)
|
14
|
-
uri.should eq("http://localhost/friends")
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'without COLLECTION_JSON_HOST set' do
|
19
|
-
it 'returns partial uri' do
|
20
|
-
ENV['COLLECTION_JSON_HOST'] = nil
|
21
|
-
uri = CollectionJSON.add_host(@href)
|
22
|
-
uri.should eq("/friends")
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
5
|
describe :generate_for do
|
28
6
|
before :each do
|
29
7
|
@friends = [
|
@@ -50,20 +28,20 @@ describe CollectionJSON do
|
|
50
28
|
builder.add_link '/friends/rss', 'feed'
|
51
29
|
@friends.each do |friend|
|
52
30
|
builder.add_item("/friends/#{friend['id']}") do |item|
|
53
|
-
item.add_data "full-name",
|
54
|
-
item.add_data "email",
|
55
|
-
item.add_link "/blogs/#{friend['id']}", "blog",
|
56
|
-
item.add_link "/blogs/#{friend['id']}", "avatar",
|
31
|
+
item.add_data "full-name", value: friend["full-name"]
|
32
|
+
item.add_data "email", value: friend["email"]
|
33
|
+
item.add_link "/blogs/#{friend['id']}", "blog", prompt: "Blog"
|
34
|
+
item.add_link "/blogs/#{friend['id']}", "avatar", prompt: "Avatar", render: 'image'
|
57
35
|
end
|
58
36
|
end
|
59
|
-
builder.add_query("/friends/search", "search",
|
37
|
+
builder.add_query("/friends/search", "search", prompt: "Search") do |query|
|
60
38
|
query.add_data "search"
|
61
39
|
end
|
62
40
|
builder.set_template do |template|
|
63
|
-
template.add_data "full-name",
|
64
|
-
template.add_data "email",
|
65
|
-
template.add_data "blog",
|
66
|
-
template.add_data "avatar",
|
41
|
+
template.add_data "full-name", prompt: "Full Name"
|
42
|
+
template.add_data "email", prompt: "Email"
|
43
|
+
template.add_data "blog", prompt: "Blog"
|
44
|
+
template.add_data "avatar", prompt: "Avatar"
|
67
45
|
end
|
68
46
|
end
|
69
47
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: collection-json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-04-19 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70137055564260 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70137055564260
|
25
25
|
description: Lightweight gem for building Collection+JSON responses.
|
26
26
|
email:
|
27
27
|
- sebastian@uprise.co.nz
|
@@ -49,6 +49,7 @@ files:
|
|
49
49
|
- spec/collection-json/attributes/collection_spec.rb
|
50
50
|
- spec/collection-json/attributes/item_spec.rb
|
51
51
|
- spec/collection-json/attributes/template_spec.rb
|
52
|
+
- spec/collection-json/transformers/uri_spec.rb
|
52
53
|
- spec/collection-json_spec.rb
|
53
54
|
- spec/spec_helper.rb
|
54
55
|
homepage: https://github.com/sebastianedwards/collection-json
|
@@ -79,5 +80,6 @@ test_files:
|
|
79
80
|
- spec/collection-json/attributes/collection_spec.rb
|
80
81
|
- spec/collection-json/attributes/item_spec.rb
|
81
82
|
- spec/collection-json/attributes/template_spec.rb
|
83
|
+
- spec/collection-json/transformers/uri_spec.rb
|
82
84
|
- spec/collection-json_spec.rb
|
83
85
|
- spec/spec_helper.rb
|