braque 0.1.5 → 0.1.7
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 +5 -5
- data/lib/braque/helpers/hypermedia_responses_helper.rb +8 -8
- data/lib/braque/model.rb +19 -11
- data/lib/braque/version.rb +1 -1
- data/spec/braque/api_root_spec.rb +26 -0
- data/spec/braque/client_headers_spec.rb +8 -8
- data/spec/braque/helpers/hypermedia_responses_helper_spec.rb +19 -18
- data/spec/braque/model_attributes_spec.rb +100 -0
- data/spec/braque/model_spec.rb +56 -52
- data/spec/braque/multiple_model_spec.rb +8 -8
- data/spec/braque/subclassed_model_spec.rb +14 -14
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 117d19be143c1b7314093816acc359f136666c06
|
4
|
+
data.tar.gz: abb1836ab553182c46553ea2578fb530c5b018be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54f0b02909ea21b7345b5718ba90738c24a7a613a6d77b346b606f5eb87e81218972a1e64f869429ae176a906c6c5bf2fe1eaaef7be1087fc030583c11f51534
|
7
|
+
data.tar.gz: b6959ce75f02e30fb785896ebb8fd43c70dac27eb0a8b614f3d8edebe8fa74227d248e23688b14dad4939135dcba5c2d8dc718dfc70a8d06a6f6a94fe347ae76
|
data/README.md
CHANGED
@@ -13,7 +13,7 @@ Braque is an early-stage and exploratory project. That said, at [Artsy](https://
|
|
13
13
|
```ruby
|
14
14
|
class Article
|
15
15
|
include Braque::Model
|
16
|
-
|
16
|
+
api_root_url Rails.application.config_for(:articles_service)['url']
|
17
17
|
|
18
18
|
attribute :id
|
19
19
|
attribute :title
|
@@ -33,8 +33,8 @@ For example, `http_authorization_header` credentials defined here are added to t
|
|
33
33
|
```ruby
|
34
34
|
class Article
|
35
35
|
include Braque::Model
|
36
|
-
|
37
|
-
|
36
|
+
api_root_url Rails.application.config_for(:articles_service)['url']
|
37
|
+
http_authorization_header Rails.application.config_for(:articles_service)['token']
|
38
38
|
|
39
39
|
attribute :id
|
40
40
|
attribute :title
|
@@ -46,8 +46,8 @@ Defining an `accept_header` credential will replace Hyperclient's default `Accep
|
|
46
46
|
```ruby
|
47
47
|
class Article
|
48
48
|
include Braque::Model
|
49
|
-
|
50
|
-
|
49
|
+
api_root_url Rails.application.config_for(:articles_service)['url']
|
50
|
+
accept_header Rails.application.config_for(:articles_service)['accept_header']
|
51
51
|
|
52
52
|
attribute :id
|
53
53
|
attribute :title
|
@@ -6,9 +6,9 @@ module Braque
|
|
6
6
|
resource_path_options = options[:resource_path] || '{id}'
|
7
7
|
root_hash = {}
|
8
8
|
root_hash[:_links] = {}
|
9
|
-
root_hash[:_links][:self] = { href: hyperclient_model_class.
|
10
|
-
root_hash[:_links][hyperclient_model_class.collection_method_name] = { href: "#{hyperclient_model_class.
|
11
|
-
root_hash[:_links][hyperclient_model_class.instance_method_name] = { href: "#{hyperclient_model_class.
|
9
|
+
root_hash[:_links][:self] = { href: hyperclient_model_class.config[:api_root_url] }
|
10
|
+
root_hash[:_links][hyperclient_model_class.collection_method_name] = { href: "#{hyperclient_model_class.config[:api_root_url]}/#{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.config[:api_root_url]}/#{hyperclient_model_class.collection_method_name}/#{resource_path_options}", templated: true }
|
12
12
|
JSON.parse root_hash.to_json
|
13
13
|
end
|
14
14
|
|
@@ -19,9 +19,9 @@ 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: "#{hyperclient_model.class.
|
23
|
-
collection_hash[:_links][:next] = { href: "#{hyperclient_model.class.
|
24
|
-
collection_hash[:_links][:prev] = { href: "#{hyperclient_model.class.
|
22
|
+
collection_hash[:_links][:self] = { href: "#{hyperclient_model.class.config[:api_root_url]}/#{hyperclient_model.class.collection_method_name}" }
|
23
|
+
collection_hash[:_links][:next] = { href: "#{hyperclient_model.class.config[:api_root_url]}/#{hyperclient_model.class.collection_method_name}" }
|
24
|
+
collection_hash[:_links][:prev] = { href: "#{hyperclient_model.class.config[:api_root_url]}/#{hyperclient_model.class.collection_method_name}" }
|
25
25
|
collection_hash[:_embedded] = {}
|
26
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
|
@@ -44,11 +44,11 @@ module Braque
|
|
44
44
|
resource_path_str = eval(options[:resource_path].to_s) if options[:resource_path]
|
45
45
|
resource_path = resource_path_str || model.id
|
46
46
|
hyperclient_element = {}
|
47
|
-
model.attributes.
|
47
|
+
model.attributes.each_key do |key|
|
48
48
|
hyperclient_element[key] = model.send(key)
|
49
49
|
end
|
50
50
|
hyperclient_element[:_links] = {}
|
51
|
-
hyperclient_element[:_links][:self] = { href: "#{model.class.
|
51
|
+
hyperclient_element[:_links][:self] = { href: "#{model.class.config[:api_root_url]}/#{model.class.collection_method_name}/#{resource_path}" }
|
52
52
|
hyperclient_element
|
53
53
|
end
|
54
54
|
end
|
data/lib/braque/model.rb
CHANGED
@@ -4,12 +4,8 @@ module Braque
|
|
4
4
|
include ::ActiveAttr::Model
|
5
5
|
|
6
6
|
included do
|
7
|
-
|
8
|
-
|
9
|
-
#
|
10
|
-
[:api_root, :accept_header, :authorization_header, :http_authorization_header].each do |sym|
|
11
|
-
class_attribute sym
|
12
|
-
end
|
7
|
+
class_attribute :config
|
8
|
+
self.config = {}
|
13
9
|
|
14
10
|
# NOTE: This assumes that the related API uses an ID attribute to
|
15
11
|
# define the resource. Rails will use this field to construct
|
@@ -19,6 +15,7 @@ module Braque
|
|
19
15
|
# in client apps if some other scheme is more advisable.
|
20
16
|
#
|
21
17
|
def to_param
|
18
|
+
fail 'Please overide to_param or add id to your model attributes.' unless attributes.include? 'id'
|
22
19
|
id.to_s
|
23
20
|
end
|
24
21
|
|
@@ -30,6 +27,7 @@ module Braque
|
|
30
27
|
# classes in client apps if some other scheme is more advisable.
|
31
28
|
#
|
32
29
|
def resource_find_options
|
30
|
+
fail 'Please overide resource_find_options or add id to your model attributes.' unless attributes.include? 'id'
|
33
31
|
{ id: id }
|
34
32
|
end
|
35
33
|
|
@@ -52,6 +50,8 @@ module Braque
|
|
52
50
|
|
53
51
|
def self.inherited(subclass)
|
54
52
|
subclass.class_eval do
|
53
|
+
self.config = self.config.dup
|
54
|
+
|
55
55
|
define_singleton_method subclass.instance_method_name do
|
56
56
|
end
|
57
57
|
|
@@ -65,6 +65,12 @@ module Braque
|
|
65
65
|
module ClassMethods
|
66
66
|
include Braque::Collection
|
67
67
|
|
68
|
+
[:api_root_url, :accept_header, :authorization_header, :http_authorization_header].each do |config_option|
|
69
|
+
define_method config_option do |value|
|
70
|
+
config[config_option] = value
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
68
74
|
def list(options = {})
|
69
75
|
options = Hash[options.map { |k, v| [CGI.escape(k.to_s), v] }]
|
70
76
|
response = client.method(collection_method_name).call(options)
|
@@ -77,7 +83,8 @@ module Braque
|
|
77
83
|
end
|
78
84
|
|
79
85
|
def create(params = {})
|
80
|
-
response = client.method(collection_method_name).call
|
86
|
+
response = client.method(collection_method_name).call
|
87
|
+
._post("#{instance_method_name}" => params)
|
81
88
|
new response
|
82
89
|
end
|
83
90
|
|
@@ -90,10 +97,11 @@ module Braque
|
|
90
97
|
end
|
91
98
|
|
92
99
|
def client
|
93
|
-
|
94
|
-
|
95
|
-
client.headers['Authorization'] =
|
96
|
-
client.headers['
|
100
|
+
fail 'Please define api_root for all Braque::Model classes' unless config[:api_root_url]
|
101
|
+
Hyperclient.new(config[:api_root_url]) do |client|
|
102
|
+
client.headers['Http-Authorization'] = config[:http_authorization_header] if config[:http_authorization_header]
|
103
|
+
client.headers['Authorization'] = config[:authorization_header] if config[:authorization_header]
|
104
|
+
client.headers['Accept'] = config[:accept_header] if config[:accept_header]
|
97
105
|
end
|
98
106
|
end
|
99
107
|
end
|
data/lib/braque/version.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'api_root' do
|
4
|
+
context 'with a Braque::Model class that does not define api_root' do
|
5
|
+
class Rodinia
|
6
|
+
include ::Braque::Model
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'raises an error' do
|
10
|
+
expect { Rodinia.client }.to raise_error(
|
11
|
+
RuntimeError, 'Please define api_root for all Braque::Model classes'
|
12
|
+
)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'with a Braque::Model class that defines api_root' do
|
17
|
+
class Magma
|
18
|
+
include ::Braque::Model
|
19
|
+
api_root_url 'http://localhost:9292'
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'does not raise an error' do
|
23
|
+
expect { Magma.client }.to_not raise_error
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -10,15 +10,15 @@ RSpec.describe 'Hyperclient header configuration' do
|
|
10
10
|
context 'with a Braque::Model class that defines an api token' do
|
11
11
|
class Mantle
|
12
12
|
include ::Braque::Model
|
13
|
-
|
14
|
-
|
13
|
+
api_root_url 'http://localhost:9292'
|
14
|
+
http_authorization_header 'replace-me'
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'passes http_authorization_header via Http-Authorization headers with requests' do
|
18
18
|
Mantle.client._get
|
19
19
|
expect(
|
20
20
|
root_request
|
21
|
-
.with(headers: { 'Http-Authorization' => Mantle.http_authorization_header })
|
21
|
+
.with(headers: { 'Http-Authorization' => Mantle.config[:http_authorization_header] })
|
22
22
|
).to have_been_requested
|
23
23
|
end
|
24
24
|
|
@@ -34,8 +34,8 @@ RSpec.describe 'Hyperclient header configuration' do
|
|
34
34
|
context 'with a Braque::Model class that defines an accept_header' do
|
35
35
|
class Subduction
|
36
36
|
include ::Braque::Model
|
37
|
-
|
38
|
-
|
37
|
+
api_root_url 'http://localhost:9292'
|
38
|
+
accept_header 'application/vnd.earth-v1+json'
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'includes class defined accept_header with request headers' do
|
@@ -50,15 +50,15 @@ RSpec.describe 'Hyperclient header configuration' do
|
|
50
50
|
context 'with a Braque::Model class that defines an authorization header' do
|
51
51
|
class Lithoshpere
|
52
52
|
include ::Braque::Model
|
53
|
-
|
54
|
-
|
53
|
+
api_root_url 'http://localhost:9292'
|
54
|
+
authorization_header 'Farallon Plate'
|
55
55
|
end
|
56
56
|
|
57
57
|
it 'includes correct Authorization header with request headers' do
|
58
58
|
Lithoshpere.client._get
|
59
59
|
expect(
|
60
60
|
root_request
|
61
|
-
.with(headers: { 'Authorization' => Lithoshpere.authorization_header })
|
61
|
+
.with(headers: { 'Authorization' => Lithoshpere.config[:authorization_header] })
|
62
62
|
).to have_been_requested
|
63
63
|
end
|
64
64
|
end
|
@@ -10,8 +10,8 @@ RSpec.describe Braque::Helpers::HypermediaResponsesHelper do
|
|
10
10
|
before(:all) do
|
11
11
|
class Article
|
12
12
|
include ::Braque::Model
|
13
|
-
|
14
|
-
|
13
|
+
api_root_url 'http://localhost:9292'
|
14
|
+
http_authorization_header 'replace-me'
|
15
15
|
attribute :id
|
16
16
|
attribute :title
|
17
17
|
attribute :key
|
@@ -25,15 +25,15 @@ RSpec.describe Braque::Helpers::HypermediaResponsesHelper do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'returns the self link' do
|
28
|
-
expect(@root_json['_links']['self']['href']).to eq Article.
|
28
|
+
expect(@root_json['_links']['self']['href']).to eq Article.config[:api_root_url]
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'returns the collection link' do
|
32
|
-
expect(@root_json['_links']['articles']['href']).to eq "#{Article.
|
32
|
+
expect(@root_json['_links']['articles']['href']).to eq "#{Article.config[:api_root_url]}/#{Article.collection_method_name}{?page,size}"
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'returns the resource link' do
|
36
|
-
expect(@root_json['_links']['article']['href']).to eq "#{Article.
|
36
|
+
expect(@root_json['_links']['article']['href']).to eq "#{Article.config[:api_root_url]}/#{Article.collection_method_name}/{id}"
|
37
37
|
end
|
38
38
|
end
|
39
39
|
context 'options' do
|
@@ -42,15 +42,15 @@ RSpec.describe Braque::Helpers::HypermediaResponsesHelper do
|
|
42
42
|
end
|
43
43
|
|
44
44
|
it 'returns the self link' do
|
45
|
-
expect(@root_json['_links']['self']['href']).to eq Article.
|
45
|
+
expect(@root_json['_links']['self']['href']).to eq Article.config[:api_root_url]
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'returns the collection link using the collection_query option' do
|
49
|
-
expect(@root_json['_links']['articles']['href']).to eq "#{Article.
|
49
|
+
expect(@root_json['_links']['articles']['href']).to eq "#{Article.config[:api_root_url]}/#{Article.collection_method_name}{?page}"
|
50
50
|
end
|
51
51
|
|
52
52
|
it 'returns the resource link using the resource_path option' do
|
53
|
-
expect(@root_json['_links']['article']['href']).to eq "#{Article.
|
53
|
+
expect(@root_json['_links']['article']['href']).to eq "#{Article.config[:api_root_url]}/#{Article.collection_method_name}/{id}?key={key}"
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
@@ -62,13 +62,13 @@ RSpec.describe Braque::Helpers::HypermediaResponsesHelper do
|
|
62
62
|
end
|
63
63
|
|
64
64
|
it 'returns the _links node' do
|
65
|
-
expect(@collection_json['_links']['self']['href']).to eq "#{Article.
|
66
|
-
expect(@collection_json['_links']['next']['href']).to eq "#{Article.
|
67
|
-
expect(@collection_json['_links']['prev']['href']).to eq "#{Article.
|
65
|
+
expect(@collection_json['_links']['self']['href']).to eq "#{Article.config[:api_root_url]}/#{Article.collection_method_name}"
|
66
|
+
expect(@collection_json['_links']['next']['href']).to eq "#{Article.config[:api_root_url]}/#{Article.collection_method_name}"
|
67
|
+
expect(@collection_json['_links']['prev']['href']).to eq "#{Article.config[:api_root_url]}/#{Article.collection_method_name}"
|
68
68
|
end
|
69
69
|
|
70
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.
|
71
|
+
expect(@collection_json['_embedded']['articles']).to eq [{ 'id' => article.id, 'title' => article.title, 'key' => article.key, '_links' => { 'self' => { 'href' => "#{Article.config[:api_root_url]}/#{Article.collection_method_name}/#{article.id}" } } }]
|
72
72
|
end
|
73
73
|
|
74
74
|
it 'returns the current_page' do
|
@@ -89,13 +89,14 @@ RSpec.describe Braque::Helpers::HypermediaResponsesHelper do
|
|
89
89
|
end
|
90
90
|
|
91
91
|
it 'returns the _links node' do
|
92
|
-
expect(@collection_json['_links']['self']['href']).to eq "#{Article.
|
93
|
-
expect(@collection_json['_links']['next']['href']).to eq "#{Article.
|
94
|
-
expect(@collection_json['_links']['prev']['href']).to eq "#{Article.
|
92
|
+
expect(@collection_json['_links']['self']['href']).to eq "#{Article.config[:api_root_url]}/#{Article.collection_method_name}"
|
93
|
+
expect(@collection_json['_links']['next']['href']).to eq "#{Article.config[:api_root_url]}/#{Article.collection_method_name}"
|
94
|
+
expect(@collection_json['_links']['prev']['href']).to eq "#{Article.config[:api_root_url]}/#{Article.collection_method_name}"
|
95
95
|
end
|
96
96
|
|
97
97
|
it 'returns the _embedded node' do
|
98
|
-
expect(@collection_json['_embedded']['articles'])
|
98
|
+
expect(@collection_json['_embedded']['articles'])
|
99
|
+
.to eq [{ 'id' => article.id, 'title' => article.title, 'key' => article.key, '_links' => { 'self' => { 'href' => "#{Article.config[:api_root_url]}/#{Article.collection_method_name}/#{article.id}?key=#{article.key}" } } }]
|
99
100
|
end
|
100
101
|
|
101
102
|
it 'returns the current_page' do
|
@@ -119,7 +120,7 @@ RSpec.describe Braque::Helpers::HypermediaResponsesHelper do
|
|
119
120
|
end
|
120
121
|
|
121
122
|
it 'returns the _links node' do
|
122
|
-
expect(@resource_json['_links']['self']['href']).to eq "#{Article.
|
123
|
+
expect(@resource_json['_links']['self']['href']).to eq "#{Article.config[:api_root_url]}/#{Article.collection_method_name}/#{article.id}"
|
123
124
|
end
|
124
125
|
|
125
126
|
it 'returns the resource attributes' do
|
@@ -133,7 +134,7 @@ RSpec.describe Braque::Helpers::HypermediaResponsesHelper do
|
|
133
134
|
end
|
134
135
|
|
135
136
|
it 'returns the _links node' do
|
136
|
-
expect(@resource_json['_links']['self']['href']).to eq "#{Article.
|
137
|
+
expect(@resource_json['_links']['self']['href']).to eq "#{Article.config[:api_root_url]}/#{Article.collection_method_name}/#{article.id}?key=#{article.key}"
|
137
138
|
end
|
138
139
|
|
139
140
|
it 'returns the resource attributes' do
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'Braque::Model attribute definitions', type: :model do
|
4
|
+
context 'with multiple defined Braque::Model classes' do
|
5
|
+
let(:identifier) { 1234 }
|
6
|
+
class Agate
|
7
|
+
include ::Braque::Model
|
8
|
+
end
|
9
|
+
|
10
|
+
class AbsoluteTime
|
11
|
+
include ::Braque::Model
|
12
|
+
attribute :id
|
13
|
+
end
|
14
|
+
|
15
|
+
class Aftershock
|
16
|
+
include ::Braque::Model
|
17
|
+
attribute :slug
|
18
|
+
|
19
|
+
def to_param
|
20
|
+
slug
|
21
|
+
end
|
22
|
+
|
23
|
+
def resource_find_options
|
24
|
+
{ slug: slug }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Amygdule < AbsoluteTime
|
29
|
+
attribute :slug
|
30
|
+
|
31
|
+
def to_param
|
32
|
+
slug
|
33
|
+
end
|
34
|
+
|
35
|
+
def resource_find_options
|
36
|
+
{ slug: slug }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Anticline < AbsoluteTime
|
41
|
+
attribute :identity
|
42
|
+
|
43
|
+
def to_param
|
44
|
+
identity
|
45
|
+
end
|
46
|
+
|
47
|
+
def resource_find_options
|
48
|
+
{ identity: identity }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context '.to_param' do
|
53
|
+
it 'raises an error unless id is defined or the method overridden' do
|
54
|
+
expect { Agate.new.to_param }.to raise_error(
|
55
|
+
RuntimeError, 'Please overide to_param or add id to your model attributes.'
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'sets to_param string based on instance.id' do
|
60
|
+
expect(AbsoluteTime.new(id: identifier).to_param).to eq identifier.to_s
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'sets to_param based the overide' do
|
64
|
+
expect(Aftershock.new(slug: identifier).to_param).to eq identifier
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'sets to_param based the overide' do
|
68
|
+
expect(Amygdule.new(slug: identifier).to_param).to eq identifier
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'sets to_param based the overide' do
|
72
|
+
expect(Anticline.new(identity: identifier).to_param).to eq identifier
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context '.resource_find_options' do
|
77
|
+
it 'raises an error unless id is defined or the method is overridden' do
|
78
|
+
expect { Agate.new.resource_find_options }.to raise_error(
|
79
|
+
RuntimeError, 'Please overide resource_find_options or add id to your model attributes.'
|
80
|
+
)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'returns a hash with the ID' do
|
84
|
+
expect(AbsoluteTime.new(id: identifier).resource_find_options).to eq(id: identifier)
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'returns the overide' do
|
88
|
+
expect(Aftershock.new(slug: identifier).resource_find_options).to eq(slug: identifier)
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'returns the overide' do
|
92
|
+
expect(Amygdule.new(slug: identifier).resource_find_options).to eq(slug: identifier)
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'returns the overide' do
|
96
|
+
expect(Anticline.new(identity: identifier).resource_find_options).to eq(identity: identifier)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/spec/braque/model_spec.rb
CHANGED
@@ -4,9 +4,10 @@ RSpec.describe Braque::Model, type: :model do
|
|
4
4
|
before(:all) do
|
5
5
|
class Breeze
|
6
6
|
include ::Braque::Model
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
api_root_url 'http://localhost:9292'
|
8
|
+
http_authorization_header 'replace-me'
|
9
|
+
accept_header 'application/vnd.el-nino-v1+json'
|
10
|
+
|
10
11
|
attribute :id
|
11
12
|
attribute :title
|
12
13
|
attribute :token
|
@@ -19,16 +20,16 @@ RSpec.describe Braque::Model, type: :model do
|
|
19
20
|
let(:resource_response) { JSON.parse(File.read 'spec/fixtures/resource.json') }
|
20
21
|
|
21
22
|
let(:root_request) do
|
22
|
-
WebMock.stub_request(:get, "#{Breeze.
|
23
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
24
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
23
|
+
WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/")
|
24
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
25
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
25
26
|
.to_return(status: 200, body: root_response)
|
26
27
|
end
|
27
28
|
|
28
29
|
let(:root_request_with_token_resource_path) do
|
29
|
-
WebMock.stub_request(:get, "#{Breeze.
|
30
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
31
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
30
|
+
WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/")
|
31
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
32
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
32
33
|
.to_return(status: 200, body: root_response_with_token_resource_path)
|
33
34
|
end
|
34
35
|
|
@@ -49,9 +50,9 @@ RSpec.describe Braque::Model, type: :model do
|
|
49
50
|
context 'with results' do
|
50
51
|
before(:each) do
|
51
52
|
root_request
|
52
|
-
@collection_request = WebMock.stub_request(:get, "#{Breeze.
|
53
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
54
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
53
|
+
@collection_request = WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/breezes")
|
54
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
55
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
55
56
|
.to_return(status: 200, body: collection_response)
|
56
57
|
@breezes = Breeze.list
|
57
58
|
end
|
@@ -70,18 +71,21 @@ RSpec.describe Braque::Model, type: :model do
|
|
70
71
|
expect(@breezes.first).to be_a_kind_of Breeze
|
71
72
|
end
|
72
73
|
it 'returns an array of items with the correct attributes' do
|
73
|
-
|
74
|
+
index = 0
|
75
|
+
while index < @breezes.size
|
76
|
+
breeze = @breezes[index]
|
74
77
|
expect(breeze.id).to eq collection_response['_embedded']['breezes'][index]['id']
|
75
78
|
expect(breeze.title).to eq collection_response['_embedded']['breezes'][index]['title']
|
79
|
+
index += 1
|
76
80
|
end
|
77
81
|
end
|
78
82
|
end
|
79
83
|
context 'with an errored response' do
|
80
84
|
before(:each) do
|
81
85
|
root_request
|
82
|
-
@collection_request = WebMock.stub_request(:get, "#{Breeze.
|
83
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
84
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
86
|
+
@collection_request = WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/breezes")
|
87
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
88
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
85
89
|
.to_return(status: 500)
|
86
90
|
end
|
87
91
|
it 'returns a Faraday::ClientError error' do
|
@@ -95,9 +99,9 @@ RSpec.describe Braque::Model, type: :model do
|
|
95
99
|
context 'with array params' do
|
96
100
|
before(:each) do
|
97
101
|
root_request
|
98
|
-
@collection_request = WebMock.stub_request(:get, "#{Breeze.
|
99
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
100
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
102
|
+
@collection_request = WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/breezes")
|
103
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
104
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
101
105
|
.with(query: 'ids%5B0%5D=1&ids%5B1%5D=2')
|
102
106
|
.to_return(status: 200, body: collection_response)
|
103
107
|
@breezes = Breeze.list('ids[]' => [1, 2])
|
@@ -112,9 +116,9 @@ RSpec.describe Braque::Model, type: :model do
|
|
112
116
|
context 'under normal conditions' do
|
113
117
|
before(:each) do
|
114
118
|
root_request
|
115
|
-
@resource_request = WebMock.stub_request(:get, "#{Breeze.
|
116
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
117
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
119
|
+
@resource_request = WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/breezes/1")
|
120
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
121
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
118
122
|
.to_return(status: 200, body: resource_response)
|
119
123
|
@breeze = Breeze.find(id: 1)
|
120
124
|
end
|
@@ -135,9 +139,9 @@ RSpec.describe Braque::Model, type: :model do
|
|
135
139
|
context 'with an errored response' do
|
136
140
|
before(:each) do
|
137
141
|
root_request
|
138
|
-
@resource_request = WebMock.stub_request(:get, "#{Breeze.
|
139
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
140
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
142
|
+
@resource_request = WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/breezes/1")
|
143
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
144
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
141
145
|
.to_return(status: 500)
|
142
146
|
end
|
143
147
|
it 'returns a Faraday::ClientError error' do
|
@@ -157,9 +161,9 @@ RSpec.describe Braque::Model, type: :model do
|
|
157
161
|
end
|
158
162
|
|
159
163
|
root_request_with_token_resource_path
|
160
|
-
@resource_request = WebMock.stub_request(:get, "#{Breeze.
|
161
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
162
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
164
|
+
@resource_request = WebMock.stub_request(:get, "#{Breeze.config[:api_root_url]}/breezes/1?token=123")
|
165
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
166
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
163
167
|
.to_return(status: 200, body: resource_response)
|
164
168
|
@breeze = Breeze.find(id: 1, token: 123)
|
165
169
|
end
|
@@ -184,9 +188,9 @@ RSpec.describe Braque::Model, type: :model do
|
|
184
188
|
context 'with a successful response' do
|
185
189
|
before(:each) do
|
186
190
|
root_request
|
187
|
-
@create_request = WebMock.stub_request(:post, "#{Breeze.
|
188
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
189
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
191
|
+
@create_request = WebMock.stub_request(:post, "#{Breeze.config[:api_root_url]}/breezes")
|
192
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
193
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
190
194
|
.to_return(status: 201, body: resource_response)
|
191
195
|
@params = { title: 'What a nice breeze.' }
|
192
196
|
@breeze = Breeze.create(@params)
|
@@ -209,9 +213,9 @@ RSpec.describe Braque::Model, type: :model do
|
|
209
213
|
context 'with an errored response' do
|
210
214
|
before(:each) do
|
211
215
|
root_request
|
212
|
-
@create_request = WebMock.stub_request(:post, "#{Breeze.
|
213
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
214
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
216
|
+
@create_request = WebMock.stub_request(:post, "#{Breeze.config[:api_root_url]}/breezes")
|
217
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
218
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
215
219
|
.to_return(status: 500)
|
216
220
|
end
|
217
221
|
it 'returns a Faraday::ClientError error' do
|
@@ -232,9 +236,9 @@ RSpec.describe Braque::Model, type: :model do
|
|
232
236
|
context 'under normal conditions' do
|
233
237
|
before(:each) do
|
234
238
|
root_request
|
235
|
-
@save_request = WebMock.stub_request(:patch, "#{Breeze.
|
236
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
237
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
239
|
+
@save_request = WebMock.stub_request(:patch, "#{Breeze.config[:api_root_url]}/breezes/1")
|
240
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
241
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
238
242
|
.to_return(status: 200, body: resource_response)
|
239
243
|
@params = { title: 'What a nice breeze.' }
|
240
244
|
@breeze = Breeze.new(id: 1).save(@params)
|
@@ -257,9 +261,9 @@ RSpec.describe Braque::Model, type: :model do
|
|
257
261
|
context 'with an errored response' do
|
258
262
|
before(:each) do
|
259
263
|
root_request
|
260
|
-
@save_request = WebMock.stub_request(:patch, "#{Breeze.
|
261
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
262
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
264
|
+
@save_request = WebMock.stub_request(:patch, "#{Breeze.config[:api_root_url]}/breezes/1")
|
265
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
266
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
263
267
|
.to_return(status: 500)
|
264
268
|
end
|
265
269
|
it 'returns a Faraday::ClientError error' do
|
@@ -280,9 +284,9 @@ RSpec.describe Braque::Model, type: :model do
|
|
280
284
|
end
|
281
285
|
|
282
286
|
root_request_with_token_resource_path
|
283
|
-
@save_request = WebMock.stub_request(:patch, "#{Breeze.
|
284
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
285
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
287
|
+
@save_request = WebMock.stub_request(:patch, "#{Breeze.config[:api_root_url]}/breezes/1?token=123")
|
288
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
289
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
286
290
|
.to_return(status: 200, body: resource_response)
|
287
291
|
@params = { title: 'What a nice breeze.' }
|
288
292
|
@breeze = Breeze.new(id: 1, token: 123).save(@params)
|
@@ -311,9 +315,9 @@ RSpec.describe Braque::Model, type: :model do
|
|
311
315
|
context 'under normal conditions' do
|
312
316
|
before(:each) do
|
313
317
|
root_request
|
314
|
-
@destroy_request = WebMock.stub_request(:delete, "#{Breeze.
|
315
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
316
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
318
|
+
@destroy_request = WebMock.stub_request(:delete, "#{Breeze.config[:api_root_url]}/breezes/1")
|
319
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
320
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
317
321
|
.to_return(status: 200)
|
318
322
|
@breeze = Breeze.new(id: 1).destroy
|
319
323
|
end
|
@@ -328,9 +332,9 @@ RSpec.describe Braque::Model, type: :model do
|
|
328
332
|
context 'with an errored response' do
|
329
333
|
before(:each) do
|
330
334
|
root_request
|
331
|
-
@destroy_request = WebMock.stub_request(:delete, "#{Breeze.
|
332
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
333
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
335
|
+
@destroy_request = WebMock.stub_request(:delete, "#{Breeze.config[:api_root_url]}/breezes/1")
|
336
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
337
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
334
338
|
.to_return(status: 500)
|
335
339
|
end
|
336
340
|
it 'returns a Faraday::ClientError error' do
|
@@ -351,9 +355,9 @@ RSpec.describe Braque::Model, type: :model do
|
|
351
355
|
end
|
352
356
|
|
353
357
|
root_request_with_token_resource_path
|
354
|
-
@destroy_request = WebMock.stub_request(:delete, "#{Breeze.
|
355
|
-
.with(headers: { 'Http-Authorization' => Breeze.http_authorization_header })
|
356
|
-
.with(headers: { 'Accept' => Breeze.accept_header })
|
358
|
+
@destroy_request = WebMock.stub_request(:delete, "#{Breeze.config[:api_root_url]}/breezes/1?token=123")
|
359
|
+
.with(headers: { 'Http-Authorization' => Breeze.config[:http_authorization_header] })
|
360
|
+
.with(headers: { 'Accept' => Breeze.config[:accept_header] })
|
357
361
|
.to_return(status: 200)
|
358
362
|
@breeze = Breeze.new(id: 1, token: 123).destroy
|
359
363
|
end
|
@@ -5,22 +5,22 @@ RSpec.describe Braque::Model, type: :model do
|
|
5
5
|
before do
|
6
6
|
class Wind
|
7
7
|
include ::Braque::Model
|
8
|
-
|
9
|
-
|
8
|
+
api_root_url 'http://localhost:9292'
|
9
|
+
http_authorization_header 'replace-me'
|
10
10
|
end
|
11
11
|
|
12
12
|
class Tide
|
13
13
|
include ::Braque::Model
|
14
|
-
|
15
|
-
|
14
|
+
api_root_url 'http://localhost:9393'
|
15
|
+
http_authorization_header 'do-replace-me-as-well'
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'sets the correct api_root and http_authorization_header for each class' do
|
20
|
-
expect(Tide.
|
21
|
-
expect(Tide.http_authorization_header).to eq 'do-replace-me-as-well'
|
22
|
-
expect(Wind.
|
23
|
-
expect(Wind.http_authorization_header).to eq 'replace-me'
|
20
|
+
expect(Tide.config[:api_root_url]).to eq 'http://localhost:9393'
|
21
|
+
expect(Tide.config[:http_authorization_header]).to eq 'do-replace-me-as-well'
|
22
|
+
expect(Wind.config[:api_root_url]).to eq 'http://localhost:9292'
|
23
|
+
expect(Wind.config[:http_authorization_header]).to eq 'replace-me'
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -5,41 +5,41 @@ RSpec.describe Braque::Model, type: :model do
|
|
5
5
|
before do
|
6
6
|
class GeologicModel
|
7
7
|
include ::Braque::Model
|
8
|
-
|
9
|
-
|
8
|
+
api_root_url 'http://localhost:9292'
|
9
|
+
http_authorization_header 'replace-me'
|
10
10
|
end
|
11
11
|
|
12
12
|
class ContinentalDrift < GeologicModel
|
13
13
|
end
|
14
14
|
|
15
15
|
class Convection < GeologicModel
|
16
|
-
|
17
|
-
|
16
|
+
api_root_url 'http://localhost:9595'
|
17
|
+
http_authorization_header 'ok-another-one-to-replace'
|
18
18
|
end
|
19
19
|
|
20
20
|
class SeafloorSpreading
|
21
21
|
include ::Braque::Model
|
22
|
-
|
23
|
-
|
22
|
+
api_root_url 'http://localhost:9393'
|
23
|
+
http_authorization_header 'do-replace-me-as-well'
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
27
|
context 'api configuration' do
|
28
28
|
it 'sets the correct api_root and api_token for the base class' do
|
29
|
-
expect(GeologicModel.
|
30
|
-
expect(GeologicModel.http_authorization_header).to eq 'replace-me'
|
29
|
+
expect(GeologicModel.config[:api_root_url]).to eq 'http://localhost:9292'
|
30
|
+
expect(GeologicModel.config[:http_authorization_header]).to eq 'replace-me'
|
31
31
|
end
|
32
32
|
it 'sets the correct api_root and api_token for a subclassed class' do
|
33
|
-
expect(ContinentalDrift.
|
34
|
-
expect(ContinentalDrift.http_authorization_header).to eq 'replace-me'
|
33
|
+
expect(ContinentalDrift.config[:api_root_url]).to eq 'http://localhost:9292'
|
34
|
+
expect(ContinentalDrift.config[:http_authorization_header]).to eq 'replace-me'
|
35
35
|
end
|
36
36
|
it 'sets the correct api_root and api_token for a subclassed class with variable overrides' do
|
37
|
-
expect(Convection.
|
38
|
-
expect(Convection.http_authorization_header).to eq 'ok-another-one-to-replace'
|
37
|
+
expect(Convection.config[:api_root_url]).to eq 'http://localhost:9595'
|
38
|
+
expect(Convection.config[:http_authorization_header]).to eq 'ok-another-one-to-replace'
|
39
39
|
end
|
40
40
|
it 'sets the correct api_root and api_token for a new Braque::Model class' do
|
41
|
-
expect(SeafloorSpreading.
|
42
|
-
expect(SeafloorSpreading.http_authorization_header).to eq 'do-replace-me-as-well'
|
41
|
+
expect(SeafloorSpreading.config[:api_root_url]).to eq 'http://localhost:9393'
|
42
|
+
expect(SeafloorSpreading.config[:http_authorization_header]).to eq 'do-replace-me-as-well'
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
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.7
|
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-09-
|
11
|
+
date: 2015-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hyperclient
|
@@ -68,9 +68,11 @@ 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/api_root_spec.rb
|
71
72
|
- spec/braque/client_headers_spec.rb
|
72
73
|
- spec/braque/collection_spec.rb
|
73
74
|
- spec/braque/helpers/hypermedia_responses_helper_spec.rb
|
75
|
+
- spec/braque/model_attributes_spec.rb
|
74
76
|
- spec/braque/model_spec.rb
|
75
77
|
- spec/braque/multiple_model_spec.rb
|
76
78
|
- spec/braque/subclassed_model_spec.rb
|