hal-client 3.3.2 → 3.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 454803feafd3de62522b145857cf25b91bceea18
4
- data.tar.gz: 279121c58f7ebc9456d2b8575a342d9069346883
3
+ metadata.gz: 74de1dba1a4d7fcb4b836da48535915b9694219b
4
+ data.tar.gz: 5e3ebfbb756110e9adaefff3e339e34532fdd982
5
5
  SHA512:
6
- metadata.gz: c4591e0f1bbfc67522893bf2e79781d40ad765030e8cefb9a3bdfddf9d5c1146107ffbdc8dbb401914a0abf3404b0ab75aacc64f35a15996c5b1fc558fb732f9
7
- data.tar.gz: 1a12ff06dea5247600ddd62b8799b0e00f130b07088aee4de2f5d3507dd48bf9173d72b0b79f2f9ab6f5fdec307f241e7a33647e471791c9dec84a5c5693955c
6
+ metadata.gz: c30ea673a86d148156f8783afbdb05e663e5445910a30aedbcf67edd2775e66d9bb7fa46d4770e165f13607cdeb29b3db6c517d8cb27679aa97a3a4bd92d0f7f
7
+ data.tar.gz: 223ce6546a7d0adcc61ed5d1e0306fb5087b99ad15c1d9a8c6ceca5dfca5e91600f02027db42198f76240b238de9b06812311441b71c82731da1c2fa9deec526
@@ -1,3 +1,3 @@
1
1
  class HalClient
2
- VERSION = "3.3.2"
2
+ VERSION = "3.4.2"
3
3
  end
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
- # Post a `Representation` or `String` to the resource identified at `url`.
70
- #
71
- # url - The URL of the resource of interest.
72
- # data - a `String` or an object that responds to `#to_hal`
73
- # headers - custom header fields to use for this request
74
- def post(url, data, headers={})
75
- req_body = if data.respond_to? :to_hal
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
- interpret_response client_for_post(override_headers: headers).post(url, body: req_body)
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
- # Put a `Representation` or `String` to the resource identified at `url`.
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
- def put(url, data, headers={})
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
- interpret_response client_for_post(override_headers: headers).put(url, body: req_body)
97
- end
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
- def patch(url, data, headers={})
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
@@ -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 }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hal-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.3.2
4
+ version: 3.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Williams