braque 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/braque/helpers/hypermedia_responses_helper.rb +4 -4
- data/lib/braque/model.rb +5 -3
- data/lib/braque/version.rb +1 -1
- data/spec/braque/client_headers_spec.rb +49 -0
- data/spec/braque/collection_spec.rb +3 -5
- data/spec/braque/model_spec.rb +23 -4
- data/spec/fixtures/collection.json +11 -1
- metadata +8 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e8bbfc9dc19e8eb3f4130366f9aa757bbb122ae
|
4
|
+
data.tar.gz: e574170f094b58b9f624a04461986978cd35de83
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8900c7e630a9e9a68c0ed67141895543205949bae1dde1192c543821b8394c75bf9922298449886120ae558f25c0a58243040fd372ccd859770e8ebf54bd94b2
|
7
|
+
data.tar.gz: 6ea404e052ea8412412635cc5d1abba74ae5a270ca29d0e8ab621d3a342f18d91db2e46a7e99bba928fe7780889e665e675dbce6bf51c571af99a7eb9e73ad6e
|
@@ -19,11 +19,11 @@ module Braque
|
|
19
19
|
collection_hash[:total_pages] = 1
|
20
20
|
collection_hash[:current_page] = 1
|
21
21
|
collection_hash[:_links] = {}
|
22
|
-
collection_hash[:_links][:self] = { href: "#{
|
23
|
-
collection_hash[:_links][:next] = { href: "#{
|
24
|
-
collection_hash[:_links][:prev] = { href: "#{
|
22
|
+
collection_hash[:_links][:self] = { href: "#{hyperclient_model.class.api_root}/#{hyperclient_model.class.collection_method_name}" }
|
23
|
+
collection_hash[:_links][:next] = { href: "#{hyperclient_model.class.api_root}/#{hyperclient_model.class.collection_method_name}" }
|
24
|
+
collection_hash[:_links][:prev] = { href: "#{hyperclient_model.class.api_root}/#{hyperclient_model.class.collection_method_name}" }
|
25
25
|
collection_hash[:_embedded] = {}
|
26
|
-
collection_hash[:_embedded][
|
26
|
+
collection_hash[:_embedded][hyperclient_model.class.collection_method_name] = embedded_elements_for_collection(hyperclient_model_collection, options)
|
27
27
|
JSON.parse collection_hash.to_json
|
28
28
|
end
|
29
29
|
|
data/lib/braque/model.rb
CHANGED
@@ -6,6 +6,7 @@ module Braque
|
|
6
6
|
included do
|
7
7
|
class_attribute :api_root
|
8
8
|
class_attribute :api_token
|
9
|
+
class_attribute :accept_header
|
9
10
|
|
10
11
|
# NOTE: This assumes that the related API uses an ID attribute to
|
11
12
|
# define the resource. Rails will use this field to construct
|
@@ -15,7 +16,7 @@ module Braque
|
|
15
16
|
# in client apps if some other scheme is more advisable.
|
16
17
|
#
|
17
18
|
def to_param
|
18
|
-
|
19
|
+
id.to_s
|
19
20
|
end
|
20
21
|
|
21
22
|
# NOTE: This assumes that the related API uses an ID field exclusively
|
@@ -35,7 +36,7 @@ module Braque
|
|
35
36
|
end
|
36
37
|
|
37
38
|
def destroy
|
38
|
-
|
39
|
+
self.class.client.method(self.class.instance_method_name).call(resource_find_options)._delete
|
39
40
|
end
|
40
41
|
|
41
42
|
class_eval <<-WRITER
|
@@ -86,7 +87,8 @@ module Braque
|
|
86
87
|
|
87
88
|
def client
|
88
89
|
Hyperclient.new(api_root) do |client|
|
89
|
-
client.headers['Http-Authorization'] = api_token
|
90
|
+
client.headers['Http-Authorization'] = api_token if api_token
|
91
|
+
client.headers['Accept'] = accept_header if accept_header
|
90
92
|
end
|
91
93
|
end
|
92
94
|
end
|
data/lib/braque/version.rb
CHANGED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'Hyperclient header configuration' do
|
4
|
+
let(:root_request) { WebMock.stub_request(:get, 'http://localhost:9292/') }
|
5
|
+
|
6
|
+
before do
|
7
|
+
root_request
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'with a Braque::Model class that defines an api token' do
|
11
|
+
class Mantle
|
12
|
+
include ::Braque::Model
|
13
|
+
self.api_root = 'http://localhost:9292'
|
14
|
+
self.api_token = 'replace-me'
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'passes api_token via Http-Authorization headers with requests' do
|
18
|
+
Mantle.client._get
|
19
|
+
expect(
|
20
|
+
root_request
|
21
|
+
.with(headers: { 'Http-Authorization' => Mantle.api_token })
|
22
|
+
).to have_been_requested
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'passes default Hyperclient Accept headers with requests' do
|
26
|
+
Mantle.client._get
|
27
|
+
expect(
|
28
|
+
root_request
|
29
|
+
.with(headers: { 'Accept' => 'application/hal+json,application/json' })
|
30
|
+
).to have_been_requested
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'with a Braque::Model class that defines an accept_header' do
|
35
|
+
class Subduction
|
36
|
+
include ::Braque::Model
|
37
|
+
self.api_root = 'http://localhost:9292'
|
38
|
+
self.accept_header = 'application/vnd.earth-v1+json'
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'includes class defined accept_header with request headers' do
|
42
|
+
Subduction.client._get
|
43
|
+
expect(
|
44
|
+
root_request
|
45
|
+
.with(headers: { 'Accept' => 'application/vnd.earth-v1+json' })
|
46
|
+
).to have_been_requested
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
3
|
RSpec.describe Braque::Collection do
|
6
4
|
before(:all) do
|
7
5
|
class BraqueClass
|
@@ -58,9 +56,9 @@ RSpec.describe Braque::Collection do
|
|
58
56
|
expect(@linked_array.first.id).to eq 1
|
59
57
|
expect(@linked_array.first.title).to eq @array.first[:title]
|
60
58
|
expect(@linked_array.first.class.name).to eq 'BraqueClass'
|
61
|
-
expect(@linked_array
|
62
|
-
expect(@linked_array
|
63
|
-
expect(@linked_array
|
59
|
+
expect(@linked_array.last.id).to eq 2
|
60
|
+
expect(@linked_array.last.title).to eq @array[1][:title]
|
61
|
+
expect(@linked_array.last.class.name).to eq 'BraqueClass'
|
64
62
|
end
|
65
63
|
|
66
64
|
it 'responds to previous_link with the correct value' do
|
data/spec/braque/model_spec.rb
CHANGED
@@ -6,6 +6,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
6
6
|
include ::Braque::Model
|
7
7
|
self.api_root = 'http://localhost:9292'
|
8
8
|
self.api_token = 'replace-me'
|
9
|
+
self.accept_header = 'application/vnd.el-nino-v1+json'
|
9
10
|
attribute :id
|
10
11
|
attribute :title
|
11
12
|
attribute :token
|
@@ -20,12 +21,14 @@ RSpec.describe Braque::Model, type: :model do
|
|
20
21
|
let(:root_request) do
|
21
22
|
WebMock.stub_request(:get, "#{Breeze.api_root}/")
|
22
23
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
24
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
23
25
|
.to_return(status: 200, body: root_response)
|
24
26
|
end
|
25
27
|
|
26
28
|
let(:root_request_with_token_resource_path) do
|
27
29
|
WebMock.stub_request(:get, "#{Breeze.api_root}/")
|
28
30
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
31
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
29
32
|
.to_return(status: 200, body: root_response_with_token_resource_path)
|
30
33
|
end
|
31
34
|
|
@@ -48,6 +51,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
48
51
|
root_request
|
49
52
|
@collection_request = WebMock.stub_request(:get, "#{Breeze.api_root}/breezes")
|
50
53
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
54
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
51
55
|
.to_return(status: 200, body: collection_response)
|
52
56
|
@breezes = Breeze.list
|
53
57
|
end
|
@@ -58,16 +62,18 @@ RSpec.describe Braque::Model, type: :model do
|
|
58
62
|
expect(@collection_request).to have_been_requested
|
59
63
|
end
|
60
64
|
it 'returns an array of items' do
|
61
|
-
expect(@breezes.count).to be
|
65
|
+
expect(@breezes.count).to be 2
|
62
66
|
expect(@breezes.first).to be_a_kind_of Breeze
|
67
|
+
expect(@breezes).to be_a_kind_of Array
|
63
68
|
end
|
64
69
|
it 'returns items with the correct class' do
|
65
70
|
expect(@breezes.first).to be_a_kind_of Breeze
|
66
71
|
end
|
67
72
|
it 'returns an array of items with the correct attributes' do
|
68
|
-
|
69
|
-
|
70
|
-
|
73
|
+
@breezes.each_with_index do |breeze, index|
|
74
|
+
expect(breeze.id).to eq collection_response['_embedded']['breezes'][index]['id']
|
75
|
+
expect(breeze.title).to eq collection_response['_embedded']['breezes'][index]['title']
|
76
|
+
end
|
71
77
|
end
|
72
78
|
end
|
73
79
|
context 'with an errored response' do
|
@@ -75,6 +81,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
75
81
|
root_request
|
76
82
|
@collection_request = WebMock.stub_request(:get, "#{Breeze.api_root}/breezes")
|
77
83
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
84
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
78
85
|
.to_return(status: 500)
|
79
86
|
end
|
80
87
|
it 'returns a Faraday::ClientError error' do
|
@@ -90,6 +97,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
90
97
|
root_request
|
91
98
|
@collection_request = WebMock.stub_request(:get, "#{Breeze.api_root}/breezes")
|
92
99
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
100
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
93
101
|
.with(query: 'ids%5B0%5D=1&ids%5B1%5D=2')
|
94
102
|
.to_return(status: 200, body: collection_response)
|
95
103
|
@breezes = Breeze.list('ids[]' => [1, 2])
|
@@ -106,6 +114,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
106
114
|
root_request
|
107
115
|
@resource_request = WebMock.stub_request(:get, "#{Breeze.api_root}/breezes/1")
|
108
116
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
117
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
109
118
|
.to_return(status: 200, body: resource_response)
|
110
119
|
@breeze = Breeze.find(id: 1)
|
111
120
|
end
|
@@ -128,6 +137,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
128
137
|
root_request
|
129
138
|
@resource_request = WebMock.stub_request(:get, "#{Breeze.api_root}/breezes/1")
|
130
139
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
140
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
131
141
|
.to_return(status: 500)
|
132
142
|
end
|
133
143
|
it 'returns a Faraday::ClientError error' do
|
@@ -149,6 +159,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
149
159
|
root_request_with_token_resource_path
|
150
160
|
@resource_request = WebMock.stub_request(:get, "#{Breeze.api_root}/breezes/1?token=123")
|
151
161
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
162
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
152
163
|
.to_return(status: 200, body: resource_response)
|
153
164
|
@breeze = Breeze.find(id: 1, token: 123)
|
154
165
|
end
|
@@ -175,6 +186,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
175
186
|
root_request
|
176
187
|
@create_request = WebMock.stub_request(:post, "#{Breeze.api_root}/breezes")
|
177
188
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
189
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
178
190
|
.to_return(status: 201, body: resource_response)
|
179
191
|
@params = { title: 'What a nice breeze.' }
|
180
192
|
@breeze = Breeze.create(@params)
|
@@ -199,6 +211,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
199
211
|
root_request
|
200
212
|
@create_request = WebMock.stub_request(:post, "#{Breeze.api_root}/breezes")
|
201
213
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
214
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
202
215
|
.to_return(status: 500)
|
203
216
|
end
|
204
217
|
it 'returns a Faraday::ClientError error' do
|
@@ -221,6 +234,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
221
234
|
root_request
|
222
235
|
@save_request = WebMock.stub_request(:patch, "#{Breeze.api_root}/breezes/1")
|
223
236
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
237
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
224
238
|
.to_return(status: 200, body: resource_response)
|
225
239
|
@params = { title: 'What a nice breeze.' }
|
226
240
|
@breeze = Breeze.new(id: 1).save(@params)
|
@@ -245,6 +259,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
245
259
|
root_request
|
246
260
|
@save_request = WebMock.stub_request(:patch, "#{Breeze.api_root}/breezes/1")
|
247
261
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
262
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
248
263
|
.to_return(status: 500)
|
249
264
|
end
|
250
265
|
it 'returns a Faraday::ClientError error' do
|
@@ -267,6 +282,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
267
282
|
root_request_with_token_resource_path
|
268
283
|
@save_request = WebMock.stub_request(:patch, "#{Breeze.api_root}/breezes/1?token=123")
|
269
284
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
285
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
270
286
|
.to_return(status: 200, body: resource_response)
|
271
287
|
@params = { title: 'What a nice breeze.' }
|
272
288
|
@breeze = Breeze.new(id: 1, token: 123).save(@params)
|
@@ -297,6 +313,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
297
313
|
root_request
|
298
314
|
@destroy_request = WebMock.stub_request(:delete, "#{Breeze.api_root}/breezes/1")
|
299
315
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
316
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
300
317
|
.to_return(status: 200)
|
301
318
|
@breeze = Breeze.new(id: 1).destroy
|
302
319
|
end
|
@@ -313,6 +330,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
313
330
|
root_request
|
314
331
|
@destroy_request = WebMock.stub_request(:delete, "#{Breeze.api_root}/breezes/1")
|
315
332
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
333
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
316
334
|
.to_return(status: 500)
|
317
335
|
end
|
318
336
|
it 'returns a Faraday::ClientError error' do
|
@@ -335,6 +353,7 @@ RSpec.describe Braque::Model, type: :model do
|
|
335
353
|
root_request_with_token_resource_path
|
336
354
|
@destroy_request = WebMock.stub_request(:delete, "#{Breeze.api_root}/breezes/1?token=123")
|
337
355
|
.with(headers: { 'Http-Authorization' => Breeze.api_token })
|
356
|
+
.with(headers: { 'Accept' => Breeze.accept_header })
|
338
357
|
.to_return(status: 200)
|
339
358
|
@breeze = Breeze.new(id: 1, token: 123).destroy
|
340
359
|
end
|
@@ -10,9 +10,19 @@
|
|
10
10
|
"_embedded":{
|
11
11
|
"breezes":[
|
12
12
|
{
|
13
|
-
"id":
|
13
|
+
"id":0,
|
14
14
|
"token":123,
|
15
15
|
"title":"Southerly Breeze",
|
16
|
+
"_links":{
|
17
|
+
"self":{
|
18
|
+
"href":"http://localhost:9292/breezes/0"
|
19
|
+
}
|
20
|
+
}
|
21
|
+
},
|
22
|
+
{
|
23
|
+
"id":1,
|
24
|
+
"token":234,
|
25
|
+
"title":"Westerly Breeze",
|
16
26
|
"_links":{
|
17
27
|
"self":{
|
18
28
|
"href":"http://localhost:9292/breezes/1"
|
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.1.
|
4
|
+
version: 0.1.4
|
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-
|
11
|
+
date: 2015-09-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hyperclient
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: active_attr
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0
|
33
|
+
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: activesupport
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- lib/braque/helpers/hypermedia_responses_helper.rb
|
69
69
|
- lib/braque/model.rb
|
70
70
|
- lib/braque/version.rb
|
71
|
+
- spec/braque/client_headers_spec.rb
|
71
72
|
- spec/braque/collection_spec.rb
|
72
73
|
- spec/braque/helpers/hypermedia_responses_helper_spec.rb
|
73
74
|
- spec/braque/model_spec.rb
|
@@ -98,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
99
|
version: '0'
|
99
100
|
requirements: []
|
100
101
|
rubyforge_project:
|
101
|
-
rubygems_version: 2.
|
102
|
+
rubygems_version: 2.4.8
|
102
103
|
signing_key:
|
103
104
|
specification_version: 4
|
104
105
|
summary: Braque provides a simple interface for interacting with Hypermedia API services
|