collection-json 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,5 +14,8 @@ Gem::Specification.new do |s|
14
14
  s.rubyforge_project = "collection-json"
15
15
 
16
16
  s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
18
  s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency "rspec"
18
21
  end
@@ -1,13 +1,14 @@
1
+ require 'json'
1
2
  require "collection-json/version"
2
- require "collection-json/response"
3
+ require "collection-json/collection"
3
4
  require "collection-json/builder"
4
5
 
5
6
  COLLECTION_JSON_VERSION = "1.0"
6
- COLLECTION_JSON_HOST = ENV['COLLECTION_JSON_HOST']
7
+ ROOT_NODE = 'collection'
7
8
 
8
9
  module CollectionJSON
9
10
  def self.generate_for(href, &block)
10
- response = Response.new(href)
11
+ response = Collection.new(href)
11
12
  if block_given?
12
13
  builder = Builder.new(response)
13
14
  yield(builder)
@@ -17,10 +18,15 @@ module CollectionJSON
17
18
  end
18
19
 
19
20
  def self.add_host(href)
20
- if COLLECTION_JSON_HOST && !href[/^http/]
21
- COLLECTION_JSON_HOST + href
21
+ if ENV['COLLECTION_JSON_HOST'] && !href[/^http/]
22
+ ENV['COLLECTION_JSON_HOST'] + href
22
23
  else
23
24
  href
24
25
  end
25
26
  end
27
+
28
+ def self.parse(json)
29
+ hash = JSON.parse(json)
30
+ collection = Collection.from_hash(hash)
31
+ end
26
32
  end
@@ -1,27 +1,27 @@
1
1
  module CollectionJSON
2
2
  class Builder
3
- attr_reader :response
3
+ attr_reader :collection
4
4
 
5
- def initialize(response)
6
- @response = response
5
+ def initialize(collection)
6
+ @collection = collection
7
7
  end
8
8
 
9
9
  def set_error(opts = {})
10
- response.error = opts
10
+ collection.error = opts
11
11
  end
12
12
 
13
13
  def set_code(code)
14
- response.code = code
14
+ collection.code = code
15
15
  end
16
16
 
17
17
  def add_link(href, rel, opts = {})
18
18
  href = CollectionJSON.add_host(href)
19
- response.links << opts.merge({rel: rel, href: href})
19
+ collection.links << opts.merge({rel: rel, href: href})
20
20
  end
21
21
 
22
22
  def add_item(href, data = [], links = [], &block)
23
23
  href = CollectionJSON.add_host(href)
24
- response.items << {href: href}.tap do |item|
24
+ collection.items << {href: href}.tap do |item|
25
25
  item_builder = ItemBuilder.new(data, links)
26
26
  yield(item_builder) if block_given?
27
27
  item.merge!({data: item_builder.data}) if item_builder.data.length > 0
@@ -31,7 +31,7 @@ module CollectionJSON
31
31
 
32
32
  def add_query(href, rel, prompt = '', data = [], &block)
33
33
  href = CollectionJSON.add_host(href)
34
- response.queries << {href: href, rel: rel}.tap do |query|
34
+ collection.queries << {href: href, rel: rel}.tap do |query|
35
35
  query_builder = QueryBuilder.new(data)
36
36
  yield(query_builder) if block_given?
37
37
  query.merge!({prompt: prompt}) if prompt != ''
@@ -40,7 +40,7 @@ module CollectionJSON
40
40
  end
41
41
 
42
42
  def set_template(data = [], &block)
43
- response.template = Hash.new.tap do |template|
43
+ collection.template = Hash.new.tap do |template|
44
44
  template_builder = TemplateBuilder.new(data)
45
45
  yield(template_builder) if block_given?
46
46
  template.merge!({data: template_builder.data}) if template_builder.data.length > 0
@@ -1,9 +1,17 @@
1
- require 'json'
2
-
3
1
  module CollectionJSON
4
- class Response
2
+ class Collection
5
3
  attr_reader :href, :links, :items, :queries, :template, :version, :error
6
- attr_writer :links, :items, :queries, :template, :version, :error
4
+ attr_writer :links, :items, :queries, :template, :version, :error
5
+
6
+ def self.from_hash(hash)
7
+ self.new(hash[ROOT_NODE]['href']).tap do |collection|
8
+ %w{items links queries error template}.each do |attribute|
9
+ if hash[ROOT_NODE][attribute]
10
+ collection.send("#{attribute}=", hash[ROOT_NODE][attribute])
11
+ end
12
+ end
13
+ end
14
+ end
7
15
 
8
16
  def initialize(href)
9
17
  @href = CollectionJSON.add_host(href)
@@ -15,6 +23,15 @@ module CollectionJSON
15
23
  @template = nil
16
24
  end
17
25
 
26
+ def collection
27
+ {ROOT_NODE => body}
28
+ end
29
+
30
+ def to_json(*args)
31
+ collection.to_json(args)
32
+ end
33
+
34
+ private
18
35
  def body
19
36
  {href: href, version: version}.tap do |body|
20
37
  %w{items links queries}.each do |child_name|
@@ -25,13 +42,5 @@ module CollectionJSON
25
42
  body.merge!({template: template}) if template
26
43
  end
27
44
  end
28
-
29
- def collection
30
- {collection: body}
31
- end
32
-
33
- def to_json(*args)
34
- collection.to_json(args)
35
- end
36
45
  end
37
46
  end
@@ -1,3 +1,3 @@
1
1
  module CollectionJSON
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,116 @@
1
+ require 'spec_helper'
2
+ require 'collection-json'
3
+
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
+ describe :generate_for do
28
+ before :each do
29
+ @friends = [
30
+ {
31
+ "id" => "jdoe",
32
+ "full-name" => "J. Doe",
33
+ "email" => "jdoe@example.org"
34
+ },
35
+ {
36
+ "id" => "msmith",
37
+ "full-name" => "M. Smith",
38
+ "email" => "msmith@example.org"
39
+ },
40
+ {
41
+ "id" => "rwilliams",
42
+ "full-name" => "R. Williams",
43
+ "email" => "rwilliams@example.org"
44
+ }
45
+ ]
46
+ end
47
+
48
+ it 'should generate an object with the attributes we expect' do
49
+ response = CollectionJSON.generate_for('/friends/') do |builder|
50
+ builder.add_link '/friends/rss', 'feed'
51
+ @friends.each do |friend|
52
+ builder.add_item("/friends/#{friend['id']}") do |item|
53
+ item.add_data "full-name", friend["full-name"]
54
+ item.add_data "email", friend["email"]
55
+ item.add_link "/blogs/#{friend['id']}", "blog", "", "Blog"
56
+ item.add_link "/blogs/#{friend['id']}", "avatar", "", "Avatar", "image"
57
+ end
58
+ end
59
+ builder.add_query("/friends/search", "search", "Search") do |query|
60
+ query.add_data "search"
61
+ end
62
+ builder.set_template do |template|
63
+ template.add_data "full-name", "", "Full Name"
64
+ template.add_data "email", "", "Email"
65
+ template.add_data "blog", "", "Blog"
66
+ template.add_data "avatar", "", "Avatar"
67
+ end
68
+ end
69
+
70
+ response.href.should eq('/friends/')
71
+ response.items.length.should eq(3)
72
+ response.items.first[:data].length.should eq(2)
73
+ response.items.first[:links].length.should eq(2)
74
+ response.items.first[:href].class.should eq(String)
75
+ response.template[:data].length.should eq(4)
76
+ response.queries.length.should eq(1)
77
+ response.queries.first[:href].should eq("/friends/search")
78
+ response.queries.first[:data].length.should eq(1)
79
+ response.queries.first[:data].first[:name].should eq('search')
80
+ end
81
+ end
82
+
83
+ describe :parse do
84
+ before(:all) do
85
+ json = '{"collection": {
86
+ "href": "http://www.example.org/friends",
87
+ "items": [
88
+ {
89
+ "href": "http://www.example.org/m.rowe",
90
+ "data": [
91
+ {"name": "full-name", "value": "Matt Rowe"}
92
+ ]
93
+ }
94
+ ]
95
+ }}'
96
+ @collection = CollectionJSON.parse(json)
97
+ end
98
+
99
+ it 'should parse JSON into a Collection' do
100
+ @collection.class.should eq(CollectionJSON::Collection)
101
+ end
102
+
103
+ it 'should have correct href' do
104
+ @collection.href.should eq("http://www.example.org/friends")
105
+ end
106
+
107
+ it 'should handle the nested attributes' do
108
+ @collection.items.first['href'].should eq("http://www.example.org/m.rowe")
109
+ @collection.items.first['data'].count.should eq(1)
110
+ end
111
+
112
+ it 'should be able to be reserialized' do
113
+ @collection.to_json.class.should eq(String)
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,7 @@
1
+ EXAMPLE_HOST = "http://localhost"
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+ end
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.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,19 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-15 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-04-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70288977749200 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70288977749200
14
25
  description: Lightweight gem for building Collection+JSON responses.
15
26
  email:
16
27
  - sebastian@uprise.co.nz
@@ -25,8 +36,10 @@ files:
25
36
  - collection-json.gemspec
26
37
  - lib/collection-json.rb
27
38
  - lib/collection-json/builder.rb
28
- - lib/collection-json/response.rb
39
+ - lib/collection-json/collection.rb
29
40
  - lib/collection-json/version.rb
41
+ - spec/collection-json_spec.rb
42
+ - spec/spec_helper.rb
30
43
  homepage: https://github.com/sebastianedwards/collection-json
31
44
  licenses: []
32
45
  post_install_message:
@@ -51,4 +64,6 @@ rubygems_version: 1.8.15
51
64
  signing_key:
52
65
  specification_version: 3
53
66
  summary: Builds Collection+JSON responses.
54
- test_files: []
67
+ test_files:
68
+ - spec/collection-json_spec.rb
69
+ - spec/spec_helper.rb