braque 0.0.5 → 0.0.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 695090ace904e767afca4305c5f8ee985d57081f
4
- data.tar.gz: 3f2507fd0e00192d70753abf29fdc9dcf60fcceb
3
+ metadata.gz: dab5d5d54312bfdbc5dd8893a06e73b82efced16
4
+ data.tar.gz: e0ee2e72c8075d1c754fbe25492c439cc6aec1d5
5
5
  SHA512:
6
- metadata.gz: d381d640d339f61c5125d7ee44bb0435c177710e5f0a0737585fbb8c324d8a11663ae1a88d19e1b0605a8f95288c3ac069aa405596fb071aaf08aa4955cead3d
7
- data.tar.gz: dbae5083decd83a6a31d8d30431ebdc03a3925e50e6594a7c3d48ff4d14665079cc518192a5eca6cc92c76eb4d6123aa1c1d8f4c1f1df9d3a6e833bf2be18237
6
+ metadata.gz: a48933aecf76a78a67bc9704ee3a5fe740be0507ec29d61f10ed6b643cb8a29d7afb8cbdc064c25b542b254a71521ac45c910bcddb053e9f5ece5abf885149c1
7
+ data.tar.gz: 83f22620eb6fc152e500a9236f9605f0acab47310c27b94fd6947c9e924f3f94b5386b1ba88bd91dd06baf30553db36edaa5e9383955dee49a2f41c91b847a25
@@ -1,16 +1,18 @@
1
1
  module Braque
2
2
  module Helpers
3
3
  module HypermediaResponsesHelper
4
- def hypermedia_root_json_for(hyperclient_model_class)
4
+ def hypermedia_root_json_for(hyperclient_model_class, options = {})
5
+ collection_query_options = options[:collection_query] || '{?page,size}'
6
+ resource_path_options = options[:resource_path] || '{id}'
5
7
  root_hash = {}
6
8
  root_hash[:_links] = {}
7
9
  root_hash[:_links][:self] = { href: hyperclient_model_class.api_root }
8
- root_hash[:_links][hyperclient_model_class.collection_method_name] = { href: "#{hyperclient_model_class.api_root}/#{hyperclient_model_class.collection_method_name}{?page,size}", templated: true }
9
- root_hash[:_links][hyperclient_model_class.instance_method_name] = { href: "#{hyperclient_model_class.api_root}/#{hyperclient_model_class.collection_method_name}/{id}", templated: true }
10
+ root_hash[:_links][hyperclient_model_class.collection_method_name] = { href: "#{hyperclient_model_class.api_root}/#{hyperclient_model_class.collection_method_name}#{collection_query_options}", templated: true }
11
+ root_hash[:_links][hyperclient_model_class.instance_method_name] = { href: "#{hyperclient_model_class.api_root}/#{hyperclient_model_class.collection_method_name}/#{resource_path_options}", templated: true }
10
12
  JSON.parse root_hash.to_json
11
13
  end
12
14
 
13
- def hypermedia_collection_json_for(hyperclient_model_collection)
15
+ def hypermedia_collection_json_for(hyperclient_model_collection, options = {})
14
16
  hyperclient_model = hyperclient_model_collection.first
15
17
  collection_hash = {}
16
18
  collection_hash[:total_count] = 1
@@ -21,30 +23,32 @@ module Braque
21
23
  collection_hash[:_links][:next] = { href: "#{hyperclient_model_collection.first.class.api_root}/#{hyperclient_model_collection.first.class.collection_method_name}" }
22
24
  collection_hash[:_links][:prev] = { href: "#{hyperclient_model_collection.first.class.api_root}/#{hyperclient_model_collection.first.class.collection_method_name}" }
23
25
  collection_hash[:_embedded] = {}
24
- collection_hash[:_embedded][hyperclient_model_collection.first.class.collection_method_name] = embedded_elements_for_collection(hyperclient_model_collection)
26
+ collection_hash[:_embedded][hyperclient_model_collection.first.class.collection_method_name] = embedded_elements_for_collection(hyperclient_model_collection, options)
25
27
  JSON.parse collection_hash.to_json
26
28
  end
27
29
 
28
- def hypermedia_resource_json_for(hyperclient_model)
29
- resource_hash = embedded_element(hyperclient_model)
30
+ def hypermedia_resource_json_for(hyperclient_model, options = {})
31
+ resource_hash = embedded_element(hyperclient_model, options)
30
32
  JSON.parse resource_hash.to_json
31
33
  end
32
34
 
33
- def embedded_elements_for_collection(hyperclient_model_collection)
35
+ def embedded_elements_for_collection(hyperclient_model_collection, options = {})
34
36
  embedded_elements = []
35
37
  hyperclient_model_collection.each do |model|
36
- embedded_elements << embedded_element(model)
38
+ embedded_elements << embedded_element(model, options)
37
39
  end
38
40
  embedded_elements
39
41
  end
40
42
 
41
- def embedded_element(model)
43
+ def embedded_element(model, options = {})
44
+ resource_path_str = eval(options[:resource_path].to_s) if options[:resource_path]
45
+ resource_path = resource_path_str || model.id
42
46
  hyperclient_element = {}
43
47
  model.attributes.keys.each do |key|
44
48
  hyperclient_element[key] = model.send(key)
45
49
  end
46
50
  hyperclient_element[:_links] = {}
47
- hyperclient_element[:_links][:self] = { href: "#{model.class.api_root}/#{model.class.collection_method_name}/#{model.id}" }
51
+ hyperclient_element[:_links][:self] = { href: "#{model.class.api_root}/#{model.class.collection_method_name}/#{resource_path}" }
48
52
  hyperclient_element
49
53
  end
50
54
  end
@@ -1,3 +1,3 @@
1
1
  module Braque
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -5,6 +5,8 @@ RSpec.describe Braque::Helpers::HypermediaResponsesHelper do
5
5
  include Braque::Helpers::HypermediaResponsesHelper
6
6
 
7
7
  context 'with a Braque::Model class' do
8
+ let(:article) { Article.new(id: 1, title: 'Sculpture in the Expanded Field', key: 'october-spring-1979') }
9
+
8
10
  before(:all) do
9
11
  class Article
10
12
  include ::Braque::Model
@@ -12,69 +14,132 @@ RSpec.describe Braque::Helpers::HypermediaResponsesHelper do
12
14
  self.api_token = 'replace-me'
13
15
  attribute :id
14
16
  attribute :title
17
+ attribute :key
15
18
  end
16
19
  end
17
20
 
18
21
  context 'hypermedia_root_json_for' do
19
- before(:each) do
20
- @root_json = hypermedia_root_json_for Article
21
- end
22
+ context 'defaults' do
23
+ before(:each) do
24
+ @root_json = hypermedia_root_json_for Article
25
+ end
22
26
 
23
- it 'returns the self link' do
24
- expect(@root_json['_links']['self']['href']).to eq Article.api_root
25
- end
27
+ it 'returns the self link' do
28
+ expect(@root_json['_links']['self']['href']).to eq Article.api_root
29
+ end
26
30
 
27
- it 'returns the collection link' do
28
- expect(@root_json['_links']['articles']['href']).to eq "#{Article.api_root}/articles{?page,size}"
31
+ it 'returns the collection link' do
32
+ expect(@root_json['_links']['articles']['href']).to eq "#{Article.api_root}/#{Article.collection_method_name}{?page,size}"
33
+ end
34
+
35
+ it 'returns the resource link' do
36
+ expect(@root_json['_links']['article']['href']).to eq "#{Article.api_root}/#{Article.collection_method_name}/{id}"
37
+ end
29
38
  end
39
+ context 'options' do
40
+ before(:each) do
41
+ @root_json = hypermedia_root_json_for Article, collection_query: '{?page}', resource_path: '{id}?key={key}'
42
+ end
43
+
44
+ it 'returns the self link' do
45
+ expect(@root_json['_links']['self']['href']).to eq Article.api_root
46
+ end
30
47
 
31
- it 'returns the resource link' do
32
- expect(@root_json['_links']['article']['href']).to eq "#{Article.api_root}/articles/{id}"
48
+ it 'returns the collection link using the collection_query option' do
49
+ expect(@root_json['_links']['articles']['href']).to eq "#{Article.api_root}/#{Article.collection_method_name}{?page}"
50
+ end
51
+
52
+ it 'returns the resource link using the resource_path option' do
53
+ expect(@root_json['_links']['article']['href']).to eq "#{Article.api_root}/#{Article.collection_method_name}/{id}?key={key}"
54
+ end
33
55
  end
34
56
  end
35
57
 
36
58
  context 'hypermedia_collection_json_for' do
37
- before(:each) do
38
- @article = Article.new(id: 1, title: 'Sculpture in the Expanded Field')
39
- @collection_json = hypermedia_collection_json_for [@article]
40
- end
59
+ context 'defaults' do
60
+ before(:each) do
61
+ @collection_json = hypermedia_collection_json_for [article]
62
+ end
41
63
 
42
- it 'returns the _links node' do
43
- expect(@collection_json['_links']['self']['href']).to eq "#{Article.api_root}/articles"
44
- expect(@collection_json['_links']['next']['href']).to eq "#{Article.api_root}/articles"
45
- expect(@collection_json['_links']['prev']['href']).to eq "#{Article.api_root}/articles"
46
- end
64
+ it 'returns the _links node' do
65
+ expect(@collection_json['_links']['self']['href']).to eq "#{Article.api_root}/#{Article.collection_method_name}"
66
+ expect(@collection_json['_links']['next']['href']).to eq "#{Article.api_root}/#{Article.collection_method_name}"
67
+ expect(@collection_json['_links']['prev']['href']).to eq "#{Article.api_root}/#{Article.collection_method_name}"
68
+ end
47
69
 
48
- it 'returns the _embedded node' do
49
- expect(@collection_json['_embedded']['articles']).to eq [{ 'id' => @article.id, 'title' => @article.title, '_links' => { 'self' => { 'href' => 'http://localhost:9292/articles/1' } } }]
50
- end
70
+ it 'returns the _embedded node' do
71
+ expect(@collection_json['_embedded']['articles']).to eq [{ 'id' => article.id, 'title' => article.title, 'key' => article.key, '_links' => { 'self' => { 'href' => "#{Article.api_root}/#{Article.collection_method_name}/#{article.id}" } } }]
72
+ end
51
73
 
52
- it 'returns the current_page' do
53
- expect(@collection_json['current_page']).to eq 1
54
- end
74
+ it 'returns the current_page' do
75
+ expect(@collection_json['current_page']).to eq 1
76
+ end
77
+
78
+ it 'returns the total_count' do
79
+ expect(@collection_json['total_count']).to eq 1
80
+ end
55
81
 
56
- it 'returns the total_count' do
57
- expect(@collection_json['total_count']).to eq 1
82
+ it 'returns the total_pages' do
83
+ expect(@collection_json['total_pages']).to eq 1
84
+ end
58
85
  end
86
+ context 'options' do
87
+ before(:each) do
88
+ @collection_json = hypermedia_collection_json_for [article], resource_path: '"#{model.id}?key=#{model.key}"'
89
+ end
90
+
91
+ it 'returns the _links node' do
92
+ expect(@collection_json['_links']['self']['href']).to eq "#{Article.api_root}/#{Article.collection_method_name}"
93
+ expect(@collection_json['_links']['next']['href']).to eq "#{Article.api_root}/#{Article.collection_method_name}"
94
+ expect(@collection_json['_links']['prev']['href']).to eq "#{Article.api_root}/#{Article.collection_method_name}"
95
+ end
96
+
97
+ it 'returns the _embedded node' do
98
+ expect(@collection_json['_embedded']['articles']).to eq [{ 'id' => article.id, 'title' => article.title, 'key' => article.key, '_links' => { 'self' => { 'href' => "#{Article.api_root}/#{Article.collection_method_name}/#{article.id}?key=#{article.key}" } } }]
99
+ end
100
+
101
+ it 'returns the current_page' do
102
+ expect(@collection_json['current_page']).to eq 1
103
+ end
59
104
 
60
- it 'returns the total_pages' do
61
- expect(@collection_json['total_pages']).to eq 1
105
+ it 'returns the total_count' do
106
+ expect(@collection_json['total_count']).to eq 1
107
+ end
108
+
109
+ it 'returns the total_pages' do
110
+ expect(@collection_json['total_pages']).to eq 1
111
+ end
62
112
  end
63
113
  end
64
114
 
65
115
  context 'hypermedia_resource_json_for' do
66
- before(:each) do
67
- @article = Article.new(id: 1, title: 'Sculpture in the Expanded Field')
68
- @resource_json = hypermedia_resource_json_for @article
69
- end
116
+ context 'default' do
117
+ before(:each) do
118
+ @resource_json = hypermedia_resource_json_for article
119
+ end
70
120
 
71
- it 'returns the _links node' do
72
- expect(@resource_json['_links']['self']['href']).to eq "#{Article.api_root}/articles/1"
121
+ it 'returns the _links node' do
122
+ expect(@resource_json['_links']['self']['href']).to eq "#{Article.api_root}/#{Article.collection_method_name}/#{article.id}"
123
+ end
124
+
125
+ it 'returns the resource attributes' do
126
+ expect(@resource_json['id']).to eq article.id
127
+ expect(@resource_json['title']).to eq article.title
128
+ end
73
129
  end
130
+ context 'options' do
131
+ before(:each) do
132
+ @resource_json = hypermedia_resource_json_for article, resource_path: '"#{model.id}?key=#{model.key}"'
133
+ end
134
+
135
+ it 'returns the _links node' do
136
+ expect(@resource_json['_links']['self']['href']).to eq "#{Article.api_root}/#{Article.collection_method_name}/#{article.id}?key=#{article.key}"
137
+ end
74
138
 
75
- it 'returns the resource attributes' do
76
- expect(@resource_json['id']).to eq @article.id
77
- expect(@resource_json['title']).to eq @article.title
139
+ it 'returns the resource attributes' do
140
+ expect(@resource_json['id']).to eq article.id
141
+ expect(@resource_json['title']).to eq article.title
142
+ end
78
143
  end
79
144
  end
80
145
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: braque
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dylan Fareed
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-05 00:00:00.000000000 Z
11
+ date: 2015-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hyperclient
@@ -96,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
96
  version: '0'
97
97
  requirements: []
98
98
  rubyforge_project:
99
- rubygems_version: 2.4.3
99
+ rubygems_version: 2.2.2
100
100
  signing_key:
101
101
  specification_version: 4
102
102
  summary: Braque provides a simple interface for interacting with Hypermedia API services