ocean-rails 2.5.0 → 2.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ocean/api.rb +95 -54
  3. data/lib/ocean/version.rb +1 -1
  4. metadata +2 -30
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dd17c75234b88f4b72f4d8b635abb238c21791c3
4
- data.tar.gz: ea63b031115d253f2f7f54c626bc87fec69926a2
3
+ metadata.gz: abf28d724166e842023309df785a57664db29c84
4
+ data.tar.gz: e36c67e240cfbecd146858221ba21ab8dfd3a950
5
5
  SHA512:
6
- metadata.gz: caa19c434c68eb565d9b04606daab2154aa92cb2b2af5b1c4381f2060da5f82e08c29a99c9dd540c088473ed43ebe887014866e2c8aa859ddb54eb63e8aee44b
7
- data.tar.gz: b1d93f16a85cab64dc4b34989dcde7708a207fdd619989e9e977fd9497f851f558444f1a32c512753cc9475a2b1fc7e440f5863e839e0010488a00a781b647ef
6
+ metadata.gz: a07c6ee6bb67c43f9f75a2ea7a8414e3455433b0925656478d271543782e32a0339217bc67a3af3f1dab3e3edae1f18857ebf325024ed2200309ce5227abf644
7
+ data.tar.gz: c2c2e990a31207d2919192c014f0408a89053f8da01ed49e344d1df0318329002f81ad38f607f48ceb6dced67f2857c28430f9bc51b96970e7f77bfc7f2b019f
data/lib/ocean/api.rb CHANGED
@@ -1,30 +1,29 @@
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
1
+ # #
2
+ # # We need to monkey-patch Faraday to pull off PURGE and BAN
3
+ # #
4
+ # require 'faraday'
5
+ # require 'faraday_middleware'
24
6
 
25
- end
26
- end
7
+ # module Faraday #:nodoc: all
8
+ # class Connection
9
+
10
+ # METHODS << :purge
11
+ # METHODS << :ban
27
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
28
27
 
29
28
  #
30
29
  # This class encapsulates all logic for calling other API services.
@@ -83,6 +82,33 @@ class Api
83
82
  end
84
83
 
85
84
 
85
+ class Response
86
+
87
+ def initialize(response)
88
+ @response = response
89
+ @headers = nil
90
+ @body = nil
91
+ end
92
+
93
+ def status
94
+ @response.response_code
95
+ end
96
+
97
+ def headers
98
+ @headers ||= (@response.response_headers || "").split("\r\n").inject({}) do |acc, h|
99
+ k, v = h.split(": ")
100
+ acc[k] = v
101
+ acc
102
+ end
103
+ end
104
+
105
+ def body
106
+ @body ||= @response.response_body.blank? ? nil : JSON.parse(@response.response_body)
107
+ end
108
+
109
+ end
110
+
111
+
86
112
  #
87
113
  # Makes a HTTP request to +host_url+ using the HTTP method +method+. The +resource_name+
88
114
  # is used to obtain the latest version string of the resource. The arg +path+ is the
@@ -90,22 +116,39 @@ class Api
90
116
  #
91
117
  # Returns the response in its entirety so that the caller can examine its status and body.
92
118
  #
93
- def self.call(host_url, http_method, resource_name, path, args={}, headers={})
94
- # Set up the connection parameters
95
- conn = Faraday.new(host_url) do |c|
96
- c.response :json, :content_type => /\bjson$/ # Convert the response body to JSON
97
- c.adapter Faraday.default_adapter # Use net-http
98
- end
99
- api_version = version_for resource_name
100
- path = "/#{api_version}#{path}"
101
- # Make the call. TODO: retries?
102
- response = conn.send(http_method, path, args, headers) do |request|
103
- request.headers['Accept'] = 'application/json'
104
- request.headers['Content-Type'] = 'application/json'
105
- end
106
- response
107
- end
108
-
119
+
120
+ # def self.call(host_url, http_method, resource_name, path, args={}, headers={})
121
+ # # Set up the connection parameters
122
+ # conn = Faraday.new(host_url) do |c|
123
+ # c.response :json, :content_type => /\bjson$/ # Convert the response body to JSON
124
+ # c.adapter Faraday.default_adapter # Use net-http
125
+ # end
126
+ # api_version = version_for resource_name
127
+ # path = "/#{api_version}#{path}"
128
+ # # Make the call. TODO: retries?
129
+ # response = conn.send(http_method, path, args, headers) do |request|
130
+ # request.headers['Accept'] = 'application/json'
131
+ # request.headers['Content-Type'] = 'application/json'
132
+ # end
133
+ # response
134
+ # end
135
+
136
+ def self.call(host_url, http_method, resource_name, path, args_or_body={}, headers={})
137
+ # Set up the request
138
+ url = "#{host_url}/#{version_for resource_name}#{path}"
139
+ headers['Accept'] = "application/json"
140
+ headers['Content-Type'] = "application/json"
141
+ headers['User-Agent'] = "Ocean"
142
+ request = Typhoeus::Request.new(url, method: http_method, headers: headers,
143
+ params: args_or_body.is_a?(String) ? nil : args_or_body,
144
+ body: args_or_body.is_a?(String) ? args_or_body : nil
145
+ )
146
+ # Run it
147
+ response = request.run
148
+ # Return the response
149
+ Response.new(response)
150
+ end
151
+
109
152
  #
110
153
  # Convenience method to make an internal +GET+ request to the Ocean Api. The +resource_name+
111
154
  # is used to obtain the latest version string of the resource. The arg +path+ is the
@@ -146,25 +189,18 @@ class Api
146
189
  #
147
190
  def self.delete(*args) call(INTERNAL_OCEAN_API_URL, :delete, *args); end
148
191
 
149
-
150
- #
151
- # Like Api.call, but makes the requests in parallel. (Parallel calls not implemented yet.)
152
- #
153
- def self.call_p(url, http_method, path, args={}, headers={})
154
- conn = Faraday.new(url) do |c|
155
- c.adapter Faraday.default_adapter # Use net-http
156
- end
157
- conn.send(http_method, path, args, headers)
158
- end
159
-
160
192
  #
161
193
  # Makes an internal +PURGE+ call to all Varnish instances. The call is made in parallel.
162
194
  # Varnish will only accept +PURGE+ requests coming from the local network.
163
195
  #
164
196
  def self.purge(*args)
197
+ hydra = Typhoeus::Hydra.hydra
165
198
  LOAD_BALANCERS.each do |host|
166
- call_p("http://#{host}", :purge, *args)
199
+ url = "http://#{host}#{path}"
200
+ request = Typhoeus::Request.new(url, method: :purge, headers: {})
201
+ hydra.queue request
167
202
  end
203
+ hydra.run
168
204
  end
169
205
 
170
206
  #
@@ -172,9 +208,14 @@ class Api
172
208
  # Varnish will only accept +BAN+ requests coming from the local network.
173
209
  #
174
210
  def self.ban(path)
211
+ hydra = Typhoeus::Hydra.hydra
212
+ escaped_path = escape(path)
175
213
  LOAD_BALANCERS.each do |host|
176
- call_p("http://#{host}", :ban, escape(path))
214
+ url = "http://#{host}#{escaped_path}"
215
+ request = Typhoeus::Request.new(url, method: :ban, headers: {})
216
+ hydra.queue request
177
217
  end
218
+ hydra.run
178
219
  end
179
220
 
180
221
 
data/lib/ocean/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ocean
2
- VERSION = "2.5.0"
2
+ VERSION = "2.6.0"
3
3
  end
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.5.0
4
+ version: 2.6.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-13 00:00:00.000000000 Z
11
+ date: 2014-03-14 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