ocean-rails 2.6.2 → 2.7.0

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: 70bf4729704338768d5f743d77131a57a3b5f727
4
- data.tar.gz: 230ebe6ac4905c2ee3439fd2adc04a7561779847
3
+ metadata.gz: b145db3805039a6cc1d38e79ca8082fff55b1cf9
4
+ data.tar.gz: f419dd31a14e6ee523304489213dc61cda381647
5
5
  SHA512:
6
- metadata.gz: 679ce12c6732acf220721832b9ce61b6688f87533b9607f83c268a7d2648d5488714d963283856edff99564547db87a64f7a9c4c90786ea2aa378e95a4ff5a9f
7
- data.tar.gz: edae51c49986ccf4e2533acad819fdbab3673e9baafb15c4b626c64f1e4a7958d23d10a7b70eff41239782c239d05190edd4b1f4f883e9e72bc77f44cf0c9cdf
6
+ metadata.gz: 74240a3d708dc16ec24d1da03607f11cbdb1f721f1f49be388bb0ea5c23846e0145b6331bd7f54b8fae088f0b0337460afedec2022748d97466ad65f07e201a8
7
+ data.tar.gz: 351a73dc913d1570f6beefaafba47060e672369bec796ede8be44c222c5357ae5cd4e2c5f5bbae663193db6bccb8734fad6c6ceab9f4bc01da850c10b611bdf7
data/lib/ocean/api.rb CHANGED
@@ -1,30 +1,3 @@
1
- #
2
- # We need to monkey-patch Faraday to pull off PURGE and BAN
3
- #
4
- require 'faraday'
5
- require 'faraday_middleware'
6
-
7
- module Faraday #:nodoc: all
8
- class Connection
9
-
10
- METHODS << :purge
11
- METHODS << :ban
12
-
13
- # purge/ban(url, params, headers)
14
- %w[purge ban].each do |method|
15
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
16
- def #{method}(url = nil, params = nil, headers = nil)
17
- run_request(:#{method}, url, nil, headers) { |request|
18
- request.params.update(params) if params
19
- yield request if block_given?
20
- }
21
- end
22
- RUBY
23
- end
24
-
25
- end
26
- end
27
-
28
1
  require 'typhoeus'
29
2
 
30
3
  #
@@ -40,6 +13,7 @@ class Api
40
13
  API_VERSIONS[resource_name.to_s] || API_VERSIONS['_default']
41
14
  end
42
15
 
16
+
43
17
  #
44
18
  # Given that this service has authenticated successfully with the Auth service,
45
19
  # returns the token returned as part of the authentication response.
@@ -84,6 +58,10 @@ class Api
84
58
  end
85
59
 
86
60
 
61
+ #
62
+ # Api::Response instances wrap Typhoeus responses as an abstraction layer, but also
63
+ # in order to provide lazy evaluation of headers and JSON decoding of the body.
64
+ #
87
65
  class Response
88
66
 
89
67
  def initialize(response)
@@ -92,10 +70,16 @@ class Api
92
70
  @body = nil
93
71
  end
94
72
 
73
+ #
74
+ # The status code of the HTTP response.
75
+ #
95
76
  def status
96
77
  @response.response_code
97
78
  end
98
79
 
80
+ #
81
+ # Returns a hash of HTTP response headers.
82
+ #
99
83
  def headers
100
84
  @headers ||= (@response.response_headers || "").split("\r\n").inject({}) do |acc, h|
101
85
  k, v = h.split(": ")
@@ -104,52 +88,75 @@ class Api
104
88
  end
105
89
  end
106
90
 
91
+ #
92
+ # Returns the HTTP response body parsed from JSON. This is done lazily and only once.
93
+ #
107
94
  def body
108
95
  @body ||= @response.response_body.blank? ? nil : JSON.parse(@response.response_body)
109
96
  end
110
97
 
98
+ #
99
+ # Returns true if the HTTP request timed out.
100
+ #
101
+ def timed_out?
102
+ @response.timed_out?
103
+ end
111
104
  end
112
105
 
113
106
 
114
107
  #
115
- # Makes a HTTP request to +host_url+ using the HTTP method +method+. The +resource_name+
108
+ # Api.request is designed to be the lowest common denominator for making any kind of
109
+ # HTTP request, except parallel requests which will have a similar method (to which BAN
110
+ # and PURGE requests to Varnish is a special case).
111
+ #
112
+ # In its present form it assumes the request is a JSON one. Eventually, keyword args will
113
+ # provide abstract control of content type.
114
+ #
115
+ # +url+ is the URL to which the request will be made.
116
+ # +http_method+ is the HTTP method to use (:post, :get, :head, :put, :delete, etc).
117
+ # +args+, if given, should be a hash of query arguments to add to the URL.
118
+ # +headers+, if given, is a hash of extra HTTP headers for the request.
119
+ # +body+, if given, is the body of the request (:post, :put) as a string.
120
+ #
121
+ # Api.request won't raise an exception if the request times out or the operation fails.
122
+ # Instead, all such information is encapsulated in the Api::Response instance returned.
123
+ #
124
+ def self.request(url, http_method, args: nil, headers: {}, body: nil)
125
+ # Set up the request
126
+ headers['Accept'] = "application/json"
127
+ headers['Content-Type'] = "application/json"
128
+ headers['User-Agent'] = "Ocean"
129
+ request = Typhoeus::Request.new(url, method: http_method, headers: headers,
130
+ params: args, body: body)
131
+ # Run it
132
+ response = request.run
133
+ # Retries would be done here
134
+
135
+ # Return the response
136
+ Response.new(response)
137
+ end
138
+
139
+
140
+ #
141
+ # Makes an internal HTTP request to +host_url+ using the HTTP method +method+. The +resource_name+
116
142
  # is used to obtain the latest version string of the resource. The arg +path+ is the
117
143
  # local path, +args+ is a hash of query args, and +headers+ a hash of extra HTTP headers.
118
144
  #
119
145
  # Returns the response in its entirety so that the caller can examine its status and body.
120
146
  #
121
-
122
- def self.call(host_url, http_method, resource_name, path, args={}, headers={})
123
- # Set up the connection parameters
124
- conn = Faraday.new(host_url) do |c|
125
- c.response :json, :content_type => /\bjson$/ # Convert the response body to JSON
126
- c.adapter Faraday.default_adapter # Use net-http
127
- end
128
- api_version = version_for resource_name
129
- path = "/#{api_version}#{path}"
130
- # Make the call. TODO: retries?
131
- response = conn.send(http_method, path, args, headers) do |request|
132
- request.headers['Accept'] = 'application/json'
133
- request.headers['Content-Type'] = 'application/json'
147
+ # It is quite probable that this method will become deprecated. The main reason for its existence
148
+ # is the version computation, nothing more, and separating host and path isn't ideal.
149
+ #
150
+ def self.call(host_url, http_method, resource_name, path, args=nil, headers={})
151
+ if [:post, :put].include? http_method
152
+ request "#{host_url}/#{version_for resource_name}#{path}", http_method,
153
+ headers: headers, body: args
154
+ else
155
+ request "#{host_url}/#{version_for resource_name}#{path}", http_method,
156
+ headers: headers, args: args
134
157
  end
135
- response
136
158
  end
137
159
 
138
- # def self.call(host_url, http_method, resource_name, path, args_or_body={}, headers={})
139
- # # Set up the request
140
- # url = "#{host_url}/#{version_for resource_name}#{path}"
141
- # headers['Accept'] = "application/json"
142
- # headers['Content-Type'] = "application/json"
143
- # headers['User-Agent'] = "Ocean"
144
- # request = Typhoeus::Request.new(url, method: http_method, headers: headers,
145
- # params: args_or_body.is_a?(String) ? nil : args_or_body,
146
- # body: args_or_body.is_a?(String) ? args_or_body : nil
147
- # )
148
- # # Run it
149
- # response = request.run
150
- # # Return the response
151
- # Response.new(response)
152
- # end
153
160
 
154
161
  #
155
162
  # Convenience method to make an internal +GET+ request to the Ocean Api. The +resource_name+
@@ -191,6 +198,7 @@ class Api
191
198
  #
192
199
  def self.delete(*args) call(INTERNAL_OCEAN_API_URL, :delete, *args); end
193
200
 
201
+
194
202
  #
195
203
  # Makes an internal +PURGE+ call to all Varnish instances. The call is made in parallel.
196
204
  # Varnish will only accept +PURGE+ requests coming from the local network.
@@ -205,6 +213,7 @@ class Api
205
213
  hydra.run
206
214
  end
207
215
 
216
+
208
217
  #
209
218
  # Makes an internal +BAN+ call to all Varnish instances. The call is made in parallel.
210
219
  # Varnish will only accept +BAN+ requests coming from the local network.
@@ -265,6 +274,7 @@ class Api
265
274
  ::Base64.strict_encode64 "#{username}:#{password}"
266
275
  end
267
276
 
277
+
268
278
  #
269
279
  # Takes encoded credentials (e.g. by Api.encode_credentials) and returns a two-element array
270
280
  # where the first element is the username and the second is the password. If the encoded
@@ -280,6 +290,7 @@ class Api
280
290
  [username || "", password || ""]
281
291
  end
282
292
 
293
+
283
294
  #
284
295
  # Performs authorisation against the Auth service. The +token+ must be a token received as a
285
296
  # result of a prior authentication operation. The args should be in the form
@@ -334,6 +345,7 @@ class Api
334
345
  'disconnect' => ['connect', 'DELETE']
335
346
  }
336
347
 
348
+
337
349
  #
338
350
  # Returns the hyperlink and HTTP method to use for an +action+ in a certain +controller+.
339
351
  # First, the +DEFAULT_ACTIONS+ are searched, then any extra actions defined for the
data/lib/ocean/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ocean
2
- VERSION = "2.6.2"
2
+ VERSION = "2.7.0"
3
3
  end
@@ -92,22 +92,27 @@ class ZeromqLogger
92
92
 
93
93
 
94
94
  def debug(*args)
95
+ return if args.blank?
95
96
  add 0, *args
96
97
  end
97
98
 
98
99
  def info(*args)
100
+ return if args.blank?
99
101
  add 1, *args
100
102
  end
101
103
 
102
104
  def warn(*args)
105
+ return if args.blank?
103
106
  add 2, *args
104
107
  end
105
108
 
106
109
  def error(*args)
110
+ return if args.blank?
107
111
  add 3, *args
108
112
  end
109
113
 
110
114
  def fatal(*args)
115
+ return if args.blank?
111
116
  add 4, *args
112
117
  end
113
118
 
metadata CHANGED
@@ -1,43 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ocean-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.2
4
+ version: 2.7.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-14 00:00:00.000000000 Z
11
+ date: 2014-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: faraday
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: faraday_middleware
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
13
  - !ruby/object:Gem::Dependency
42
14
  name: typhoeus
43
15
  requirement: !ruby/object:Gem::Requirement