ocean-rails 2.9.0 → 2.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/helpers/application_helper.rb +6 -2
- data/lib/ocean/api.rb +88 -25
- data/lib/ocean/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 199cd45800bf7316cb1bb1b79f622ee1c6a74556
|
4
|
+
data.tar.gz: 8865f5b42851598a6b02ae921e62e42487d6ce9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7db74a25a4eeddb2b8ce989ab9725c211a272c4353aeabc4b3099ee55e7f7525a8af5f6b9abcd3f23164b549a1309163ddc829a572c10874c094ed1d0440a27d
|
7
|
+
data.tar.gz: 415353c47a9a3a8a09e4307262a1952f0dc7fcbf8a909145a1bd566bde5478c9500eea6ad6e43d689a0ed795ea2055376c682211e5b2923065c63a4e06ec7f55
|
@@ -33,13 +33,17 @@ module ApplicationHelper
|
|
33
33
|
|
34
34
|
|
35
35
|
#
|
36
|
-
# View helper predicates to determine
|
37
|
-
# to one or more of a list of Groups.
|
36
|
+
# View helper predicates to determine if the ApiUser behind the current
|
37
|
+
# authorisation belongs to one or more of a list of Groups.
|
38
38
|
#
|
39
39
|
def member_of_group?(*names)
|
40
40
|
@group_names && @group_names.intersect?(names.to_set)
|
41
41
|
end
|
42
42
|
|
43
|
+
#
|
44
|
+
# Returns true if the ApiUser behind the current authorisation belongs
|
45
|
+
# to the Ocean Group "Superusers".
|
46
|
+
#
|
43
47
|
def superuser?
|
44
48
|
member_of_group?("Superusers")
|
45
49
|
end
|
data/lib/ocean/api.rb
CHANGED
@@ -14,15 +14,6 @@ class Api
|
|
14
14
|
end
|
15
15
|
|
16
16
|
|
17
|
-
#
|
18
|
-
# Given that this service has authenticated successfully with the Auth service,
|
19
|
-
# returns the token returned as part of the authentication response.
|
20
|
-
#
|
21
|
-
def self.token
|
22
|
-
@token
|
23
|
-
end
|
24
|
-
|
25
|
-
|
26
17
|
#
|
27
18
|
# Adds environment info to the basename, so that testing and execution in various combinations
|
28
19
|
# of the Rails env and the Chef environment can be done without collision.
|
@@ -77,6 +68,13 @@ class Api
|
|
77
68
|
@response.response_code
|
78
69
|
end
|
79
70
|
|
71
|
+
#
|
72
|
+
# The status message of the HTTP response.
|
73
|
+
#
|
74
|
+
def message
|
75
|
+
@response.status_message
|
76
|
+
end
|
77
|
+
|
80
78
|
#
|
81
79
|
# Returns a hash of HTTP response headers.
|
82
80
|
#
|
@@ -95,12 +93,26 @@ class Api
|
|
95
93
|
@body ||= @response.response_body.blank? ? nil : JSON.parse(@response.response_body)
|
96
94
|
end
|
97
95
|
|
96
|
+
#
|
97
|
+
# Returns true if the HTTP request was a success and status == 2xx.
|
98
|
+
#
|
99
|
+
def success?
|
100
|
+
@response.success?
|
101
|
+
end
|
102
|
+
|
98
103
|
#
|
99
104
|
# Returns true if the HTTP request timed out.
|
100
105
|
#
|
101
106
|
def timed_out?
|
102
107
|
@response.timed_out?
|
103
108
|
end
|
109
|
+
|
110
|
+
#
|
111
|
+
# Returns true if the HTTP response was a success and not a 304.
|
112
|
+
#
|
113
|
+
def modified?
|
114
|
+
@response.modified?
|
115
|
+
end
|
104
116
|
end
|
105
117
|
|
106
118
|
|
@@ -117,26 +129,48 @@ class Api
|
|
117
129
|
# +args+, if given, should be a hash of query arguments to add to the URL.
|
118
130
|
# +headers+, if given, is a hash of extra HTTP headers for the request.
|
119
131
|
# +body+, if given, is the body of the request (:post, :put) as a string.
|
132
|
+
# +x_api_token+, if given, is a string which will be used as an X-API-Token header.
|
120
133
|
#
|
121
|
-
|
122
|
-
|
123
|
-
#
|
124
|
-
def self.request(url, http_method, args: nil, headers: {}, body: nil)
|
134
|
+
def self.request(url, http_method, args: nil, headers: {}, body: nil,
|
135
|
+
x_api_token: nil)
|
125
136
|
# Set up the request
|
126
137
|
headers['Accept'] = "application/json"
|
127
138
|
headers['Content-Type'] = "application/json"
|
128
139
|
headers['User-Agent'] = "Ocean"
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
140
|
+
headers['X-API-Token'] = x_api_token if x_api_token
|
141
|
+
|
142
|
+
while (true) do
|
143
|
+
request = Typhoeus::Request.new(url, method: http_method, headers: headers,
|
144
|
+
params: args, body: body)
|
145
|
+
# Run it
|
146
|
+
response = Response.new(request.run)
|
147
|
+
# If successful, return
|
148
|
+
return response if response.success?
|
149
|
+
|
150
|
+
# Not successful, deal with it
|
151
|
+
if response.timed_out?
|
152
|
+
raise Api::TimeoutError, "Api.request timed out"
|
153
|
+
elsif response.status == 0
|
154
|
+
# Could not get an http response, something's wrong.
|
155
|
+
raise Api::NoResponseError, "Api.request could not obtain a response"
|
156
|
+
elsif [400, 419].include?(response.status) && x_api_token.present?
|
157
|
+
# Re-authenticate and retry
|
158
|
+
Api.reset_service_token
|
159
|
+
headers['X-API-Token'] = Api.service_token
|
160
|
+
x_api_token = false # This prevents us from ending up here twice
|
161
|
+
else
|
162
|
+
# Failed
|
163
|
+
break
|
164
|
+
end
|
165
|
+
end
|
166
|
+
# Return the wrapped response to the failed request
|
167
|
+
response
|
137
168
|
end
|
138
169
|
|
139
170
|
|
171
|
+
class TimeoutError < StandardError; end
|
172
|
+
class NoResponseError < StandardError; end
|
173
|
+
|
140
174
|
#
|
141
175
|
# Makes an internal HTTP request to +host_url+ using the HTTP method +method+. The +resource_name+
|
142
176
|
# is used to obtain the latest version string of the resource. The arg +path+ is the
|
@@ -237,18 +271,47 @@ class Api
|
|
237
271
|
URI.escape(path, Regexp.new("[^/$\\-+_.!~*'()a-zA-Z0-9]"))
|
238
272
|
end
|
239
273
|
|
274
|
+
|
275
|
+
|
276
|
+
#
|
277
|
+
# Given that this service has authenticated successfully with the Auth service,
|
278
|
+
# returns the token returned as part of the authentication response.
|
279
|
+
# NB: This method is deprecated.
|
280
|
+
#
|
281
|
+
def self.token
|
282
|
+
ActiveSupport::Deprecation.warn "Api.token is deprecated, use Api.service_token instead and skip the explicit call to Api.authenticate.", caller
|
283
|
+
service_token
|
284
|
+
end
|
285
|
+
|
286
|
+
#
|
287
|
+
# This method returns the current token. If no current token has been obtained,
|
288
|
+
# authenticates.
|
289
|
+
#
|
290
|
+
def self.service_token
|
291
|
+
@service_token ||= authenticate
|
292
|
+
end
|
293
|
+
|
294
|
+
#
|
295
|
+
# Resets the service token, causing the next call to Api.service_token to
|
296
|
+
# re-authenticate.
|
297
|
+
#
|
298
|
+
def self.reset_service_token
|
299
|
+
@service_token = nil
|
300
|
+
end
|
301
|
+
|
240
302
|
|
241
303
|
#
|
242
304
|
# Authenticates against the Auth service (which must be deployed and running) with
|
243
|
-
# a given +username+ and +password+. If successful, the authentication token is returned.
|
244
|
-
# token is also assigned to the instance variable @
|
305
|
+
# a given +username+ and +password+. If successful, the authentication token is returned.
|
306
|
+
# The token is also assigned to the instance variable @service_token.
|
307
|
+
# If not successful, +nil+ is returned.
|
245
308
|
#
|
246
309
|
def self.authenticate(username=API_USER, password=API_PASSWORD)
|
247
310
|
response = Api.post(:auth, "/authentications", nil,
|
248
311
|
{'X-API-Authenticate' => encode_credentials(username, password)})
|
249
312
|
case response.status
|
250
313
|
when 201
|
251
|
-
@
|
314
|
+
@service_token = response.body['authentication']['token']
|
252
315
|
when 400
|
253
316
|
# Malformed credentials. Don't repeat the request.
|
254
317
|
nil
|
@@ -299,7 +362,7 @@ class Api
|
|
299
362
|
#
|
300
363
|
# e.g.
|
301
364
|
#
|
302
|
-
# Api.permitted?(@
|
365
|
+
# Api.permitted?(@service_token, query: "cms:texts:self:GET:*:*")
|
303
366
|
#
|
304
367
|
# Api.authorization_string can be used to produce the query string.
|
305
368
|
#
|
data/lib/ocean/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ocean-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Bengtson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: typhoeus
|