braque 0.0.4 → 0.0.5
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/README.md +2 -2
- data/lib/braque/model.rb +22 -4
- data/lib/braque/version.rb +1 -1
- data/spec/braque/model_spec.rb +148 -51
- data/spec/fixtures/collection.json +1 -0
- data/spec/fixtures/resource.json +1 -0
- data/spec/fixtures/root_with_token_resource_path.json +18 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 695090ace904e767afca4305c5f8ee985d57081f
|
4
|
+
data.tar.gz: 3f2507fd0e00192d70753abf29fdc9dcf60fcceb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d381d640d339f61c5125d7ee44bb0435c177710e5f0a0737585fbb8c324d8a11663ae1a88d19e1b0605a8f95288c3ac069aa405596fb071aaf08aa4955cead3d
|
7
|
+
data.tar.gz: dbae5083decd83a6a31d8d30431ebdc03a3925e50e6594a7c3d48ff4d14665079cc518192a5eca6cc92c76eb4d6123aa1c1d8f4c1f1df9d3a6e833bf2be18237
|
data/README.md
CHANGED
@@ -51,7 +51,7 @@ class ArticlesController < ApplicationController
|
|
51
51
|
|
52
52
|
def update
|
53
53
|
@article = @article.save params[:article]
|
54
|
-
redirect_to article_path(
|
54
|
+
redirect_to article_path(@article.id)
|
55
55
|
end
|
56
56
|
|
57
57
|
def destroy
|
@@ -62,7 +62,7 @@ class ArticlesController < ApplicationController
|
|
62
62
|
private
|
63
63
|
|
64
64
|
def find_article
|
65
|
-
@article = Article.find params[:id]
|
65
|
+
@article = Article.find(id: params[:id])
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
data/lib/braque/model.rb
CHANGED
@@ -4,17 +4,35 @@ module Braque
|
|
4
4
|
include ::ActiveAttr::Model
|
5
5
|
|
6
6
|
included do
|
7
|
+
# NOTE: This assumes that the related API uses an ID attribute to
|
8
|
+
# define the resource. Rails will use this field to construct
|
9
|
+
# resource routes in client applications.
|
10
|
+
#
|
11
|
+
# to_param can be overridden when defining Braque::Model classes
|
12
|
+
# in client apps if some other scheme is more advisable.
|
13
|
+
#
|
7
14
|
def to_param
|
8
15
|
"#{id}"
|
9
16
|
end
|
10
17
|
|
18
|
+
# NOTE: This assumes that the related API uses an ID field exclusively
|
19
|
+
# to locate this resource. resource_find_options is used to build the
|
20
|
+
# url path to the API resource
|
21
|
+
#
|
22
|
+
# resource_find_options can be overridden when defining Braque::Model
|
23
|
+
# classes in client apps if some other scheme is more advisable.
|
24
|
+
#
|
25
|
+
def resource_find_options
|
26
|
+
{ id: id }
|
27
|
+
end
|
28
|
+
|
11
29
|
def save(params = {})
|
12
|
-
response = self.class.client.method(self.class.instance_method_name).call(
|
30
|
+
response = self.class.client.method(self.class.instance_method_name).call(resource_find_options)._patch("#{self.class.instance_method_name}" => params)
|
13
31
|
self.class.new response
|
14
32
|
end
|
15
33
|
|
16
34
|
def destroy
|
17
|
-
response = self.class.client.method(self.class.instance_method_name).call(
|
35
|
+
response = self.class.client.method(self.class.instance_method_name).call(resource_find_options)._delete
|
18
36
|
end
|
19
37
|
|
20
38
|
class_eval <<-WRITER
|
@@ -37,8 +55,8 @@ module Braque
|
|
37
55
|
LinkedArray.new response, self
|
38
56
|
end
|
39
57
|
|
40
|
-
def find(
|
41
|
-
response = client.method(instance_method_name).call(
|
58
|
+
def find(options = {})
|
59
|
+
response = client.method(instance_method_name).call(options)
|
42
60
|
new response
|
43
61
|
end
|
44
62
|
|
data/lib/braque/version.rb
CHANGED
data/spec/braque/model_spec.rb
CHANGED
@@ -8,18 +8,27 @@ RSpec.describe Braque::Model, type: :model do
|
|
8
8
|
self.api_token = 'replace-me'
|
9
9
|
attribute :id
|
10
10
|
attribute :title
|
11
|
+
attribute :token
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
14
15
|
let(:root_response) { JSON.parse(File.read 'spec/fixtures/root.json') }
|
16
|
+
let(:root_response_with_token_resource_path) { JSON.parse(File.read 'spec/fixtures/root_with_token_resource_path.json') }
|
15
17
|
let(:collection_response) { JSON.parse(File.read 'spec/fixtures/collection.json') }
|
16
18
|
let(:resource_response) { JSON.parse(File.read 'spec/fixtures/resource.json') }
|
19
|
+
|
17
20
|
let(:root_request) do
|
18
21
|
WebMock.stub_request(:get, "#{Breeze.api_root}/")
|
19
22
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
20
23
|
.to_return(status: 200, body: root_response)
|
21
24
|
end
|
22
25
|
|
26
|
+
let(:root_request_with_token_resource_path) do
|
27
|
+
WebMock.stub_request(:get, "#{Breeze.api_root}/")
|
28
|
+
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
29
|
+
.to_return(status: 200, body: root_response_with_token_resource_path)
|
30
|
+
end
|
31
|
+
|
23
32
|
context 'class methods' do
|
24
33
|
it 'responds to find, create, and list' do
|
25
34
|
expect(Breeze).to respond_to(:find, :create, :list)
|
@@ -77,25 +86,56 @@ RSpec.describe Braque::Model, type: :model do
|
|
77
86
|
end
|
78
87
|
|
79
88
|
context '.find' do
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
89
|
+
context 'under normal conditions' do
|
90
|
+
before(:each) do
|
91
|
+
root_request
|
92
|
+
@resource_request = WebMock.stub_request(:get, "#{Breeze.api_root}/breezes/1")
|
93
|
+
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
94
|
+
.to_return(status: 200, body: resource_response)
|
95
|
+
@breeze = Breeze.find(id: 1)
|
96
|
+
end
|
97
|
+
it 'performs the API root request' do
|
98
|
+
expect(root_request).to have_been_requested
|
99
|
+
end
|
100
|
+
it 'performs the resource request' do
|
101
|
+
expect(@resource_request).to have_been_requested
|
102
|
+
end
|
103
|
+
it 'returns an item with the correct class' do
|
104
|
+
expect(@breeze).to be_a_kind_of Breeze
|
105
|
+
end
|
106
|
+
it 'returns an item with the correct attributes' do
|
107
|
+
expect(@breeze.id).to eq resource_response['id']
|
108
|
+
expect(@breeze.title).to eq resource_response['title']
|
109
|
+
end
|
95
110
|
end
|
96
|
-
|
97
|
-
|
98
|
-
|
111
|
+
context 'when overriding resource_find_options' do
|
112
|
+
before(:each) do
|
113
|
+
class Breeze
|
114
|
+
def resource_find_options
|
115
|
+
{ id: id, token: token }
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
root_request_with_token_resource_path
|
120
|
+
@resource_request = WebMock.stub_request(:get, "#{Breeze.api_root}/breezes/1?token=123")
|
121
|
+
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
122
|
+
.to_return(status: 200, body: resource_response)
|
123
|
+
@breeze = Breeze.find(id: 1, token: 123)
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'performs the API root request' do
|
127
|
+
expect(root_request_with_token_resource_path).to have_been_requested
|
128
|
+
end
|
129
|
+
it 'performs the resource request' do
|
130
|
+
expect(@resource_request).to have_been_requested
|
131
|
+
end
|
132
|
+
it 'returns an item with the correct class' do
|
133
|
+
expect(@breeze).to be_a_kind_of Breeze
|
134
|
+
end
|
135
|
+
it 'returns an item with the correct attributes' do
|
136
|
+
expect(@breeze.id).to eq resource_response['id']
|
137
|
+
expect(@breeze.title).to eq resource_response['title']
|
138
|
+
end
|
99
139
|
end
|
100
140
|
end
|
101
141
|
|
@@ -129,45 +169,102 @@ RSpec.describe Braque::Model, type: :model do
|
|
129
169
|
end
|
130
170
|
|
131
171
|
context '#save' do
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
172
|
+
context 'under normal conditions' do
|
173
|
+
before(:each) do
|
174
|
+
root_request
|
175
|
+
@save_request = WebMock.stub_request(:patch, "#{Breeze.api_root}/breezes/1")
|
176
|
+
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
177
|
+
.to_return(status: 200, body: resource_response)
|
178
|
+
@params = { title: 'What a nice breeze.' }
|
179
|
+
@breeze = Breeze.new(id: 1).save(@params)
|
180
|
+
end
|
181
|
+
it 'performs the API root request' do
|
182
|
+
expect(root_request).to have_been_requested
|
183
|
+
end
|
184
|
+
it 'performs the save request' do
|
185
|
+
expect(@save_request).to have_been_requested
|
186
|
+
end
|
187
|
+
it 'returns an item with the correct class' do
|
188
|
+
expect(@breeze).to be_a_kind_of Breeze
|
189
|
+
end
|
190
|
+
it 'returns an item with the correct attributes' do
|
191
|
+
expect(@breeze.id).to eq resource_response['id']
|
192
|
+
expect(@breeze.title).to eq resource_response['title']
|
193
|
+
end
|
148
194
|
end
|
149
|
-
|
150
|
-
|
151
|
-
|
195
|
+
|
196
|
+
context 'when overriding resource_find_options' do
|
197
|
+
before(:each) do
|
198
|
+
class Breeze
|
199
|
+
def resource_find_options
|
200
|
+
{ id: id, token: token }
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
root_request_with_token_resource_path
|
205
|
+
@save_request = WebMock.stub_request(:patch, "#{Breeze.api_root}/breezes/1?token=123")
|
206
|
+
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
207
|
+
.to_return(status: 200, body: resource_response)
|
208
|
+
@params = { title: 'What a nice breeze.' }
|
209
|
+
@breeze = Breeze.new(id: 1, token: 123).save(@params)
|
210
|
+
end
|
211
|
+
|
212
|
+
it 'performs the API root request' do
|
213
|
+
expect(root_request_with_token_resource_path).to have_been_requested
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'performs the save request' do
|
217
|
+
expect(@save_request).to have_been_requested
|
218
|
+
end
|
219
|
+
|
220
|
+
it 'returns an item with the correct class' do
|
221
|
+
expect(@breeze).to be_a_kind_of Breeze
|
222
|
+
end
|
223
|
+
|
224
|
+
it 'returns an item with the correct attributes' do
|
225
|
+
expect(@breeze.id).to eq resource_response['id']
|
226
|
+
expect(@breeze.title).to eq resource_response['title']
|
227
|
+
end
|
152
228
|
end
|
153
229
|
end
|
154
230
|
|
155
231
|
context '#destroy' do
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
232
|
+
context 'under normal conditions' do
|
233
|
+
before(:each) do
|
234
|
+
root_request
|
235
|
+
@destroy_request = WebMock.stub_request(:delete, "#{Breeze.api_root}/breezes/1")
|
236
|
+
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
237
|
+
.to_return(status: 200)
|
238
|
+
@breeze = Breeze.new(id: 1).destroy
|
239
|
+
end
|
240
|
+
it 'performs the API root request' do
|
241
|
+
expect(root_request).to have_been_requested
|
242
|
+
end
|
243
|
+
it 'performs the destroy request' do
|
244
|
+
expect(@destroy_request).to have_been_requested
|
245
|
+
end
|
168
246
|
end
|
169
|
-
|
170
|
-
|
247
|
+
|
248
|
+
context 'when overriding resource_find_options' do
|
249
|
+
before(:each) do
|
250
|
+
class Breeze
|
251
|
+
def resource_find_options
|
252
|
+
{ id: id, token: token }
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
root_request_with_token_resource_path
|
257
|
+
@destroy_request = WebMock.stub_request(:delete, "#{Breeze.api_root}/breezes/1?token=123")
|
258
|
+
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
259
|
+
.to_return(status: 200)
|
260
|
+
@breeze = Breeze.new(id: 1, token: 123).destroy
|
261
|
+
end
|
262
|
+
it 'performs the API root request' do
|
263
|
+
expect(root_request_with_token_resource_path).to have_been_requested
|
264
|
+
end
|
265
|
+
it 'performs the destroy request' do
|
266
|
+
expect(@destroy_request).to have_been_requested
|
267
|
+
end
|
171
268
|
end
|
172
269
|
end
|
173
270
|
end
|
data/spec/fixtures/resource.json
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
{
|
2
|
+
"_links":{
|
3
|
+
"self":{
|
4
|
+
"href":"http://localhost:9292"
|
5
|
+
},
|
6
|
+
"health":{
|
7
|
+
"href":"http://localhost:9292/health"
|
8
|
+
},
|
9
|
+
"breezes":{
|
10
|
+
"href":"http://localhost:9292/breezes{?page,size,ids%5B%5D*}",
|
11
|
+
"templated":true
|
12
|
+
},
|
13
|
+
"breeze":{
|
14
|
+
"href":"http://localhost:9292/breezes/{id}?token={token}",
|
15
|
+
"templated":true
|
16
|
+
}
|
17
|
+
}
|
18
|
+
}
|
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.
|
4
|
+
version: 0.0.5
|
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-
|
11
|
+
date: 2015-03-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hyperclient
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- spec/fixtures/collection.json
|
75
75
|
- spec/fixtures/resource.json
|
76
76
|
- spec/fixtures/root.json
|
77
|
+
- spec/fixtures/root_with_token_resource_path.json
|
77
78
|
- spec/spec_helper.rb
|
78
79
|
homepage: https://github.com/dylanfareed/braque
|
79
80
|
licenses:
|