bootic_client 0.0.19 → 0.0.20
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -1
- data/lib/bootic_client.rb +39 -4
- data/lib/bootic_client/entity.rb +4 -4
- data/lib/bootic_client/relation.rb +17 -7
- data/lib/bootic_client/version.rb +1 -1
- data/lib/bootic_client/whiny_uri.rb +28 -7
- data/spec/client_credentials_strategy_spec.rb +1 -1
- data/spec/configuration_spec.rb +63 -0
- data/spec/entity_spec.rb +6 -0
- data/spec/relation_spec.rb +19 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd55ff4344906b2db45c139a932b1fe546bd83ac
|
4
|
+
data.tar.gz: 773cbf15b9e596ef502aae54e65df649c9da3bc1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: febd1da193a466980cfaaa62b8bcf29eb78a44b7629f65754b12dfd9d06c98266a005dd0fad6c2aa7a50d5b465feef096526a9edac07255dfaca9d37f9c583cc
|
7
|
+
data.tar.gz: 94449a8bfc7d58a03c9ae30cf80db26fe6e2946c0d7a183c05b69ee87fc4248834cf6642b2f116798026f09d9b20da4575c4cd9ce6c4ca866e17f2536b0c2f07
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
## [v0.0.19](https://github.com/bootic/bootic_client.rb/tree/v0.0.19) (2017-06-01)
|
4
|
+
[Full Changelog](https://github.com/bootic/bootic_client.rb/compare/v0.0.18...v0.0.19)
|
5
|
+
|
6
|
+
**Merged pull requests:**
|
7
|
+
|
8
|
+
- Raise errors on invalid URLs [\#11](https://github.com/bootic/bootic_client.rb/pull/11) ([ismasan](https://github.com/ismasan))
|
9
|
+
|
3
10
|
## [v0.0.18](https://github.com/bootic/bootic_client.rb/tree/v0.0.18) (2017-04-03)
|
4
11
|
[Full Changelog](https://github.com/bootic/bootic_client.rb/compare/v0.0.17...v0.0.18)
|
5
12
|
|
@@ -33,7 +40,7 @@
|
|
33
40
|
|
34
41
|
**Merged pull requests:**
|
35
42
|
|
36
|
-
- Base64-encode IO instances before POST|PUT|
|
43
|
+
- Base64-encode IO instances before POST|PUT|PATCH [\#6](https://github.com/bootic/bootic_client.rb/pull/6) ([ismasan](https://github.com/ismasan))
|
37
44
|
|
38
45
|
## [v0.0.13](https://github.com/bootic/bootic_client.rb/tree/v0.0.13) (2016-01-12)
|
39
46
|
[Full Changelog](https://github.com/bootic/bootic_client.rb/compare/v0.0.12...v0.0.13)
|
data/lib/bootic_client.rb
CHANGED
@@ -5,14 +5,15 @@ require "bootic_client/relation"
|
|
5
5
|
require "bootic_client/client"
|
6
6
|
|
7
7
|
module BooticClient
|
8
|
+
InvalidConfigurationError = Class.new(StandardError)
|
9
|
+
VERY_BASIC_URL_CHECK = /^(http|https):/.freeze
|
8
10
|
|
9
11
|
AUTH_HOST = 'https://auth.bootic.net'.freeze
|
10
12
|
API_ROOT = 'https://api.bootic.net/v1'.freeze
|
11
13
|
|
12
14
|
class << self
|
13
|
-
|
14
|
-
|
15
|
-
attr_writer :auth_host, :api_root, :logger
|
15
|
+
attr_accessor :logging
|
16
|
+
attr_reader :client_id, :client_secret, :cache_store
|
16
17
|
|
17
18
|
def strategies
|
18
19
|
@strategies ||= {}
|
@@ -27,6 +28,32 @@ module BooticClient
|
|
27
28
|
strategies.fetch(strategy_name.to_sym).new self, opts, &on_new_token
|
28
29
|
end
|
29
30
|
|
31
|
+
def client_id=(v)
|
32
|
+
set_non_nil :client_id, v
|
33
|
+
end
|
34
|
+
|
35
|
+
def client_secret=(v)
|
36
|
+
set_non_nil :client_secret, v
|
37
|
+
end
|
38
|
+
|
39
|
+
def cache_store=(v)
|
40
|
+
set_non_nil :cache_store, v
|
41
|
+
end
|
42
|
+
|
43
|
+
def auth_host=(v)
|
44
|
+
check_url! :auth_host, v
|
45
|
+
set_non_nil :auth_host, v
|
46
|
+
end
|
47
|
+
|
48
|
+
def api_root=(v)
|
49
|
+
check_url! :api_root, v
|
50
|
+
set_non_nil :api_root, v
|
51
|
+
end
|
52
|
+
|
53
|
+
def logger=(v)
|
54
|
+
set_non_nil :logger, v
|
55
|
+
end
|
56
|
+
|
30
57
|
def auth_host
|
31
58
|
@auth_host || AUTH_HOST
|
32
59
|
end
|
@@ -42,6 +69,14 @@ module BooticClient
|
|
42
69
|
def configure(&block)
|
43
70
|
yield self
|
44
71
|
end
|
45
|
-
end
|
46
72
|
|
73
|
+
def set_non_nil(name, v)
|
74
|
+
raise InvalidConfigurationError, "#{name} cannot be nil" if v.nil?
|
75
|
+
instance_variable_set("@#{name}", v)
|
76
|
+
end
|
77
|
+
|
78
|
+
def check_url!(name, v)
|
79
|
+
raise InvalidConfigurationError, "#{name} must be a valid URL" unless v.to_s =~ VERY_BASIC_URL_CHECK
|
80
|
+
end
|
81
|
+
end
|
47
82
|
end
|
data/lib/bootic_client/entity.rb
CHANGED
@@ -30,7 +30,7 @@ module BooticClient
|
|
30
30
|
|
31
31
|
attr_reader :curies, :entities
|
32
32
|
|
33
|
-
def initialize(attrs, client, top
|
33
|
+
def initialize(attrs, client, top: self)
|
34
34
|
@attrs = attrs.kind_of?(Hash) ? attrs : {}
|
35
35
|
@client, @top = client, top
|
36
36
|
build!
|
@@ -123,7 +123,7 @@ module BooticClient
|
|
123
123
|
end
|
124
124
|
if rel != CURIES_REL
|
125
125
|
rel_attrs['name'] = rel
|
126
|
-
memo[rel.to_sym] = Relation.new(rel_attrs, client
|
126
|
+
memo[rel.to_sym] = Relation.new(rel_attrs, client)
|
127
127
|
end
|
128
128
|
end
|
129
129
|
)
|
@@ -142,9 +142,9 @@ module BooticClient
|
|
142
142
|
|
143
143
|
@entities = attrs.fetch('_embedded', {}).each_with_object({}) do |(k,v),memo|
|
144
144
|
memo[k.to_sym] = if v.kind_of?(Array)
|
145
|
-
v.map{|ent_attrs| Entity.new(ent_attrs, client, top)}
|
145
|
+
v.map{|ent_attrs| Entity.new(ent_attrs, client, top: top)}
|
146
146
|
else
|
147
|
-
Entity.new(v, client, top)
|
147
|
+
Entity.new(v, client, top: top)
|
148
148
|
end
|
149
149
|
end
|
150
150
|
end
|
@@ -1,16 +1,26 @@
|
|
1
1
|
require "bootic_client/whiny_uri"
|
2
2
|
require "bootic_client/entity"
|
3
|
+
require 'ostruct'
|
3
4
|
|
4
5
|
module BooticClient
|
5
6
|
|
6
7
|
class Relation
|
7
|
-
|
8
8
|
GET = 'get'.freeze
|
9
9
|
HEAD = 'head'.freeze
|
10
10
|
OPTIONS = 'options'.freeze
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
class << self
|
13
|
+
attr_writer :complain_on_undeclared_params
|
14
|
+
|
15
|
+
def complain_on_undeclared_params
|
16
|
+
return true unless instance_variable_defined?('@complain_on_undeclared_params')
|
17
|
+
@complain_on_undeclared_params
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(attrs, client, entity_class: Entity, complain_on_undeclared_params: self.class.complain_on_undeclared_params)
|
22
|
+
@attrs, @client, @entity_class = attrs, client, entity_class
|
23
|
+
@complain_on_undeclared_params = complain_on_undeclared_params
|
14
24
|
end
|
15
25
|
|
16
26
|
def inspect
|
@@ -61,9 +71,9 @@ module BooticClient
|
|
61
71
|
end
|
62
72
|
# remove payload vars from URI opts if destructive action
|
63
73
|
opts = opts.reject{|k, v| !uri_vars.include?(k.to_s) } if destructive?
|
64
|
-
client.request_and_wrap transport_method.to_sym, uri.expand(opts),
|
74
|
+
client.request_and_wrap transport_method.to_sym, uri.expand(opts), entity_class, payload
|
65
75
|
else
|
66
|
-
client.request_and_wrap transport_method.to_sym, href,
|
76
|
+
client.request_and_wrap transport_method.to_sym, href, entity_class, opts
|
67
77
|
end
|
68
78
|
end
|
69
79
|
|
@@ -72,10 +82,10 @@ module BooticClient
|
|
72
82
|
end
|
73
83
|
|
74
84
|
protected
|
75
|
-
attr_reader :
|
85
|
+
attr_reader :entity_class, :client, :attrs, :complain_on_undeclared_params
|
76
86
|
|
77
87
|
def uri
|
78
|
-
@uri ||= WhinyURI.new(href)
|
88
|
+
@uri ||= WhinyURI.new(href, complain_on_undeclared_params)
|
79
89
|
end
|
80
90
|
|
81
91
|
def destructive?
|
@@ -4,28 +4,33 @@ module BooticClient
|
|
4
4
|
class WhinyURI
|
5
5
|
attr_reader :variables
|
6
6
|
|
7
|
-
def initialize(href)
|
7
|
+
def initialize(href, complain_on_undeclared_params = true)
|
8
8
|
@href = href
|
9
9
|
@uri = URITemplate.new(href)
|
10
10
|
@variables = @uri.variables
|
11
|
+
@complain_on_undeclared_params = complain_on_undeclared_params
|
11
12
|
end
|
12
13
|
|
13
14
|
def expand(attrs = {})
|
15
|
+
attrs = stringify(attrs)
|
16
|
+
|
14
17
|
missing = missing_path_variables(attrs)
|
15
18
|
if missing.any?
|
16
19
|
raise InvalidURLError, missing_err(missing)
|
17
20
|
end
|
18
21
|
|
19
22
|
undeclared = undeclared_params(attrs)
|
20
|
-
if
|
21
|
-
|
23
|
+
if complain_on_undeclared_params
|
24
|
+
if undeclared.any?
|
25
|
+
raise InvalidURLError, undeclared_err(undeclared)
|
26
|
+
end
|
22
27
|
end
|
23
28
|
|
24
|
-
uri.expand attrs
|
29
|
+
uri.expand whitelisted(attrs)
|
25
30
|
end
|
26
31
|
|
27
32
|
private
|
28
|
-
attr_reader :uri, :href
|
33
|
+
attr_reader :uri, :href, :complain_on_undeclared_params
|
29
34
|
|
30
35
|
def path_variables
|
31
36
|
@path_variables ||= (
|
@@ -35,12 +40,22 @@ module BooticClient
|
|
35
40
|
)
|
36
41
|
end
|
37
42
|
|
43
|
+
def whitelisted(attrs = {})
|
44
|
+
variables.each_with_object({}) do |key, hash|
|
45
|
+
hash[key] = attrs[key] if attrs.key?(key)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
38
49
|
def missing_path_variables(attrs)
|
39
|
-
path_variables - attrs.keys
|
50
|
+
path_variables - attrs.keys
|
51
|
+
end
|
52
|
+
|
53
|
+
def declared_params
|
54
|
+
@declared_params ||= variables - path_variables
|
40
55
|
end
|
41
56
|
|
42
57
|
def undeclared_params(attrs)
|
43
|
-
attrs.keys
|
58
|
+
attrs.keys - variables
|
44
59
|
end
|
45
60
|
|
46
61
|
def undeclared_err(undeclared)
|
@@ -57,6 +72,12 @@ module BooticClient
|
|
57
72
|
def format_vars(vars)
|
58
73
|
vars.map{|v| "`#{v}`"}.join(', ')
|
59
74
|
end
|
75
|
+
|
76
|
+
def stringify(attrs)
|
77
|
+
attrs.each_with_object({}) do |(k, v), hash|
|
78
|
+
hash[k.to_s] = v
|
79
|
+
end
|
80
|
+
end
|
60
81
|
end
|
61
82
|
end
|
62
83
|
|
@@ -15,7 +15,7 @@ describe 'BooticClient::Strategies::ClientCredentials' do
|
|
15
15
|
|
16
16
|
describe 'with missing client credentials' do
|
17
17
|
it 'raises error' do
|
18
|
-
BooticClient.client_id
|
18
|
+
allow(BooticClient).to receive(:client_id).and_return nil
|
19
19
|
expect{
|
20
20
|
BooticClient.client(:client_credentials, scope: 'admin')
|
21
21
|
}.to raise_error(ArgumentError)
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BooticClient do
|
4
|
+
it "raises if nil client_id" do
|
5
|
+
expect {
|
6
|
+
BooticClient.configure do |c|
|
7
|
+
c.client_id = nil
|
8
|
+
end
|
9
|
+
}.to raise_error BooticClient::InvalidConfigurationError
|
10
|
+
end
|
11
|
+
|
12
|
+
it "raises if nil client_secret" do
|
13
|
+
expect {
|
14
|
+
BooticClient.configure do |c|
|
15
|
+
c.client_secret = nil
|
16
|
+
end
|
17
|
+
}.to raise_error BooticClient::InvalidConfigurationError
|
18
|
+
end
|
19
|
+
|
20
|
+
it "raises if nil cache_store" do
|
21
|
+
expect {
|
22
|
+
BooticClient.configure do |c|
|
23
|
+
c.cache_store = nil
|
24
|
+
end
|
25
|
+
}.to raise_error BooticClient::InvalidConfigurationError
|
26
|
+
end
|
27
|
+
|
28
|
+
it "raises if nil or invalid auth_host" do
|
29
|
+
expect {
|
30
|
+
BooticClient.configure do |c|
|
31
|
+
c.auth_host = nil
|
32
|
+
end
|
33
|
+
}.to raise_error BooticClient::InvalidConfigurationError
|
34
|
+
|
35
|
+
expect {
|
36
|
+
BooticClient.configure do |c|
|
37
|
+
c.auth_host = 'not-a-url'
|
38
|
+
end
|
39
|
+
}.to raise_error BooticClient::InvalidConfigurationError
|
40
|
+
end
|
41
|
+
|
42
|
+
it "raises if nil or invalid api_root" do
|
43
|
+
expect {
|
44
|
+
BooticClient.configure do |c|
|
45
|
+
c.api_root = nil
|
46
|
+
end
|
47
|
+
}.to raise_error BooticClient::InvalidConfigurationError
|
48
|
+
|
49
|
+
expect {
|
50
|
+
BooticClient.configure do |c|
|
51
|
+
c.api_root = 'not-a-url'
|
52
|
+
end
|
53
|
+
}.to raise_error BooticClient::InvalidConfigurationError
|
54
|
+
end
|
55
|
+
|
56
|
+
it "raises if nil logger" do
|
57
|
+
expect {
|
58
|
+
BooticClient.configure do |c|
|
59
|
+
c.logger = nil
|
60
|
+
end
|
61
|
+
}.to raise_error BooticClient::InvalidConfigurationError
|
62
|
+
end
|
63
|
+
end
|
data/spec/entity_spec.rb
CHANGED
@@ -174,6 +174,12 @@ describe BooticClient::Entity do
|
|
174
174
|
expect(next_entity.page).to eql(2)
|
175
175
|
end
|
176
176
|
end
|
177
|
+
|
178
|
+
it 'complains if passing undeclared link params' do
|
179
|
+
expect {
|
180
|
+
entity.search(foo: 'bar')
|
181
|
+
}.to raise_error(BooticClient::InvalidURLError)
|
182
|
+
end
|
177
183
|
end
|
178
184
|
|
179
185
|
end
|
data/spec/relation_spec.rb
CHANGED
@@ -80,6 +80,25 @@ describe BooticClient::Relation do
|
|
80
80
|
}.to raise_error BooticClient::InvalidURLError
|
81
81
|
end
|
82
82
|
end
|
83
|
+
|
84
|
+
context "configured to not complain on undeclared variables" do
|
85
|
+
it "whitelists params but does not complain" do
|
86
|
+
BooticClient::Relation.complain_on_undeclared_params = false
|
87
|
+
|
88
|
+
relation = BooticClient::Relation.new({
|
89
|
+
'href' => '/foos/{id}{?q,page}',
|
90
|
+
'templated' => true
|
91
|
+
},
|
92
|
+
client,
|
93
|
+
)
|
94
|
+
|
95
|
+
expect(client).to receive(:request_and_wrap).with(:get, '/foos/2?q=test&page=3', BooticClient::Entity, {foo: 1}).and_return entity
|
96
|
+
|
97
|
+
relation.run(id: 2, q: 'test', page: 3, foo: 1)
|
98
|
+
|
99
|
+
BooticClient::Relation.complain_on_undeclared_params = true
|
100
|
+
end
|
101
|
+
end
|
83
102
|
end
|
84
103
|
|
85
104
|
describe 'POST' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bootic_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.20
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ismael Celis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -201,6 +201,7 @@ files:
|
|
201
201
|
- spec/bootic_client_spec.rb
|
202
202
|
- spec/client_credentials_strategy_spec.rb
|
203
203
|
- spec/client_spec.rb
|
204
|
+
- spec/configuration_spec.rb
|
204
205
|
- spec/entity_spec.rb
|
205
206
|
- spec/fixtures/file.gif
|
206
207
|
- spec/memcache_storage_spec.rb
|
@@ -227,7 +228,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
227
228
|
version: '0'
|
228
229
|
requirements: []
|
229
230
|
rubyforge_project:
|
230
|
-
rubygems_version: 2.
|
231
|
+
rubygems_version: 2.6.13
|
231
232
|
signing_key:
|
232
233
|
specification_version: 4
|
233
234
|
summary: Official Ruby client for the Bootic API
|
@@ -238,6 +239,7 @@ test_files:
|
|
238
239
|
- spec/bootic_client_spec.rb
|
239
240
|
- spec/client_credentials_strategy_spec.rb
|
240
241
|
- spec/client_spec.rb
|
242
|
+
- spec/configuration_spec.rb
|
241
243
|
- spec/entity_spec.rb
|
242
244
|
- spec/fixtures/file.gif
|
243
245
|
- spec/memcache_storage_spec.rb
|