active_rest_client 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +25 -0
- data/lib/active_rest_client/logger.rb +1 -1
- data/lib/active_rest_client/request.rb +15 -16
- data/lib/active_rest_client/version.rb +1 -1
- data/spec/lib/request_spec.rb +7 -0
- 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: 168401e841f472c8c7dbb6295e9a7b6f87e10712
|
4
|
+
data.tar.gz: eb08bff09d6e74f4c3f1dc3a6933113e726cd620
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca279962a442b496caa49d6dff757966bde80df37d7ef9d24e721a64f547a98c5200e4e0e8bf24136255ac7103797831b38c5b83f9da3897fddf739b004d72a8
|
7
|
+
data.tar.gz: 4deb89b8022c5da47c761df1a3853bb16b77949676b1f090cf87150ab0d8389c8b4297a3f22cd61ced2d1c1033add4e271cda532ac629c319158db0b28201013
|
data/README.md
CHANGED
@@ -198,6 +198,31 @@ If you need to, you can access properties of the HAL association. By default ju
|
|
198
198
|
@person.students[0]._hal_attributes("title")
|
199
199
|
```
|
200
200
|
|
201
|
+
#### Association Type 4 - Nested Resources
|
202
|
+
|
203
|
+
It's common to have resources that are logically children of other resources. For example, suppose that your API includes these endpoints:
|
204
|
+
|
205
|
+
| HTTP Verb | Path | |
|
206
|
+
|-----------|-----------------------------|------------------------------------------|
|
207
|
+
| POST | /magazines/:magazine_id/ads | create a new ad belonging to a magazine |
|
208
|
+
| GET | /magazines/:magazine_id/ads | display a list of all ads for a magazine |
|
209
|
+
|
210
|
+
In these cases, your child class will contain the following:
|
211
|
+
|
212
|
+
```ruby
|
213
|
+
class Ad < ActiveRestClient::Base
|
214
|
+
post :create, "/magazines/:magazine_id/ads"
|
215
|
+
get :all, "/magazines/:magazine_id/ads"
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
219
|
+
You can then access Ads by specifying their magazine IDs:
|
220
|
+
|
221
|
+
```ruby
|
222
|
+
Add.all(magazine_id: 1)
|
223
|
+
Add.create(magazine_id: 1, title: "My Add Title")
|
224
|
+
```
|
225
|
+
|
201
226
|
#### Combined Example
|
202
227
|
|
203
228
|
OK, so let's say you have an API for getting articles. Each article has a property called `title` (which is a string) and a property `images` which includes a list of URIs. Following this URI would take you to a image API that returns the image's `filename` and `filesize`. We'll also assume this is a HAL compliant API. We would declare our two models (one for articles and one for images) like the following:
|
@@ -29,7 +29,7 @@ module ActiveRestClient
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def self.info(message)
|
32
|
-
if defined?(Rails) && Rails.respond_to?(:logger)
|
32
|
+
if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
|
33
33
|
Rails.logger.info(message)
|
34
34
|
elsif @logfile
|
35
35
|
File.open(@logfile, "a") do |f|
|
@@ -274,9 +274,9 @@ module ActiveRestClient
|
|
274
274
|
|
275
275
|
def handle_response(response)
|
276
276
|
@response = response
|
277
|
-
@response.status
|
277
|
+
status = @response.status || 200
|
278
278
|
|
279
|
-
if (200..399).include?
|
279
|
+
if (200..399).include?(status)
|
280
280
|
if @method[:options][:plain]
|
281
281
|
return @response = response.body
|
282
282
|
elsif is_json_response?
|
@@ -287,7 +287,7 @@ module ActiveRestClient
|
|
287
287
|
end
|
288
288
|
result = generate_new_object
|
289
289
|
else
|
290
|
-
raise ResponseParseException.new(status
|
290
|
+
raise ResponseParseException.new(status:status, body:@response.body)
|
291
291
|
end
|
292
292
|
else
|
293
293
|
if is_json_response?
|
@@ -295,22 +295,21 @@ module ActiveRestClient
|
|
295
295
|
else
|
296
296
|
error_response = @response.body
|
297
297
|
end
|
298
|
-
if
|
299
|
-
raise HTTPBadRequestClientException.new(status
|
300
|
-
elsif
|
301
|
-
raise HTTPUnauthorisedClientException.new(status
|
302
|
-
elsif
|
303
|
-
raise HTTPForbiddenClientException.new(status
|
304
|
-
elsif
|
305
|
-
raise HTTPNotFoundClientException.new(status
|
306
|
-
elsif (400..499).include?
|
307
|
-
raise HTTPClientException.new(status
|
308
|
-
elsif (500..599).include?
|
309
|
-
raise HTTPServerException.new(status
|
298
|
+
if status == 400
|
299
|
+
raise HTTPBadRequestClientException.new(status:status, result:error_response, url:@url)
|
300
|
+
elsif status == 401
|
301
|
+
raise HTTPUnauthorisedClientException.new(status:status, result:error_response, url:@url)
|
302
|
+
elsif status == 403
|
303
|
+
raise HTTPForbiddenClientException.new(status:status, result:error_response, url:@url)
|
304
|
+
elsif status == 404
|
305
|
+
raise HTTPNotFoundClientException.new(status:status, result:error_response, url:@url)
|
306
|
+
elsif (400..499).include? status
|
307
|
+
raise HTTPClientException.new(status:status, result:error_response, url:@url)
|
308
|
+
elsif (500..599).include? status
|
309
|
+
raise HTTPServerException.new(status:status, result:error_response, url:@url)
|
310
310
|
end
|
311
311
|
end
|
312
312
|
|
313
|
-
|
314
313
|
result
|
315
314
|
end
|
316
315
|
|
data/spec/lib/request_spec.rb
CHANGED
@@ -69,6 +69,13 @@ describe ActiveRestClient::Request do
|
|
69
69
|
ExampleClient.remove(id:1)
|
70
70
|
end
|
71
71
|
|
72
|
+
it "should work with faraday response objects" do
|
73
|
+
response = Faraday::Response.new
|
74
|
+
response.stub(:body).and_return({}.to_json)
|
75
|
+
ActiveRestClient::Connection.any_instance.should_receive(:get).and_return(response)
|
76
|
+
expect { ExampleClient.all }.to_not raise_error
|
77
|
+
end
|
78
|
+
|
72
79
|
it "should pass through get parameters" do
|
73
80
|
ActiveRestClient::Connection.any_instance.should_receive(:get).with("/?debug=true", an_instance_of(Hash)).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
74
81
|
ExampleClient.all debug:true
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_rest_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Which Ltd
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-09-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|