shaf_client 0.1.1 → 0.2.0
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
- checksums.yaml.gz.sig +0 -0
- data/lib/shaf_client/base_resource.rb +22 -12
- data/lib/shaf_client/form.rb +15 -1
- data/lib/shaf_client/link.rb +2 -2
- data/lib/shaf_client/middleware/cache.rb +10 -5
- data/lib/shaf_client/middleware/redirect.rb +35 -0
- data/lib/shaf_client/resource.rb +18 -4
- data/lib/shaf_client.rb +24 -18
- data.tar.gz.sig +0 -0
- metadata +3 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c22649fc5b9cf64045bfe2a8d00c7ee333875b908683f59371b111e23baa6e68
|
4
|
+
data.tar.gz: ea3690f1f041619f9d1f6491ac21784fdc5bf31b5ee496c80d2fe3bd3195e9b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c79cfd0da579e74134d9c01a5745f0f69b8e9dcc72532fcd88bbd3ca3c82410c48a0e61ee41923becbdcef72d67e2af551d00760be1521534494f331634f19d2
|
7
|
+
data.tar.gz: 748c0dee85126f1bda4db6bffbe338ff4b1d71ea50b02e4bb3779cd9479cc963dadeeefc62c3c5e3d09911b57d42b4cfab77de4d18416fd8194a5fedf6ea0bb6
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
@@ -17,11 +17,14 @@ class ShafClient
|
|
17
17
|
parse
|
18
18
|
end
|
19
19
|
|
20
|
-
def
|
20
|
+
def to_h
|
21
21
|
attributes
|
22
22
|
.merge(_links: transform_values_to_s(links))
|
23
23
|
.merge(_embedded: transform_values_to_s(embedded_resources))
|
24
|
-
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_s
|
27
|
+
JSON.pretty_generate(to_h)
|
25
28
|
end
|
26
29
|
|
27
30
|
def attribute(key)
|
@@ -48,12 +51,23 @@ class ShafClient
|
|
48
51
|
links.keys
|
49
52
|
end
|
50
53
|
|
51
|
-
|
54
|
+
protected
|
52
55
|
|
53
56
|
def payload
|
54
57
|
@payload ||= {}
|
55
58
|
end
|
56
59
|
|
60
|
+
def <<(other)
|
61
|
+
@payload = other.payload.dup
|
62
|
+
@attributes = other.attributes.dup
|
63
|
+
@links = other.links.dup
|
64
|
+
@curies = other.curies.dup
|
65
|
+
@embedded_resources = other.embedded_resources.dup
|
66
|
+
self
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
57
71
|
def parse
|
58
72
|
@attributes = payload.transform_keys(&:to_sym)
|
59
73
|
parse_links
|
@@ -61,11 +75,10 @@ class ShafClient
|
|
61
75
|
end
|
62
76
|
|
63
77
|
def parse_links
|
64
|
-
links = attributes.delete(:_links) || {}
|
65
78
|
@links ||= {}
|
66
79
|
@curies ||= {}
|
67
80
|
|
68
|
-
|
81
|
+
(attributes.delete(:_links) || {}).each do |key, value|
|
69
82
|
next parse_curies(value) if key == 'curies'
|
70
83
|
@links[key.to_sym] = Link.from(value)
|
71
84
|
end
|
@@ -93,11 +106,8 @@ class ShafClient
|
|
93
106
|
end
|
94
107
|
|
95
108
|
def method_missing(method_name, *args, &block)
|
96
|
-
|
97
|
-
|
98
|
-
else
|
99
|
-
super
|
100
|
-
end
|
109
|
+
return super unless attributes.key?(method_name)
|
110
|
+
attribute(method_name)
|
101
111
|
end
|
102
112
|
|
103
113
|
def respond_to_missing?(method_name, include_private = false)
|
@@ -108,9 +118,9 @@ class ShafClient
|
|
108
118
|
def transform_values_to_s(hash)
|
109
119
|
hash.transform_values do |value|
|
110
120
|
if value.is_a? Array
|
111
|
-
value.map(&:
|
121
|
+
value.map(&:to_h)
|
112
122
|
else
|
113
|
-
value.
|
123
|
+
value.to_h
|
114
124
|
end
|
115
125
|
end
|
116
126
|
end
|
data/lib/shaf_client/form.rb
CHANGED
@@ -31,7 +31,21 @@ class ShafClient
|
|
31
31
|
# def validate; end
|
32
32
|
|
33
33
|
def submit
|
34
|
-
client.send(http_method, target, @values)
|
34
|
+
client.send(http_method, target, payload: @values)
|
35
|
+
end
|
36
|
+
|
37
|
+
def reload!
|
38
|
+
self << get_form(:self, skip_cache: true)
|
39
|
+
end
|
40
|
+
|
41
|
+
protected
|
42
|
+
|
43
|
+
def <<(other)
|
44
|
+
@values = {}
|
45
|
+
other.values.each do |key, value|
|
46
|
+
@values[key] = value.dup
|
47
|
+
end
|
48
|
+
super
|
35
49
|
end
|
36
50
|
end
|
37
51
|
end
|
data/lib/shaf_client/link.rb
CHANGED
@@ -122,9 +122,11 @@ class ShafClient
|
|
122
122
|
end
|
123
123
|
|
124
124
|
def call(request_env)
|
125
|
-
key = cache_key(request_env)
|
125
|
+
key = cache_key(request_env[:url], request_env[:request_headers])
|
126
126
|
|
127
|
-
|
127
|
+
skip_cache = request_env[:request_headers].delete :skip_cache
|
128
|
+
|
129
|
+
if !skip_cache && request_env[:method] == :get
|
128
130
|
cached = self.class.get(key: key)
|
129
131
|
return Response.new(body: cached, headers: {}) if cached
|
130
132
|
end
|
@@ -132,6 +134,8 @@ class ShafClient
|
|
132
134
|
add_etag(request_env, key)
|
133
135
|
|
134
136
|
@app.call(request_env).on_complete do |response_env|
|
137
|
+
# key might have changed in other middleware
|
138
|
+
key = cache_key(response_env[:url], request_env[:request_headers])
|
135
139
|
add_cached_payload(response_env, key)
|
136
140
|
cache_response(response_env, key)
|
137
141
|
self.class.inc_request_count
|
@@ -139,7 +143,8 @@ class ShafClient
|
|
139
143
|
end
|
140
144
|
|
141
145
|
def add_etag(env, key = nil)
|
142
|
-
|
146
|
+
return unless %i[get head].include? env[:method]
|
147
|
+
key ||= cache_key(env[:url], env[:request_headers])
|
143
148
|
etag = self.class.get_etag(key: key)
|
144
149
|
env[:request_headers]['If-None-Match'] = etag if etag
|
145
150
|
end
|
@@ -162,8 +167,8 @@ class ShafClient
|
|
162
167
|
)
|
163
168
|
end
|
164
169
|
|
165
|
-
def cache_key(
|
166
|
-
:"#{
|
170
|
+
def cache_key(url, request_headers)
|
171
|
+
:"#{url}.#{request_headers&.dig(auth_header)}"
|
167
172
|
end
|
168
173
|
|
169
174
|
def auth_header
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
class ShafClient
|
4
|
+
module Middleware
|
5
|
+
class Redirect
|
6
|
+
|
7
|
+
def initialize(app)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(request_env)
|
12
|
+
@app.call(request_env).on_complete do |response_env|
|
13
|
+
status = response_env[:status]
|
14
|
+
location = response_env[:response_headers]['Location']
|
15
|
+
next unless redirect? status
|
16
|
+
next unless location
|
17
|
+
update_env(request_env, status, location)
|
18
|
+
@app.call(request_env)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def redirect?(status)
|
23
|
+
[301, 302, 303, 307, 308].include? status
|
24
|
+
end
|
25
|
+
|
26
|
+
def update_env(request_env, status, location)
|
27
|
+
if status == 303
|
28
|
+
request_env[:method] = :get
|
29
|
+
request_env[:body] = nil
|
30
|
+
end
|
31
|
+
request_env[:url] = URI(location)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/shaf_client/resource.rb
CHANGED
@@ -3,18 +3,20 @@ require 'shaf_client/base_resource'
|
|
3
3
|
|
4
4
|
class ShafClient
|
5
5
|
class Resource < BaseResource
|
6
|
+
attr_reader :http_status, :headers
|
6
7
|
|
7
|
-
def initialize(client, payload)
|
8
|
+
def initialize(client, payload, status = nil, headers = nil)
|
8
9
|
@client = client
|
10
|
+
@http_status = status
|
11
|
+
@headers = headers
|
9
12
|
super(payload)
|
10
13
|
end
|
11
14
|
|
12
15
|
%i[get put post delete patch get_form].each do |method|
|
13
|
-
define_method(method) do |rel, payload = nil|
|
16
|
+
define_method(method) do |rel, payload = nil, **options|
|
14
17
|
href = link(rel).href
|
15
18
|
args = [method, href]
|
16
|
-
args
|
17
|
-
client.send(*args)
|
19
|
+
client.send(*args, payload: payload, **options)
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
@@ -32,6 +34,18 @@ class ShafClient
|
|
32
34
|
client.get_doc(uri)
|
33
35
|
end
|
34
36
|
|
37
|
+
def reload!
|
38
|
+
self << get(:self, skip_cache: true)
|
39
|
+
end
|
40
|
+
|
41
|
+
protected
|
42
|
+
|
43
|
+
def <<(other)
|
44
|
+
@http_status = other.http_status.dup
|
45
|
+
@headers = other.headers.dup
|
46
|
+
super
|
47
|
+
end
|
48
|
+
|
35
49
|
private
|
36
50
|
|
37
51
|
attr_reader :client
|
data/lib/shaf_client.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'faraday'
|
2
4
|
require 'json'
|
3
5
|
require 'shaf_client/middleware/cache'
|
6
|
+
require 'shaf_client/middleware/redirect'
|
4
7
|
require 'shaf_client/resource'
|
5
8
|
require 'shaf_client/form'
|
6
9
|
|
@@ -17,29 +20,34 @@ class ShafClient
|
|
17
20
|
@client = Faraday.new(url: root_uri) do |conn|
|
18
21
|
conn.basic_auth(@user, @pass) if basic_auth?
|
19
22
|
conn.use Middleware::Cache, auth_header: auth_header
|
23
|
+
conn.use Middleware::Redirect
|
20
24
|
conn.adapter adapter
|
21
25
|
end
|
22
26
|
end
|
23
27
|
|
24
|
-
def get_root
|
25
|
-
get(@root_uri)
|
28
|
+
def get_root(**options)
|
29
|
+
get(@root_uri, **options)
|
26
30
|
end
|
27
31
|
|
28
|
-
def get_form(uri)
|
29
|
-
response = request(method: :get, uri: uri)
|
30
|
-
Form.new(self, response.body)
|
32
|
+
def get_form(uri, **options)
|
33
|
+
response = request(method: :get, uri: uri, opts: options)
|
34
|
+
Form.new(self, response.body, response.status, response.headers)
|
31
35
|
end
|
32
36
|
|
33
|
-
def get_doc(uri)
|
34
|
-
response = request(method: :get, uri: uri)
|
37
|
+
def get_doc(uri, **options)
|
38
|
+
response = request(method: :get, uri: uri, opts: options)
|
35
39
|
response&.body || ''
|
36
40
|
end
|
37
41
|
|
38
42
|
%i[get put post delete patch].each do |method|
|
39
|
-
define_method(method) do |uri, payload
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
+
define_method(method) do |uri, payload: nil, **options|
|
44
|
+
response = request(
|
45
|
+
method: method,
|
46
|
+
uri: uri,
|
47
|
+
payload: payload,
|
48
|
+
opts: options
|
49
|
+
)
|
50
|
+
Resource.new(self, response.body, response.status, response.headers)
|
43
51
|
end
|
44
52
|
end
|
45
53
|
|
@@ -71,17 +79,15 @@ class ShafClient
|
|
71
79
|
@user && @pass
|
72
80
|
end
|
73
81
|
|
74
|
-
def
|
75
|
-
response = yield
|
76
|
-
Resource.new(self, response.body)
|
77
|
-
end
|
78
|
-
|
79
|
-
def request(method:, uri:, payload: nil, headers: {})
|
82
|
+
def request(method:, uri:, payload: nil, opts: {})
|
80
83
|
payload = JSON.generate(payload) if payload&.is_a?(Hash)
|
84
|
+
headers = @default_headers.merge(opts.fetch(:headers, {}))
|
85
|
+
headers[:skip_cache] = true if opts[:skip_cache]
|
86
|
+
|
81
87
|
@client.send(method) do |req|
|
82
88
|
req.url uri
|
83
89
|
req.body = payload if payload
|
84
|
-
req.headers.merge!
|
90
|
+
req.headers.merge! headers
|
85
91
|
end
|
86
92
|
end
|
87
93
|
end
|
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.2.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: 2018-12-
|
33
|
+
date: 2018-12-18 00:00:00.000000000 Z
|
34
34
|
dependencies:
|
35
35
|
- !ruby/object:Gem::Dependency
|
36
36
|
name: faraday
|
@@ -92,6 +92,7 @@ files:
|
|
92
92
|
- lib/shaf_client/form.rb
|
93
93
|
- lib/shaf_client/link.rb
|
94
94
|
- lib/shaf_client/middleware/cache.rb
|
95
|
+
- lib/shaf_client/middleware/redirect.rb
|
95
96
|
- lib/shaf_client/resource.rb
|
96
97
|
homepage: https://github.com/sammyhenningsson/shaf_client
|
97
98
|
licenses:
|
metadata.gz.sig
CHANGED
Binary file
|