braque 0.2.4 → 0.2.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: b98f70cd6ccb5ad8f86de5af368555a62b72901a
4
- data.tar.gz: b32d4f95f61be9dc742d4f8f5432443a8b3eb8a7
3
+ metadata.gz: d97d923b573d2def2987c0ec50a13a75b8c77e49
4
+ data.tar.gz: 5e513a8caf7f12bdf7e45514c63b55cbe77d17e8
5
5
  SHA512:
6
- metadata.gz: 2db286037fa8f11ce115e0584be19ffac30f6052acb11c331b63edf7fa4a12bab91bc0adabcf634a0cabdc4b8df26f9984b46c465696a5e6ce51a8f5517e35ca
7
- data.tar.gz: f9ef00b9a9b3dc3c0965f5234db4125af637fa4989c53dd7a154c68f1400ff0983f5bb708656a4b6afa4a21ebc62020e8942ece8cc5fbed05841070045959e90
6
+ metadata.gz: e6a40517912ec7286e1fb661fe2c61b74f08771a1783a1ad0aa0ad27e0e1381bfca856b89310abad52befa384263160b45464c6715b35d1ba9d03ab99c7ec4b2
7
+ data.tar.gz: 15fbd1640334032b276b2cd5d2910437a125d8d4400c59f5e86092f18ce607bf4483caaeb7471672a0b604fc3bd2f14ad021d9f616262dc259c8fea4548b49f7
@@ -7,6 +7,13 @@ module Braque
7
7
  class_attribute :config
8
8
  self.config = {}
9
9
 
10
+ # NOTE: Initialize with the Hyperclient resource's _attributes if present.
11
+ # Fallback to permit assigning attributes via a simple hash.
12
+ def initialize(response = nil, options = {})
13
+ attributes = response.respond_to?(:_attributes) ? response._attributes : response
14
+ super(attributes, options)
15
+ end
16
+
10
17
  # NOTE: This assumes that the related API uses an ID attribute to
11
18
  # define the resource. Rails will use this field to construct
12
19
  # resource routes in client applications.
@@ -15,7 +22,7 @@ module Braque
15
22
  # in client apps if some other scheme is more advisable.
16
23
  #
17
24
  def to_param
18
- fail 'Please overide to_param or add ID to your model attributes.' unless attributes.include? 'id'
25
+ raise 'Please overide to_param or add ID to your model attributes.' unless attributes.include? 'id'
19
26
  id.to_s
20
27
  end
21
28
 
@@ -27,13 +34,13 @@ module Braque
27
34
  # classes in client apps if some other scheme is more advisable.
28
35
  #
29
36
  def resource_find_options
30
- fail 'Please overide resource_find_options or add ID to your model attributes.' unless attributes.include? 'id'
37
+ raise 'Please overide resource_find_options or add ID to your model attributes.' unless attributes.include? 'id'
31
38
  { id: id }
32
39
  end
33
40
 
34
41
  def save(params = {})
35
42
  response = self.class.client.method(self.class.instance_method_name).call(resource_find_options)
36
- ._patch("#{self.class.instance_method_name}" => params)
43
+ ._patch(self.class.instance_method_name.to_s => params)
37
44
  self.class.new response
38
45
  end
39
46
 
@@ -99,11 +106,11 @@ module Braque
99
106
 
100
107
  def create(resource_params = {}, options = {})
101
108
  response = client.method(collection_method_name)
102
- .call
103
- ._post(
104
- { "#{instance_method_name}" => resource_params }
105
- .merge(options)
106
- )
109
+ .call
110
+ ._post(
111
+ { instance_method_name.to_s => resource_params }
112
+ .merge(options)
113
+ )
107
114
  new response
108
115
  end
109
116
 
@@ -116,7 +123,7 @@ module Braque
116
123
  end
117
124
 
118
125
  def client
119
- fail 'Please define api_root for all Braque::Model classes' unless config[:api_root_url]
126
+ raise 'Please define api_root for all Braque::Model classes' unless config[:api_root_url]
120
127
  Hyperclient.new(config[:api_root_url]) do |client|
121
128
  client.headers['Http-Authorization'] = config[:http_authorization_header] if config[:http_authorization_header]
122
129
  client.headers['Authorization'] = config[:authorization_header] if config[:authorization_header]
@@ -14,8 +14,8 @@ module Braque
14
14
  def has_many(relation)
15
15
  define_method relation do |params = {}|
16
16
  response = self.class.client.method(self.class.instance_method_name)
17
- .call(resource_find_options)
18
- .method(relation).call(params)
17
+ .call(resource_find_options)
18
+ .method(relation).call(params)
19
19
  Braque::Collection::LinkedArray.new(
20
20
  response,
21
21
  relation.to_s.classify.singularize.constantize
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Braque
2
- VERSION = '0.2.4'
3
+ VERSION = '0.2.5'.freeze
3
4
  end
@@ -12,17 +12,17 @@ RSpec.describe Braque::Model, type: :model do
12
12
  end
13
13
  end
14
14
 
15
- let(:root_response) { JSON.parse(File.read 'spec/fixtures/root.json') }
16
- let(:breeze_response) { JSON.parse(File.read 'spec/fixtures/resource.json') }
15
+ let(:root_response) { JSON.parse(File.read('spec/fixtures/root.json')) }
16
+ let(:breeze_response) { JSON.parse(File.read('spec/fixtures/resource.json')) }
17
17
 
18
18
  let(:root_request) do
19
19
  WebMock.stub_request(:get, "#{OceanBreeze.config[:api_root_url]}/")
20
- .to_return(status: 200, body: root_response)
20
+ .to_return(status: 200, body: root_response)
21
21
  end
22
22
 
23
23
  let(:breeze_request) do
24
24
  WebMock.stub_request(:get, "#{OceanBreeze.config[:api_root_url]}/breezes/1")
25
- .to_return(status: 200, body: breeze_response)
25
+ .to_return(status: 200, body: breeze_response)
26
26
  end
27
27
 
28
28
  let(:ocean_breeze) { OceanBreeze.new id: 1 }
@@ -14,23 +14,25 @@ RSpec.describe Braque::Model, type: :model do
14
14
  end
15
15
  end
16
16
 
17
- let(:root_response) { JSON.parse(File.read 'spec/fixtures/root.json') }
18
- let(:root_response_with_token_resource_path) { JSON.parse(File.read 'spec/fixtures/root_with_token_resource_path.json') }
19
- let(:collection_response) { JSON.parse(File.read 'spec/fixtures/collection.json') }
20
- let(:resource_response) { JSON.parse(File.read 'spec/fixtures/resource.json') }
17
+ let(:root_response) { JSON.parse(File.read('spec/fixtures/root.json')) }
18
+ let(:root_response_with_token_resource_path) do
19
+ JSON.parse(File.read('spec/fixtures/root_with_token_resource_path.json'))
20
+ end
21
+ let(:collection_response) { JSON.parse(File.read('spec/fixtures/collection.json')) }
22
+ let(:resource_response) { JSON.parse(File.read('spec/fixtures/resource.json')) }
21
23
 
22
24
  let(:root_request) do
23
25
  WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/")
24
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
25
- 'Accept' => Breeze.config[:accept_header] })
26
- .to_return(status: 200, body: root_response)
26
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
27
+ 'Accept' => Breeze.config[:accept_header] })
28
+ .to_return(status: 200, body: root_response)
27
29
  end
28
30
 
29
31
  let(:root_request_with_token_resource_path) do
30
32
  WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/")
31
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
32
- 'Accept' => Breeze.config[:accept_header] })
33
- .to_return(status: 200, body: root_response_with_token_resource_path)
33
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
34
+ 'Accept' => Breeze.config[:accept_header] })
35
+ .to_return(status: 200, body: root_response_with_token_resource_path)
34
36
  end
35
37
 
36
38
  context 'class methods' do
@@ -51,9 +53,9 @@ RSpec.describe Braque::Model, type: :model do
51
53
  before do
52
54
  root_request
53
55
  @collection_request = WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/breezes")
54
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
55
- 'Accept' => Breeze.config[:accept_header] })
56
- .to_return(status: 200, body: collection_response)
56
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
57
+ 'Accept' => Breeze.config[:accept_header] })
58
+ .to_return(status: 200, body: collection_response)
57
59
  @breezes = Breeze.list
58
60
  end
59
61
  it 'performs the API root request' do
@@ -84,9 +86,9 @@ RSpec.describe Braque::Model, type: :model do
84
86
  before do
85
87
  root_request
86
88
  @collection_request = WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/breezes")
87
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
88
- 'Accept' => Breeze.config[:accept_header] })
89
- .to_return(status: 500)
89
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
90
+ 'Accept' => Breeze.config[:accept_header] })
91
+ .to_return(status: 500)
90
92
  end
91
93
  it 'returns a Faraday::ClientError error' do
92
94
  expect do
@@ -100,10 +102,10 @@ RSpec.describe Braque::Model, type: :model do
100
102
  before do
101
103
  root_request
102
104
  @collection_request = WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/breezes")
103
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
104
- 'Accept' => Breeze.config[:accept_header] })
105
- .with(query: 'ids%5B0%5D=1&ids%5B1%5D=2')
106
- .to_return(status: 200, body: collection_response)
105
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
106
+ 'Accept' => Breeze.config[:accept_header] })
107
+ .with(query: 'ids%5B0%5D=1&ids%5B1%5D=2')
108
+ .to_return(status: 200, body: collection_response)
107
109
  @breezes = Breeze.list('ids[]' => [1, 2])
108
110
  end
109
111
  it 'performs the collection request with converted array params' do
@@ -117,9 +119,9 @@ RSpec.describe Braque::Model, type: :model do
117
119
  before do
118
120
  root_request
119
121
  @resource_request = WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/breezes/1")
120
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
121
- 'Accept' => Breeze.config[:accept_header] })
122
- .to_return(status: 200, body: resource_response)
122
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
123
+ 'Accept' => Breeze.config[:accept_header] })
124
+ .to_return(status: 200, body: resource_response)
123
125
  @breeze = Breeze.find(id: 1)
124
126
  end
125
127
  it 'performs the API root request' do
@@ -140,9 +142,9 @@ RSpec.describe Braque::Model, type: :model do
140
142
  before do
141
143
  root_request
142
144
  @resource_request = WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/breezes/1")
143
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
144
- 'Accept' => Breeze.config[:accept_header] })
145
- .to_return(status: 500)
145
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
146
+ 'Accept' => Breeze.config[:accept_header] })
147
+ .to_return(status: 500)
146
148
  end
147
149
  it 'returns a Faraday::ClientError error' do
148
150
  expect do
@@ -163,9 +165,9 @@ RSpec.describe Braque::Model, type: :model do
163
165
 
164
166
  root_request_with_token_resource_path
165
167
  @resource_request = WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/breezes/1?token=123")
166
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
167
- 'Accept' => Breeze.config[:accept_header] })
168
- .to_return(status: 200, body: resource_response)
168
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
169
+ 'Accept' => Breeze.config[:accept_header] })
170
+ .to_return(status: 200, body: resource_response)
169
171
  @breeze = Breeze.find(id: 1, token: 123)
170
172
  end
171
173
 
@@ -191,9 +193,9 @@ RSpec.describe Braque::Model, type: :model do
191
193
  before do
192
194
  root_request
193
195
  @create_request = WebMock.stub_request(:post, "#{Breeze.config[:api_root_url]}/breezes")
194
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
195
- 'Accept' => Breeze.config[:accept_header] })
196
- .to_return(status: 201, body: resource_response)
196
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
197
+ 'Accept' => Breeze.config[:accept_header] })
198
+ .to_return(status: 201, body: resource_response)
197
199
  @params = { title: 'What a nice breeze.' }
198
200
  @breeze = Breeze.create(@params)
199
201
  end
@@ -217,12 +219,12 @@ RSpec.describe Braque::Model, type: :model do
217
219
  @params = { title: 'What a nice breeze.' }
218
220
  @subdomain = 'breezey'
219
221
  @create_request = WebMock.stub_request(:post, "#{Breeze.config[:api_root_url]}/breezes")
220
- .with(headers: {
221
- 'Http-Authorization' => Breeze.config[:http_authorization_header],
222
- 'Accept' => Breeze.config[:accept_header]
223
- })
224
- .with(body: '{"breeze":{"title":"What a nice breeze."},"subdomain":"breezey"}')
225
- .to_return(status: 201, body: resource_response)
222
+ .with(headers: {
223
+ 'Http-Authorization' => Breeze.config[:http_authorization_header],
224
+ 'Accept' => Breeze.config[:accept_header]
225
+ })
226
+ .with(body: '{"breeze":{"title":"What a nice breeze."},"subdomain":"breezey"}')
227
+ .to_return(status: 201, body: resource_response)
226
228
  @breeze = Breeze.create(@params, subdomain: @subdomain)
227
229
  end
228
230
  it 'performs the API root request' do
@@ -245,10 +247,10 @@ RSpec.describe Braque::Model, type: :model do
245
247
  before do
246
248
  root_request
247
249
  @create_request = WebMock.stub_request(:post, "#{Breeze.config[:api_root_url]}/breezes")
248
- .with(headers: {
249
- 'Http-Authorization' => Breeze.config[:http_authorization_header],
250
- 'Accept' => Breeze.config[:accept_header]
251
- }).to_return(status: 500)
250
+ .with(headers: {
251
+ 'Http-Authorization' => Breeze.config[:http_authorization_header],
252
+ 'Accept' => Breeze.config[:accept_header]
253
+ }).to_return(status: 500)
252
254
  end
253
255
  it 'returns a Faraday::ClientError error' do
254
256
  expect do
@@ -269,9 +271,9 @@ RSpec.describe Braque::Model, type: :model do
269
271
  before do
270
272
  root_request
271
273
  @save_request = WebMock.stub_request(:patch, "#{Breeze.config[:api_root_url]}/breezes/1")
272
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
273
- 'Accept' => Breeze.config[:accept_header] })
274
- .to_return(status: 200, body: resource_response)
274
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
275
+ 'Accept' => Breeze.config[:accept_header] })
276
+ .to_return(status: 200, body: resource_response)
275
277
  @params = { title: 'What a nice breeze.' }
276
278
  @breeze = Breeze.new(id: 1).save(@params)
277
279
  end
@@ -294,9 +296,9 @@ RSpec.describe Braque::Model, type: :model do
294
296
  before do
295
297
  root_request
296
298
  @save_request = WebMock.stub_request(:patch, "#{Breeze.config[:api_root_url]}/breezes/1")
297
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
298
- 'Accept' => Breeze.config[:accept_header] })
299
- .to_return(status: 500)
299
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
300
+ 'Accept' => Breeze.config[:accept_header] })
301
+ .to_return(status: 500)
300
302
  end
301
303
  it 'returns a Faraday::ClientError error' do
302
304
  expect do
@@ -317,9 +319,9 @@ RSpec.describe Braque::Model, type: :model do
317
319
 
318
320
  root_request_with_token_resource_path
319
321
  @save_request = WebMock.stub_request(:patch, "#{Breeze.config[:api_root_url]}/breezes/1?token=123")
320
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
321
- 'Accept' => Breeze.config[:accept_header] })
322
- .to_return(status: 200, body: resource_response)
322
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
323
+ 'Accept' => Breeze.config[:accept_header] })
324
+ .to_return(status: 200, body: resource_response)
323
325
  @params = { title: 'What a nice breeze.' }
324
326
  @breeze = Breeze.new(id: 1, token: 123).save(@params)
325
327
  end
@@ -348,9 +350,9 @@ RSpec.describe Braque::Model, type: :model do
348
350
  before do
349
351
  root_request
350
352
  @destroy_request = WebMock.stub_request(:delete, "#{Breeze.config[:api_root_url]}/breezes/1")
351
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
352
- 'Accept' => Breeze.config[:accept_header] })
353
- .to_return(status: 200)
353
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
354
+ 'Accept' => Breeze.config[:accept_header] })
355
+ .to_return(status: 200)
354
356
  @breeze = Breeze.new(id: 1).destroy
355
357
  end
356
358
  it 'performs the API root request' do
@@ -365,9 +367,9 @@ RSpec.describe Braque::Model, type: :model do
365
367
  before do
366
368
  root_request
367
369
  @destroy_request = WebMock.stub_request(:delete, "#{Breeze.config[:api_root_url]}/breezes/1")
368
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
369
- 'Accept' => Breeze.config[:accept_header] })
370
- .to_return(status: 500)
370
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
371
+ 'Accept' => Breeze.config[:accept_header] })
372
+ .to_return(status: 500)
371
373
  end
372
374
  it 'returns a Faraday::ClientError error' do
373
375
  expect do
@@ -388,9 +390,9 @@ RSpec.describe Braque::Model, type: :model do
388
390
 
389
391
  root_request_with_token_resource_path
390
392
  @destroy_request = WebMock.stub_request(:delete, "#{Breeze.config[:api_root_url]}/breezes/1?token=123")
391
- .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
392
- 'Accept' => Breeze.config[:accept_header] })
393
- .to_return(status: 200)
393
+ .with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header],
394
+ 'Accept' => Breeze.config[:accept_header] })
395
+ .to_return(status: 200)
394
396
  @breeze = Breeze.new(id: 1, token: 123).destroy
395
397
  end
396
398
  it 'performs the API root request' do
@@ -20,29 +20,29 @@ RSpec.describe Braque::Relations, type: :model do
20
20
  end
21
21
  end
22
22
 
23
- let(:root_response) { JSON.parse(File.read 'spec/fixtures/root.json') }
24
- let(:breeze_response) { JSON.parse(File.read 'spec/fixtures/resource.json') }
25
- let(:tide_response) { JSON.parse(File.read 'spec/fixtures/associated_resource.json') }
26
- let(:tides_response) { JSON.parse(File.read 'spec/fixtures/associated_collection.json') }
23
+ let(:root_response) { JSON.parse(File.read('spec/fixtures/root.json')) }
24
+ let(:breeze_response) { JSON.parse(File.read('spec/fixtures/resource.json')) }
25
+ let(:tide_response) { JSON.parse(File.read('spec/fixtures/associated_resource.json')) }
26
+ let(:tides_response) { JSON.parse(File.read('spec/fixtures/associated_collection.json')) }
27
27
 
28
28
  let(:root_request) do
29
29
  WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/")
30
- .to_return(status: 200, body: root_response)
30
+ .to_return(status: 200, body: root_response)
31
31
  end
32
32
 
33
33
  let(:breeze_request) do
34
34
  WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/breezes/1")
35
- .to_return(status: 200, body: breeze_response)
35
+ .to_return(status: 200, body: breeze_response)
36
36
  end
37
37
 
38
38
  let(:tide_request) do
39
39
  WebMock.stub_request(:get, "#{Tide.config[:api_root_url]}/tides/1")
40
- .to_return(status: 200, body: tide_response)
40
+ .to_return(status: 200, body: tide_response)
41
41
  end
42
42
 
43
43
  let(:tides_request) do
44
44
  WebMock.stub_request(:get, "#{Tide.config[:api_root_url]}/tides?account_id=1")
45
- .to_return(status: 200, body: tides_response)
45
+ .to_return(status: 200, body: tides_response)
46
46
  end
47
47
 
48
48
  let(:breeze) { Breeze.new id: 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.2.4
4
+ version: 0.2.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: 2016-01-24 00:00:00.000000000 Z
11
+ date: 2016-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hyperclient