activeresource-response 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
= Activeresource-response
|
2
2
|
|
3
|
-
This gem adds possibility to access http response object from result of activeresource call (
|
3
|
+
This gem adds possibility to access http response object from result (single object or collection) of activeresource call (methods : find, all, first, last )
|
4
|
+
|
5
|
+
|
4
6
|
|
5
7
|
== Why It can be used?
|
6
8
|
Such functionallity can be used for easily implementing pagination in a REST API so that an ActiveResource client can navigate paginated results.
|
@@ -52,3 +54,34 @@ Client Side
|
|
52
54
|
end
|
53
55
|
|
54
56
|
|
57
|
+
== Gem also creates http_response method for ActiveResource::Connection class (no matter if find method was called or one of custom methods, which invokes http connection)
|
58
|
+
Example
|
59
|
+
|
60
|
+
class Order < ActiveResource::Base
|
61
|
+
self.site = 'http://0.0.0.0:3000/'
|
62
|
+
self.element_name = "order"
|
63
|
+
add_response_method :my_response # our new method
|
64
|
+
end
|
65
|
+
|
66
|
+
orders = Order.all
|
67
|
+
first_order = Order.find(1)
|
68
|
+
orders.my_response['content-length']
|
69
|
+
# => "3831"
|
70
|
+
first_order.my_response['content-length']
|
71
|
+
#=> "260"
|
72
|
+
#connection also always has last http response object , to access it use http_response method
|
73
|
+
Order.connection.http_response.to_hash
|
74
|
+
# => {"content-type"=>["application/json; charset=utf-8"], "x-ua-compatible"=>["IE=Edge"], "etag"=>["\"573cabd02b2f1f90405f7f4f77995fab\""], "cache-control"=>["max-age=0, private, must-revalidate"], "x-request-id"=>["2911c13a0c781044c474450ed789613d"], "x-runtime"=>["0.071018"], "content-length"=>["260"], "server"=>["WEBrick/1.3.1 (Ruby/1.9.2/2011-02-18)"], "date"=>["Sun, 19 Feb 2012 10:21:29 GMT"], "connection"=>["close"]}
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
== About Http response
|
79
|
+
http response is object of Net::HTTPOK, Net::HTTPClientError or one of other subclasses
|
80
|
+
of Net::HTTPResponse class. For more information see documentation http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTPResponse.html
|
81
|
+
|
82
|
+
|
83
|
+
== Inspirators
|
84
|
+
|
85
|
+
http://phpblog.com.ua/2012/01/rails-activeresource-i-zagolovki/
|
86
|
+
|
87
|
+
http://stackoverflow.com/questions/5972429/active-resource-responses-how-to-get-them
|
@@ -5,17 +5,19 @@ module ActiveresourceResponse
|
|
5
5
|
end
|
6
6
|
module ClassMethods
|
7
7
|
def add_response_method(method_name = 'http_response')
|
8
|
+
[:find, :get].each do |method|
|
8
9
|
class_eval <<-EOS
|
9
10
|
class << self
|
10
|
-
alias_method :
|
11
|
-
def
|
12
|
-
result =
|
11
|
+
alias_method :origin_#{method}, :#{method}
|
12
|
+
def #{method}(*arguments)
|
13
|
+
result = origin_#{method}(*arguments)
|
13
14
|
result.class_eval("attr_reader :#{method_name}")
|
14
15
|
result.instance_variable_set(:"@#{method_name}", connection.http_response)
|
15
16
|
result
|
16
17
|
end
|
17
|
-
|
18
|
+
end
|
18
19
|
EOS
|
20
|
+
end
|
19
21
|
end
|
20
22
|
end
|
21
23
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: activeresource-response
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Igor Fedoronchuk
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-02-
|
13
|
+
date: 2012-02-21 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activeresource
|