gds-api-adapters 7.13.0 → 7.14.0

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.
@@ -12,10 +12,11 @@ module GdsApi
12
12
  end
13
13
 
14
14
  class HTTPErrorResponse < BaseError
15
- attr_accessor :code
15
+ attr_accessor :code, :error_details
16
16
 
17
- def initialize(code)
17
+ def initialize(code, error_details = nil)
18
18
  @code = code
19
+ @error_details = error_details
19
20
  end
20
21
  end
21
22
 
@@ -88,7 +88,7 @@ module GdsApi
88
88
  end
89
89
 
90
90
  def delete_json!(url, params = nil, additional_headers = {})
91
- do_request(:delete, url, params, additional_headers)
91
+ do_json_request(:delete, url, params, additional_headers)
92
92
  end
93
93
 
94
94
  def post_multipart(url, params)
@@ -125,12 +125,12 @@ module GdsApi
125
125
 
126
126
  rescue RestClient::Exception => e
127
127
  # Attempt to parse the body as JSON if possible
128
- body = begin
128
+ error_details = begin
129
129
  e.http_body ? JSON.parse(e.http_body) : nil
130
130
  rescue JSON::ParserError
131
- e.http_body
131
+ nil
132
132
  end
133
- raise GdsApi::HTTPErrorResponse.new(e.http_code), body
133
+ raise GdsApi::HTTPErrorResponse.new(e.http_code, error_details), e.http_body
134
134
  end
135
135
 
136
136
  # If no custom response is given, just instantiate Response
@@ -1,5 +1,6 @@
1
1
  require 'json'
2
2
  require 'ostruct'
3
+ require 'forwardable'
3
4
  require_relative 'core-ext/openstruct'
4
5
 
5
6
  module GdsApi
@@ -0,0 +1,33 @@
1
+ require_relative 'base'
2
+ #require_relative 'exceptions'
3
+
4
+ class GdsApi::Router < GdsApi::Base
5
+
6
+ ### Backends
7
+
8
+ def get_backend(id)
9
+ get_json("#{endpoint}/backends/#{CGI.escape(id)}")
10
+ end
11
+
12
+ def add_backend(id, url)
13
+ put_json!("#{endpoint}/backends/#{CGI.escape(id)}", :backend => {:backend_url => url})
14
+ end
15
+
16
+ def delete_backend(id)
17
+ delete_json!("#{endpoint}/backends/#{CGI.escape(id)}")
18
+ end
19
+
20
+ ### Routes
21
+
22
+ def get_route(path, type)
23
+ get_json("#{endpoint}/routes?incoming_path=#{CGI.escape(path)}&route_type=#{CGI.escape(type)}")
24
+ end
25
+
26
+ def add_route(path, type, backend_id)
27
+ put_json!("#{endpoint}/routes", :route => {:incoming_path => path, :route_type => type, :handler => "backend", :backend_id => backend_id})
28
+ end
29
+
30
+ def delete_route(path, type)
31
+ delete_json!("#{endpoint}/routes?incoming_path=#{CGI.escape(path)}&route_type=#{CGI.escape(type)}")
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module GdsApi
2
- VERSION = '7.13.0'
2
+ VERSION = '7.14.0'
3
3
  end
@@ -0,0 +1,268 @@
1
+ require 'test_helper'
2
+ require 'gds_api/router'
3
+
4
+ describe GdsApi::Router do
5
+
6
+ before do
7
+ @base_api_url = "http://router-api.example.com"
8
+ @api = GdsApi::Router.new(@base_api_url)
9
+ end
10
+
11
+ describe "managing backends" do
12
+ describe "fetching details about a backend" do
13
+ it "should return backend details" do
14
+ req = WebMock.stub_request(:get, "#{@base_api_url}/backends/foo").
15
+ to_return(:body => {"backend_id" => "foo", "backend_url" => "http://foo.example.com/"}.to_json,
16
+ :headers => {"Content-type" => "application/json"})
17
+
18
+ response = @api.get_backend("foo")
19
+ assert_equal 200, response.code
20
+ assert_equal "http://foo.example.com/", response.backend_url
21
+
22
+ assert_requested(req)
23
+ end
24
+
25
+ it "should return nil for a non-existend backend" do
26
+ req = WebMock.stub_request(:get, "#{@base_api_url}/backends/foo").
27
+ to_return(:status => 404)
28
+
29
+ response = @api.get_backend("foo")
30
+ assert_nil response
31
+
32
+ assert_requested(req)
33
+ end
34
+
35
+ it "should URI escape the given ID" do
36
+ req = WebMock.stub_request(:get, "#{@base_api_url}/backends/foo+bar").
37
+ to_return(:status => 404)
38
+
39
+ response = @api.get_backend("foo bar")
40
+ assert_nil response
41
+
42
+ assert_requested(req)
43
+ end
44
+ end
45
+
46
+ describe "creating/updating a backend" do
47
+ it "should allow creating/updating a backend" do
48
+ req = WebMock.stub_request(:put, "#{@base_api_url}/backends/foo").
49
+ with(:body => {"backend" => {"backend_url" => "http://foo.example.com/"}}.to_json).
50
+ to_return(:status => 201, :body => {"backend_id" => "foo", "backend_url" => "http://foo.example.com/"}.to_json,
51
+ :headers => {"Content-type" => "application/json"})
52
+
53
+ response = @api.add_backend("foo", "http://foo.example.com/")
54
+ assert_equal 201, response.code
55
+ assert_equal "http://foo.example.com/", response.backend_url
56
+
57
+ assert_requested(req)
58
+ end
59
+
60
+ it "should raise an error if creating/updating a backend fails" do
61
+ response_data = {"backend_id" => "foo", "backend_url" => "ftp://foo.example.com/", "errors" => {"backend_url" => "is not an HTTP URL"}}
62
+ req = WebMock.stub_request(:put, "#{@base_api_url}/backends/foo").
63
+ with(:body => {"backend" => {"backend_url" => "http://foo.example.com/"}}.to_json).
64
+ to_return(:status => 400, :body => response_data.to_json, :headers => {"Content-type" => "application/json"})
65
+
66
+ e = nil
67
+ begin
68
+ @api.add_backend("foo", "http://foo.example.com/")
69
+ rescue GdsApi::HTTPErrorResponse => ex
70
+ e = ex
71
+ end
72
+
73
+ refute_nil e
74
+ assert_equal 400, e.code
75
+ assert_equal response_data, e.error_details
76
+
77
+ assert_requested(req)
78
+ end
79
+
80
+ it "should URI escape the passed id" do
81
+ req = WebMock.stub_request(:put, "#{@base_api_url}/backends/foo+bar").
82
+ with(:body => {"backend" => {"backend_url" => "http://foo.example.com/"}}.to_json).
83
+ to_return(:status => 404, :body => "Not found")
84
+
85
+ # We expect a GdsApi::HTTPErrorResponse, but we want to ensure nothing else is raised
86
+ begin
87
+ @api.add_backend("foo bar", "http://foo.example.com/")
88
+ rescue GdsApi::HTTPErrorResponse
89
+ end
90
+
91
+ assert_requested(req)
92
+ end
93
+ end
94
+
95
+ describe "deleting a backend" do
96
+ it "allow deleting a backend" do
97
+ req = WebMock.stub_request(:delete, "#{@base_api_url}/backends/foo").
98
+ to_return(:status => 200, :body => {"backend_id" => "foo", "backend_url" => "http://foo.example.com/"}.to_json,
99
+ :headers => {"Content-type" => "application/json"})
100
+
101
+ response = @api.delete_backend("foo")
102
+ assert_equal 200, response.code
103
+ assert_equal "http://foo.example.com/", response.backend_url
104
+
105
+ assert_requested(req)
106
+ end
107
+
108
+ it "should raise an error if deleting the backend fails" do
109
+ response_data = {"backend_id" => "foo", "backend_url" => "ftp://foo.example.com/", "errors" => {"base" => "Backend has routes - can't delete"}}
110
+ req = WebMock.stub_request(:delete, "#{@base_api_url}/backends/foo").
111
+ to_return(:status => 400, :body => response_data.to_json, :headers => {"Content-type" => "application/json"})
112
+
113
+ e = nil
114
+ begin
115
+ @api.delete_backend("foo")
116
+ rescue GdsApi::HTTPErrorResponse => ex
117
+ e = ex
118
+ end
119
+
120
+ refute_nil e
121
+ assert_equal 400, e.code
122
+ assert_equal response_data, e.error_details
123
+
124
+ assert_requested(req)
125
+ end
126
+
127
+ it "should URI escape the passed id" do
128
+ req = WebMock.stub_request(:delete, "#{@base_api_url}/backends/foo+bar").
129
+ to_return(:status => 404, :body => "Not found")
130
+
131
+ # We expect a GdsApi::HTTPErrorResponse, but we want to ensure nothing else is raised
132
+ begin
133
+ @api.delete_backend("foo bar")
134
+ rescue GdsApi::HTTPErrorResponse
135
+ end
136
+
137
+ assert_requested(req)
138
+ end
139
+ end
140
+ end
141
+
142
+ describe "managing routes" do
143
+ describe "fetching a route" do
144
+ it "should return the route details" do
145
+ route_data = {"incoming_path" => "/foo", "route_type" => "exact", "handler" => "backend", "backend_id" => "foo"}
146
+ req = WebMock.stub_request(:get, "#{@base_api_url}/routes").
147
+ with(:query => {"incoming_path" => "/foo", "route_type" => "exact"}).
148
+ to_return(:status => 200, :body => route_data.to_json, :headers => {"Content-type" => "application/json"})
149
+
150
+ response = @api.get_route("/foo", "exact")
151
+ assert_equal 200, response.code
152
+ assert_equal "foo", response.backend_id
153
+
154
+ assert_requested(req)
155
+ end
156
+
157
+ it "should return nil if nothing found" do
158
+ req = WebMock.stub_request(:get, "#{@base_api_url}/routes").
159
+ with(:query => {"incoming_path" => "/foo", "route_type" => "exact"}).
160
+ to_return(:status => 404)
161
+
162
+ response = @api.get_route("/foo", "exact")
163
+ assert_nil response
164
+
165
+ assert_requested(req)
166
+ end
167
+
168
+ it "should escape the params" do
169
+ # The WebMock query matcher matches unescaped params. The call blows up if they're not escaped
170
+
171
+ req = WebMock.stub_request(:get, "#{@base_api_url}/routes").
172
+ with(:query => {"incoming_path" => "/foo bar", "route_type" => "exa ct"}).
173
+ to_return(:status => 404)
174
+
175
+ response = @api.get_route("/foo bar", "exa ct")
176
+ assert_nil response
177
+
178
+ assert_requested(req)
179
+ end
180
+ end
181
+
182
+ describe "creating/updating a route" do
183
+ it "should allow creating/updating a route" do
184
+ route_data = {"incoming_path" => "/foo", "route_type" => "exact", "handler" => "backend", "backend_id" => "foo"}
185
+ req = WebMock.stub_request(:put, "#{@base_api_url}/routes").
186
+ with(:body => {"route" => route_data}.to_json).
187
+ to_return(:status => 201, :body => route_data.to_json, :headers => {"Content-type" => "application/json"})
188
+
189
+ response = @api.add_route("/foo", "exact", "foo")
190
+ assert_equal 201, response.code
191
+ assert_equal "foo", response.backend_id
192
+
193
+ assert_requested(req)
194
+ end
195
+
196
+ it "should raise an error if creating/updating the route fails" do
197
+ route_data = {"incoming_path" => "/foo", "route_type" => "exact", "handler" => "backend", "backend_id" => "foo"}
198
+ response_data = route_data.merge(:errors => {"backend_id" => "does not exist"})
199
+
200
+ response_data = {"backend_id" => "foo", "backend_url" => "ftp://foo.example.com/", "errors" => {"backend_url" => "is not an HTTP URL"}}
201
+ req = WebMock.stub_request(:put, "#{@base_api_url}/routes").
202
+ with(:body => {"route" => route_data}.to_json).
203
+ to_return(:status => 400, :body => response_data.to_json, :headers => {"Content-type" => "application/json"})
204
+
205
+ e = nil
206
+ begin
207
+ @api.add_route("/foo", "exact", "foo")
208
+ rescue GdsApi::HTTPErrorResponse => ex
209
+ e = ex
210
+ end
211
+
212
+ refute_nil e
213
+ assert_equal 400, e.code
214
+ assert_equal response_data, e.error_details
215
+
216
+ assert_requested(req)
217
+ end
218
+ end
219
+
220
+ describe "deleting a route" do
221
+ it "should allow deleting a route" do
222
+ route_data = {"incoming_path" => "/foo", "route_type" => "exact", "handler" => "backend", "backend_id" => "foo"}
223
+ req = WebMock.stub_request(:delete, "#{@base_api_url}/routes").
224
+ with(:query => {"incoming_path" => "/foo", "route_type" => "exact"}).
225
+ to_return(:status => 200, :body => route_data.to_json, :headers => {"Content-type" => "application/json"})
226
+
227
+ response = @api.delete_route("/foo", "exact")
228
+ assert_equal 200, response.code
229
+ assert_equal "foo", response.backend_id
230
+
231
+ assert_requested(req)
232
+ end
233
+
234
+ it "should raise HTTPNotFound if nothing found" do
235
+ req = WebMock.stub_request(:delete, "#{@base_api_url}/routes").
236
+ with(:query => {"incoming_path" => "/foo", "route_type" => "exact"}).
237
+ to_return(:status => 404)
238
+
239
+ e = nil
240
+ begin
241
+ @api.delete_route("/foo", "exact")
242
+ rescue GdsApi::HTTPNotFound => ex
243
+ e = ex
244
+ end
245
+
246
+ refute_nil e
247
+ assert_equal 404, e.code
248
+
249
+ assert_requested(req)
250
+ end
251
+
252
+ it "should escape the params" do
253
+ # The WebMock query matcher matches unescaped params. The call blows up if they're not escaped
254
+
255
+ req = WebMock.stub_request(:delete, "#{@base_api_url}/routes").
256
+ with(:query => {"incoming_path" => "/foo bar", "route_type" => "exa ct"}).
257
+ to_return(:status => 404)
258
+
259
+ begin
260
+ @api.delete_route("/foo bar", "exa ct")
261
+ rescue GdsApi::HTTPNotFound
262
+ end
263
+
264
+ assert_requested(req)
265
+ end
266
+ end
267
+ end
268
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gds-api-adapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.13.0
4
+ version: 7.14.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-22 00:00:00.000000000 Z
12
+ date: 2013-10-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: plek
@@ -299,6 +299,7 @@ files:
299
299
  - lib/gds_api/core-ext/openstruct.rb
300
300
  - lib/gds_api/response.rb
301
301
  - lib/gds_api/publisher.rb
302
+ - lib/gds_api/router.rb
302
303
  - lib/gds_api/content_api.rb
303
304
  - README.md
304
305
  - Rakefile
@@ -320,6 +321,7 @@ files:
320
321
  - test/asset_manager_test.rb
321
322
  - test/fixtures/hello.txt
322
323
  - test/fixtures/world_organisations_australia.json
324
+ - test/router_test.rb
323
325
  - test/list_response_test.rb
324
326
  - test/gds_api_base_test.rb
325
327
  - test/worldwide_api_test.rb
@@ -338,7 +340,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
338
340
  version: '0'
339
341
  segments:
340
342
  - 0
341
- hash: -3882474794805816869
343
+ hash: -758637662694178891
342
344
  required_rubygems_version: !ruby/object:Gem::Requirement
343
345
  none: false
344
346
  requirements:
@@ -347,7 +349,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
347
349
  version: '0'
348
350
  segments:
349
351
  - 0
350
- hash: -3882474794805816869
352
+ hash: -758637662694178891
351
353
  requirements: []
352
354
  rubyforge_project:
353
355
  rubygems_version: 1.8.23
@@ -373,6 +375,7 @@ test_files:
373
375
  - test/asset_manager_test.rb
374
376
  - test/fixtures/hello.txt
375
377
  - test/fixtures/world_organisations_australia.json
378
+ - test/router_test.rb
376
379
  - test/list_response_test.rb
377
380
  - test/gds_api_base_test.rb
378
381
  - test/worldwide_api_test.rb