shaf_client 0.6.3 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/shaf_client/base_resource.rb +33 -17
- data/lib/shaf_client/curie.rb +4 -0
- data/lib/shaf_client/empty_resource.rb +1 -1
- data/lib/shaf_client/hypertext_cache_strategy.rb +47 -0
- data/lib/shaf_client/mime_types.rb +7 -0
- data/lib/shaf_client/resource.rb +46 -8
- data/lib/shaf_client/resource_mapper.rb +4 -0
- data/lib/shaf_client/unknown_resource.rb +1 -1
- data/lib/shaf_client/version.rb +3 -0
- data/lib/shaf_client.rb +8 -10
- data.tar.gz.sig +0 -0
- metadata +5 -2
- metadata.gz.sig +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de51f879d8fd9a032434411bbe29df1b1dd6629c39277c1373384ae10615e2ff
|
4
|
+
data.tar.gz: fa37f60def169a85f3910d6c4dd36faa3155b299f42725606682500e722c504e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e11dd7e17aa9052606440cbb16c06e8bec99933b0d1033c803116e14c18591795eb26304277288f249c8812a3991d8a2a3bca6deedd1e0c59ea0a1a529abd85
|
7
|
+
data.tar.gz: 4db2d441198e463dc8f590638d2017baab472d4d00a205b0694c6b9aef74f6952d5fb715e408f81da70959bbf9d7aeb02970ecc78a1853859dbcb595c0c43432
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
@@ -10,8 +10,13 @@ class ShafClient
|
|
10
10
|
@payload =
|
11
11
|
if payload&.is_a? String
|
12
12
|
JSON.parse(payload)
|
13
|
+
elsif payload.respond_to? :to_h
|
14
|
+
payload.to_h
|
13
15
|
else
|
14
|
-
|
16
|
+
raise Error, <<~ERR
|
17
|
+
Trying to create an instance of #{self.class} with a payload that
|
18
|
+
cannot be coerced into a Hash
|
19
|
+
ERR
|
15
20
|
end
|
16
21
|
|
17
22
|
parse
|
@@ -19,8 +24,9 @@ class ShafClient
|
|
19
24
|
|
20
25
|
def to_h
|
21
26
|
attributes.dup.tap do |hash|
|
22
|
-
hash[:_links] =
|
23
|
-
|
27
|
+
hash[:_links] = transform_values_to_h(links)
|
28
|
+
hash[:_links].merge!(curies: curies.values.map(&:to_h)) unless curies.empty?
|
29
|
+
embedded = transform_values_to_h(embedded_resources)
|
24
30
|
hash[:_embedded] = embedded unless embedded.empty?
|
25
31
|
end
|
26
32
|
end
|
@@ -34,31 +40,23 @@ class ShafClient
|
|
34
40
|
end
|
35
41
|
|
36
42
|
def attribute(key)
|
37
|
-
raise Error, "No attribute for key: #{key}"
|
38
|
-
attributes.fetch(key.to_sym)
|
43
|
+
_attribute(key) or raise Error, "No attribute for key: #{key}"
|
39
44
|
end
|
40
45
|
|
41
46
|
def link(rel)
|
42
|
-
|
43
|
-
raise Error, "No link with rel: #{rel}" unless links.key? rewritten_rel
|
44
|
-
links[rewritten_rel]
|
47
|
+
_link(rel) or raise Error, "No link with rel: #{rel}"
|
45
48
|
end
|
46
49
|
|
47
50
|
def curie(rel)
|
48
|
-
raise Error, "No curie with rel: #{rel}"
|
49
|
-
curies[rel.to_sym]
|
51
|
+
_curie(rel) or raise Error, "No curie with rel: #{rel}"
|
50
52
|
end
|
51
53
|
|
52
54
|
def embedded(rel)
|
53
|
-
|
54
|
-
unless embedded_resources.key? rewritten_rel
|
55
|
-
raise Error, "No embedded resources with rel: #{rel}"
|
56
|
-
end
|
57
|
-
embedded_resources[rewritten_rel]
|
55
|
+
_embedded(rel) or raise Error, "No embedded resources with rel: #{rel}"
|
58
56
|
end
|
59
57
|
|
60
58
|
def rel?(rel)
|
61
|
-
!link(rel).nil?
|
59
|
+
!link(rel).nil? || !embedded(rel).nil?
|
62
60
|
rescue StandardError
|
63
61
|
false
|
64
62
|
end
|
@@ -77,6 +75,24 @@ class ShafClient
|
|
77
75
|
@payload ||= {}
|
78
76
|
end
|
79
77
|
|
78
|
+
def _attribute(key)
|
79
|
+
attributes[key.to_sym]
|
80
|
+
end
|
81
|
+
|
82
|
+
def _link(rel)
|
83
|
+
rewritten_rel = best_match(links.keys, rel)
|
84
|
+
links[rewritten_rel]
|
85
|
+
end
|
86
|
+
|
87
|
+
def _curie(rel)
|
88
|
+
curies[rel.to_sym]
|
89
|
+
end
|
90
|
+
|
91
|
+
def _embedded(rel)
|
92
|
+
rewritten_rel = best_match(embedded_resources.keys, rel)
|
93
|
+
embedded_resources[rewritten_rel]
|
94
|
+
end
|
95
|
+
|
80
96
|
def <<(other)
|
81
97
|
@payload = other.payload.dup
|
82
98
|
@attributes = other.attributes.dup
|
@@ -148,7 +164,7 @@ class ShafClient
|
|
148
164
|
best_match(rels, rel.to_s.tr('_', '-')) if rel.to_s.include? '_'
|
149
165
|
end
|
150
166
|
|
151
|
-
def
|
167
|
+
def transform_values_to_h(hash)
|
152
168
|
hash.transform_values do |value|
|
153
169
|
if value.is_a? Array
|
154
170
|
value.map(&:to_h)
|
data/lib/shaf_client/curie.rb
CHANGED
@@ -0,0 +1,47 @@
|
|
1
|
+
class ShafClient
|
2
|
+
module HypertextCacheStrategy
|
3
|
+
AVAILABLE_CACHE_STRATEGIES = [
|
4
|
+
CACHE_STRATEGY_NO_CACHE = :no_cache,
|
5
|
+
CACHE_STRATEGY_EMBEDDED = :use_embedded,
|
6
|
+
CACHE_STRATEGY_FETCH_HEADERS = :fetch_headers
|
7
|
+
]
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def cacheable?(strategy)
|
11
|
+
[CACHE_STRATEGY_EMBEDDED, CACHE_STRATEGY_FETCH_HEADERS].include? strategy
|
12
|
+
end
|
13
|
+
|
14
|
+
def fetch_headers?(strategy)
|
15
|
+
CACHE_STRATEGY_FETCH_HEADERS == strategy
|
16
|
+
end
|
17
|
+
|
18
|
+
def default_http_status
|
19
|
+
203
|
20
|
+
end
|
21
|
+
|
22
|
+
def default_headers
|
23
|
+
{'content-type' => 'application/hal+json'}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def default_hypertext_cache_strategy
|
28
|
+
@__default_hypertext_cache_strategy ||= CACHE_STRATEGY_EMBEDDED
|
29
|
+
end
|
30
|
+
|
31
|
+
def default_hypertext_cache_strategy=(strategy)
|
32
|
+
unless __valid_cache? strategy
|
33
|
+
raise Error, <<~ERR
|
34
|
+
Unsupported hypertext cache strategy: #{strategy}
|
35
|
+
Possible strategies are: #{AVAILABLE_CACHE_STRATEGIES.join(', ')}
|
36
|
+
ERR
|
37
|
+
end
|
38
|
+
@__default_hypertext_cache_strategy = value
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def __valid_cache?(strategy)
|
44
|
+
AVAILABLE_CACHE_STRATEGIES.include? strategy
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/shaf_client/resource.rb
CHANGED
@@ -4,21 +4,23 @@ require 'shaf_client/base_resource'
|
|
4
4
|
|
5
5
|
class ShafClient
|
6
6
|
class Resource < BaseResource
|
7
|
+
include MimeTypes
|
8
|
+
|
7
9
|
attr_reader :http_status, :headers
|
8
10
|
|
9
|
-
ResourceMapper.register(
|
11
|
+
ResourceMapper.register(MIME_TYPE_HAL, self)
|
10
12
|
|
11
13
|
def self.content_type(type)
|
12
14
|
ResourceMapper.register(type, self)
|
13
15
|
end
|
14
16
|
|
15
17
|
def self.profile(name)
|
16
|
-
content_type "
|
18
|
+
content_type "#{MIME_TYPE_HAL};profile=#{name}"
|
17
19
|
end
|
18
20
|
|
19
|
-
def self.build(client, payload, status = nil, headers = {})
|
20
|
-
|
21
|
-
|
21
|
+
def self.build(client, payload, content_type = MIME_TYPE_HAL, status = nil, headers = {})
|
22
|
+
ResourceMapper.for(content_type)
|
23
|
+
.new(client, payload, status, headers)
|
22
24
|
end
|
23
25
|
|
24
26
|
def initialize(client, payload, status = nil, headers = {})
|
@@ -36,14 +38,21 @@ class ShafClient
|
|
36
38
|
RESOURCE
|
37
39
|
end
|
38
40
|
|
39
|
-
%i[
|
41
|
+
%i[put post delete patch].each do |method|
|
40
42
|
define_method(method) do |rel, payload = nil, **options|
|
41
43
|
href = link(rel).href
|
42
44
|
client.send(method, href, payload: payload, **options)
|
43
45
|
end
|
44
46
|
end
|
45
47
|
|
46
|
-
def
|
48
|
+
def get(rel, **options)
|
49
|
+
href = link(rel).href
|
50
|
+
embedded_resource = _embedded(rel)
|
51
|
+
cached_resource = hypertext_cache_resource(href, embedded_resource, options)
|
52
|
+
cached_resource || client.get(href, **options)
|
53
|
+
end
|
54
|
+
|
55
|
+
def get_doc(rel, **options)
|
47
56
|
rel = rel.to_s
|
48
57
|
curie_name, rel =
|
49
58
|
if rel.include? ':'
|
@@ -54,7 +63,7 @@ class ShafClient
|
|
54
63
|
|
55
64
|
curie = curie(curie_name)
|
56
65
|
uri = curie.resolve_templated(rel: rel)
|
57
|
-
client.
|
66
|
+
client.get(uri, options)
|
58
67
|
end
|
59
68
|
|
60
69
|
def get_hal_form(rel)
|
@@ -91,5 +100,34 @@ class ShafClient
|
|
91
100
|
private
|
92
101
|
|
93
102
|
attr_reader :client
|
103
|
+
|
104
|
+
def hypertext_cache_strategy(options)
|
105
|
+
options.fetch(:hypertext_cache_strategy) do
|
106
|
+
ShafClient.default_hypertext_cache_strategy
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def hypertext_cache?(options)
|
111
|
+
HypertextCacheStrategy.cacheable? hypertext_cache_strategy(options)
|
112
|
+
end
|
113
|
+
|
114
|
+
def hypertext_cache_resource(href, embedded_resource, options)
|
115
|
+
return unless embedded_resource
|
116
|
+
|
117
|
+
cache_strategy = hypertext_cache_strategy(options)
|
118
|
+
return unless HypertextCacheStrategy.cacheable? cache_strategy
|
119
|
+
|
120
|
+
if HypertextCacheStrategy.fetch_headers? cache_strategy
|
121
|
+
resource = client.head(href, options)
|
122
|
+
status = resource.http_status
|
123
|
+
headers = resource.headers
|
124
|
+
embedded_resource = embedded_resource.payload
|
125
|
+
else
|
126
|
+
status = HypertextCacheStrategy.default_http_status
|
127
|
+
headers = HypertextCacheStrategy.default_headers
|
128
|
+
end
|
129
|
+
|
130
|
+
self.class.build(client, embedded_resource, headers['content-type'], status, headers)
|
131
|
+
end
|
94
132
|
end
|
95
133
|
end
|
data/lib/shaf_client.rb
CHANGED
@@ -9,6 +9,7 @@ require 'faraday_http_cache_patch'
|
|
9
9
|
|
10
10
|
require 'json'
|
11
11
|
require 'shaf_client/error'
|
12
|
+
require 'shaf_client/mime_types'
|
12
13
|
require 'shaf_client/middleware/redirect'
|
13
14
|
require 'shaf_client/resource'
|
14
15
|
require 'shaf_client/shaf_form'
|
@@ -16,10 +17,12 @@ require 'shaf_client/hal_form'
|
|
16
17
|
require 'shaf_client/api_error'
|
17
18
|
require 'shaf_client/empty_resource'
|
18
19
|
require 'shaf_client/unknown_resource'
|
20
|
+
require 'shaf_client/hypertext_cache_strategy'
|
19
21
|
|
20
22
|
class ShafClient
|
21
|
-
|
22
|
-
|
23
|
+
extend HypertextCacheStrategy
|
24
|
+
include MimeTypes
|
25
|
+
|
23
26
|
DEFAULT_ADAPTER = :net_http
|
24
27
|
|
25
28
|
def initialize(root_uri, **options)
|
@@ -35,12 +38,7 @@ class ShafClient
|
|
35
38
|
get(@root_uri, **options)
|
36
39
|
end
|
37
40
|
|
38
|
-
|
39
|
-
response = request(method: :get, uri: uri, opts: options)
|
40
|
-
response&.body || ''
|
41
|
-
end
|
42
|
-
|
43
|
-
%i[get put post delete patch].each do |method|
|
41
|
+
%i[head get put post delete patch].each do |method|
|
44
42
|
define_method(method) do |uri, payload: nil, **options|
|
45
43
|
response = request(
|
46
44
|
method: method,
|
@@ -50,9 +48,9 @@ class ShafClient
|
|
50
48
|
)
|
51
49
|
|
52
50
|
body = String(response.body)
|
53
|
-
response.headers['content-type']
|
51
|
+
content_type = response.headers['content-type'] unless body.empty?
|
54
52
|
|
55
|
-
Resource.build(self, body, response.status, response.headers)
|
53
|
+
Resource.build(self, body, content_type, response.status, response.headers)
|
56
54
|
end
|
57
55
|
end
|
58
56
|
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shaf_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sammy Henningsson
|
@@ -30,7 +30,7 @@ cert_chain:
|
|
30
30
|
ZMhjYR7sRczGJx+GxGU2EaR0bjRsPVlC4ywtFxoOfRG3WaJcpWGEoAoMJX6Z0bRv
|
31
31
|
M40=
|
32
32
|
-----END CERTIFICATE-----
|
33
|
-
date: 2019-
|
33
|
+
date: 2019-11-01 00:00:00.000000000 Z
|
34
34
|
dependencies:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: faraday
|
@@ -110,12 +110,15 @@ files:
|
|
110
110
|
- lib/shaf_client/field.rb
|
111
111
|
- lib/shaf_client/form.rb
|
112
112
|
- lib/shaf_client/hal_form.rb
|
113
|
+
- lib/shaf_client/hypertext_cache_strategy.rb
|
113
114
|
- lib/shaf_client/link.rb
|
114
115
|
- lib/shaf_client/middleware/redirect.rb
|
116
|
+
- lib/shaf_client/mime_types.rb
|
115
117
|
- lib/shaf_client/resource.rb
|
116
118
|
- lib/shaf_client/resource_mapper.rb
|
117
119
|
- lib/shaf_client/shaf_form.rb
|
118
120
|
- lib/shaf_client/unknown_resource.rb
|
121
|
+
- lib/shaf_client/version.rb
|
119
122
|
homepage: https://github.com/sammyhenningsson/shaf_client
|
120
123
|
licenses:
|
121
124
|
- MIT
|
metadata.gz.sig
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
��^���!�j��k\�
|
2
|
+
,�P������\?�z��[+�mbsE�\Y�Ԧ�&��7�M?��ZXv����u����C�H�]*܋�i��v��l��f�o�a G�_V�0/(��=!w����ht-3`�N
|