braque 0.0.4 → 0.0.5

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: 2ab231c18ebc05e007531d66f7503b7b149213b6
4
- data.tar.gz: d07db5d4abf55f5d8138dd73a904e672df2c064a
3
+ metadata.gz: 695090ace904e767afca4305c5f8ee985d57081f
4
+ data.tar.gz: 3f2507fd0e00192d70753abf29fdc9dcf60fcceb
5
5
  SHA512:
6
- metadata.gz: 5f031712669fc5e2ed29f3a08e95744375d8f60a3608f50172eca74b121c830c68b7f860454080755f60de3df506d716615d4d359ca532f4bdbb038193d653bd
7
- data.tar.gz: deb8befc3e96e1f3ae4d3358d0fb9053517a4217a7ce63f46a7dbc05784cacfd861f8ef400de548fb5e7d85eb808264a6bbd3ed824aea21d10b1815cf1a2dde0
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(id: @article.id)
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(id: id)._patch("#{self.class.instance_method_name}" => params)
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(id: id)._delete
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(id)
41
- response = client.method(instance_method_name).call(id: id)
58
+ def find(options = {})
59
+ response = client.method(instance_method_name).call(options)
42
60
  new response
43
61
  end
44
62
 
@@ -1,3 +1,3 @@
1
1
  module Braque
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -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
- before(:each) do
81
- root_request
82
- @resource_request = WebMock.stub_request(:get, "#{Breeze.api_root}/breezes/1")
83
- .with(headers: { 'Http-Authorization' => Breeze.api_token })
84
- .to_return(status: 200, body: resource_response)
85
- @breeze = Breeze.find 1
86
- end
87
- it 'performs the API root request' do
88
- expect(root_request).to have_been_requested
89
- end
90
- it 'performs the resource request' do
91
- expect(@resource_request).to have_been_requested
92
- end
93
- it 'returns an item with the correct class' do
94
- expect(@breeze).to be_a_kind_of Breeze
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
- it 'returns an item with the correct attributes' do
97
- expect(@breeze.id).to eq resource_response['id']
98
- expect(@breeze.title).to eq resource_response['title']
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
- before(:each) do
133
- root_request
134
- @save_request = WebMock.stub_request(:patch, "#{Breeze.api_root}/breezes/1")
135
- .with(headers: { 'Http-Authorization' => Breeze.api_token })
136
- .to_return(status: 200, body: resource_response)
137
- @params = { title: 'What a nice breeze.' }
138
- @breeze = Breeze.new(id: 1).save(@params)
139
- end
140
- it 'performs the API root request' do
141
- expect(root_request).to have_been_requested
142
- end
143
- it 'performs the save request' do
144
- expect(@save_request).to have_been_requested
145
- end
146
- it 'returns an item with the correct class' do
147
- expect(@breeze).to be_a_kind_of Breeze
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
- it 'returns an item with the correct attributes' do
150
- expect(@breeze.id).to eq resource_response['id']
151
- expect(@breeze.title).to eq resource_response['title']
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
- before(:each) do
157
- root_request
158
- @destroy_request = WebMock.stub_request(:delete, "#{Breeze.api_root}/breezes/1")
159
- .with(headers: { 'Http-Authorization' => Breeze.api_token })
160
- .to_return(status: 200)
161
- @resource_request = WebMock.stub_request(:get, "#{Breeze.api_root}/breezes/1")
162
- .with(headers: { 'Http-Authorization' => Breeze.api_token })
163
- .to_return(status: 200, body: resource_response)
164
- @breeze = Breeze.new(id: 1).destroy
165
- end
166
- it 'performs the API root request' do
167
- expect(root_request).to have_been_requested
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
- it 'performs the destroy request' do
170
- expect(@destroy_request).to have_been_requested
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
@@ -11,6 +11,7 @@
11
11
  "breezes":[
12
12
  {
13
13
  "id":1,
14
+ "token":123,
14
15
  "title":"Southerly Breeze",
15
16
  "_links":{
16
17
  "self":{
@@ -1,5 +1,6 @@
1
1
  {
2
2
  "id":1,
3
+ "token":123,
3
4
  "title":"Southerly Breeze",
4
5
  "created_at":"2015-02-11T18:59:01.669Z",
5
6
  "updated_at":"2015-02-12T00:26:22.255Z",
@@ -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
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-03 00:00:00.000000000 Z
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: