hal-client 3.3.2 → 3.4.2
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/lib/hal_client/version.rb +1 -1
- data/lib/hal_client.rb +46 -27
- data/spec/hal_client_spec.rb +16 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 74de1dba1a4d7fcb4b836da48535915b9694219b
|
4
|
+
data.tar.gz: 5e3ebfbb756110e9adaefff3e339e34532fdd982
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c30ea673a86d148156f8783afbdb05e663e5445910a30aedbcf67edd2775e66d9bb7fa46d4770e165f13607cdeb29b3db6c517d8cb27679aa97a3a4bd92d0f7f
|
7
|
+
data.tar.gz: 223ce6546a7d0adcc61ed5d1e0306fb5087b99ad15c1d9a8c6ceca5dfca5e91600f02027db42198f76240b238de9b06812311441b71c82731da1c2fa9deec526
|
data/lib/hal_client/version.rb
CHANGED
data/lib/hal_client.rb
CHANGED
@@ -24,10 +24,15 @@ class HalClient
|
|
24
24
|
# prepended to the `Accept` header field of each request.
|
25
25
|
# :content_type - a single content type that should be
|
26
26
|
# prepended to the `Content-Type` header field of each request.
|
27
|
+
# :authorization - a `#call`able which takes the url being
|
28
|
+
# requested and returns the authorization header value to use
|
29
|
+
# for the request or a string which will always be the value of
|
30
|
+
# the authorization header
|
27
31
|
# :headers - a hash of other headers to send on each request.
|
28
32
|
def initialize(options={})
|
29
33
|
@default_message_request_headers = HTTP::Headers.new
|
30
34
|
@default_entity_request_headers = HTTP::Headers.new
|
35
|
+
@auth_helper = as_callable(options.fetch(:authorization, NullAuthHelper))
|
31
36
|
|
32
37
|
default_message_request_headers.set('Accept', options[:accept]) if
|
33
38
|
options[:accept]
|
@@ -63,57 +68,71 @@ class HalClient
|
|
63
68
|
# url - The URL of the resource of interest.
|
64
69
|
# headers - custom header fields to use for this request
|
65
70
|
def get(url, headers={})
|
71
|
+
headers = auth_headers(url).merge(headers)
|
66
72
|
interpret_response client_for_get(override_headers: headers).get(url)
|
67
73
|
end
|
68
74
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
75
|
+
class << self
|
76
|
+
protected
|
77
|
+
|
78
|
+
def def_unsafe_request(method)
|
79
|
+
define_method(method) do |url, data, headers={}|
|
80
|
+
headers = auth_headers(url).merge(headers)
|
81
|
+
|
82
|
+
req_body = if data.respond_to? :to_hal
|
76
83
|
data.to_hal
|
77
84
|
else
|
78
85
|
data
|
79
86
|
end
|
80
87
|
|
81
|
-
|
88
|
+
interpret_response client_for_post(override_headers: headers)
|
89
|
+
.request(method, url, body: req_body)
|
90
|
+
end
|
91
|
+
end
|
82
92
|
end
|
83
93
|
|
84
|
-
#
|
94
|
+
# Post a `Representation` or `String` to the resource identified at `url`.
|
85
95
|
#
|
86
96
|
# url - The URL of the resource of interest.
|
87
97
|
# data - a `String` or an object that responds to `#to_hal`
|
88
98
|
# headers - custom header fields to use for this request
|
89
|
-
|
90
|
-
req_body = if data.respond_to? :to_hal
|
91
|
-
data.to_hal
|
92
|
-
else
|
93
|
-
data
|
94
|
-
end
|
99
|
+
def_unsafe_request :post
|
95
100
|
|
96
|
-
|
97
|
-
|
101
|
+
# Put a `Representation` or `String` to the resource identified at `url`.
|
102
|
+
#
|
103
|
+
# url - The URL of the resource of interest.
|
104
|
+
# data - a `String` or an object that responds to `#to_hal`
|
105
|
+
# headers - custom header fields to use for this request
|
106
|
+
def_unsafe_request :put
|
98
107
|
|
99
108
|
# Patch a `Representation` or `String` to the resource identified at `url`.
|
100
109
|
#
|
101
110
|
# url - The URL of the resource of interest.
|
102
111
|
# data - a `String` or an object that responds to `#to_hal`
|
103
112
|
# headers - custom header fields to use for this request
|
104
|
-
|
105
|
-
req_body = if data.respond_to? :to_hal
|
106
|
-
data.to_hal
|
107
|
-
else
|
108
|
-
data
|
109
|
-
end
|
110
|
-
|
111
|
-
interpret_response client_for_post(override_headers: headers).patch(url, body: req_body)
|
112
|
-
end
|
113
|
+
def_unsafe_request :patch
|
113
114
|
|
114
115
|
protected
|
115
116
|
|
116
|
-
attr_reader :headers
|
117
|
+
attr_reader :headers, :auth_helper
|
118
|
+
|
119
|
+
NullAuthHelper = ->(_url) { nil }
|
120
|
+
|
121
|
+
def as_callable(thing)
|
122
|
+
if thing.respond_to?(:call)
|
123
|
+
thing
|
124
|
+
else
|
125
|
+
->(*_args) { thing }
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def auth_headers(url)
|
130
|
+
if h_val = auth_helper.call(url)
|
131
|
+
{"Authorization" => h_val}
|
132
|
+
else
|
133
|
+
{}
|
134
|
+
end
|
135
|
+
end
|
117
136
|
|
118
137
|
def interpret_response(resp)
|
119
138
|
case resp.status
|
data/spec/hal_client_spec.rb
CHANGED
@@ -47,6 +47,22 @@ describe HalClient do
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
+
context "explicit authorization helper" do
|
51
|
+
subject(:client) { HalClient.new authorization: ->(_url) { "Bearer hello" } }
|
52
|
+
it "sends specified accept header" do
|
53
|
+
expect(request.with(headers: {'Authorization' => "Bearer hello"})).
|
54
|
+
to have_been_made
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "explicit authorization string" do
|
59
|
+
subject(:client) { HalClient.new authorization: "Bearer hello" }
|
60
|
+
it "sends specified accept header" do
|
61
|
+
expect(request.with(headers: {'Authorization' => "Bearer hello"})).
|
62
|
+
to have_been_made
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
50
66
|
context "other headers" do
|
51
67
|
let(:headers) { {"Authorization" => "Bearer f73c04b0970f1deb6005fab53edd1708"} }
|
52
68
|
subject(:client) { HalClient.new headers: headers }
|