activeresource-response 3.0.0 → 3.1.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.
- checksums.yaml +4 -4
- data/README.md +68 -58
- data/activeresource-response.gemspec +3 -2
- data/lib/active_resource_response/connection.rb +14 -6
- data/lib/active_resource_response/version.rb +1 -1
- data/test/active_resource_response_test.rb +3 -1
- metadata +20 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ac95d9180051ba82db3f5e1983cf87321855351f8a7a06e546a866207d2bcdd3
|
|
4
|
+
data.tar.gz: 354b4839689f1c964eaa251fbf264e9cccfd80b3378a0d6c466c38b05016bc10
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3965902ea880021dbffd11ccb6796fdfce27c636fa7592c71a4ca710d61dbc04801e60045b7adf861c47576a868fe6d6bd616e7fa49179396b4a3dd1c473d964
|
|
7
|
+
data.tar.gz: 82615892c23ef13731a3a1e7288a47d5df8caf054fb61fe2de21c7fe0eea410221954dbbc5ab653a8fe93be8dacdbdd8fdfdd71597c8ddbe6dbcaaaaca8589a0
|
data/README.md
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
##
|
|
2
|
-
This gem adds
|
|
1
|
+
## ActiveResource-Response
|
|
2
|
+
This gem adds the ability to access the HTTP response (`Net::HTTPResponse`) object from the result (either a single object or a collection) of an ActiveResource call (methods: `find`, `all`, `first`, `last`, `get`).
|
|
3
3
|
|
|
4
|
-
#### Why It
|
|
5
|
-
|
|
4
|
+
#### Why Can It Be Used?
|
|
5
|
+
This functionality can be used to easily implement pagination in a REST API, so that an ActiveResource client can navigate paginated results.
|
|
6
6
|
|
|
7
|
-
#### How to
|
|
8
|
-
Add dependency to your Gemfile
|
|
7
|
+
#### How to Use?
|
|
8
|
+
Add the dependency to your `Gemfile`:
|
|
9
9
|
|
|
10
10
|
```ruby
|
|
11
11
|
gem "activeresource-response"
|
|
12
12
|
```
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
Open your ActiveResource class and add:
|
|
15
15
|
|
|
16
16
|
```ruby
|
|
17
17
|
add_response_method :your_method_name
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
-
You can add method to ActiveResource::Base to use it in all subclasses
|
|
20
|
+
You can add the method to `ActiveResource::Base` to use it in all subclasses:
|
|
21
21
|
|
|
22
22
|
```ruby
|
|
23
23
|
class ActiveResource::Base
|
|
@@ -25,7 +25,7 @@ class ActiveResource::Base
|
|
|
25
25
|
end
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
-
You can remove method from ActiveResource subclass
|
|
28
|
+
You can remove the method from an ActiveResource subclass:
|
|
29
29
|
|
|
30
30
|
```ruby
|
|
31
31
|
class Order < ActiveResource::Base
|
|
@@ -33,25 +33,25 @@ class Order < ActiveResource::Base
|
|
|
33
33
|
end
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
-
## Full
|
|
36
|
+
## Full Example of Usage with the Kaminari Gem
|
|
37
37
|
|
|
38
|
-
ActiveResource Class
|
|
38
|
+
ActiveResource Class:
|
|
39
39
|
|
|
40
40
|
```ruby
|
|
41
41
|
class Order < ActiveResource::Base
|
|
42
42
|
self.format = :json
|
|
43
43
|
self.site = 'http://0.0.0.0:3000/'
|
|
44
44
|
self.element_name = "order"
|
|
45
|
-
add_response_method :http_response #
|
|
45
|
+
add_response_method :http_response # Our new method for returned objects
|
|
46
46
|
end
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
Server Side
|
|
49
|
+
Server Side:
|
|
51
50
|
|
|
51
|
+
```ruby
|
|
52
52
|
class OrdersController < ApplicationController
|
|
53
53
|
def index
|
|
54
|
-
@orders = Order.page(params[:page]).per(params[:per_page] || 10) #default 10 per page
|
|
54
|
+
@orders = Order.page(params[:page]).per(params[:per_page] || 10) # default 10 per page
|
|
55
55
|
response.headers["X-total"] = @orders.total_count.to_s
|
|
56
56
|
response.headers["X-offset"] = @orders.offset_value.to_s
|
|
57
57
|
response.headers["X-limit"] = @orders.limit_value.to_s
|
|
@@ -60,13 +60,13 @@ class OrdersController < ApplicationController
|
|
|
60
60
|
end
|
|
61
61
|
```
|
|
62
62
|
|
|
63
|
-
Client Side
|
|
63
|
+
Client Side:
|
|
64
64
|
|
|
65
65
|
```ruby
|
|
66
66
|
class OrdersController < ApplicationController
|
|
67
67
|
def index
|
|
68
68
|
orders = Order.all(params: params)
|
|
69
|
-
@orders = Kaminari::PaginatableArray.new(orders,{
|
|
69
|
+
@orders = Kaminari::PaginatableArray.new(orders, {
|
|
70
70
|
limit: orders.http_response['X-limit'].to_i,
|
|
71
71
|
offset: orders.http_response['X-offset'].to_i,
|
|
72
72
|
total_count: orders.http_response['X-total'].to_i
|
|
@@ -75,92 +75,102 @@ class OrdersController < ApplicationController
|
|
|
75
75
|
end
|
|
76
76
|
```
|
|
77
77
|
|
|
78
|
-
###
|
|
79
|
-
will_paginate has a
|
|
78
|
+
### Will_paginate Compatibility
|
|
79
|
+
`will_paginate` has a slightly different API, so to populate the headers:
|
|
80
80
|
|
|
81
81
|
```ruby
|
|
82
82
|
response.headers["X-total"] = @orders.total_entries.to_s
|
|
83
83
|
response.headers["X-offset"] = @orders.offset.to_s
|
|
84
84
|
response.headers["X-limit"] = @orders.per_page.to_s
|
|
85
|
-
```
|
|
85
|
+
```
|
|
86
86
|
|
|
87
|
-
On the API client side you might also use will_paginate
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
On the API client side, you might also use `will_paginate`. In that case, you can just require `will_paginate/array` (e.g., in an initializer):
|
|
88
|
+
|
|
89
|
+
```ruby
|
|
90
|
+
orders = Order.all(params: params)
|
|
91
|
+
@orders = WillPaginate::Collection.create(params[:page] || 1, params[:per_page] || 10, orders.http_response['X-total'].to_i) do |pager|
|
|
91
92
|
pager.replace orders
|
|
92
93
|
end
|
|
93
94
|
```
|
|
94
95
|
|
|
95
|
-
### Every
|
|
96
|
-
|
|
96
|
+
### Every Time an HTTP Connection is Invoked
|
|
97
|
+
The ActiveResource connection object stores the HTTP response. You can access it with the `http_response` method.
|
|
98
|
+
|
|
99
|
+
Example:
|
|
100
|
+
|
|
97
101
|
```ruby
|
|
98
102
|
class Order < ActiveResource::Base
|
|
99
103
|
self.site = 'http://0.0.0.0:3000/'
|
|
100
104
|
self.element_name = "order"
|
|
101
|
-
add_response_method :my_response #
|
|
105
|
+
add_response_method :my_response # Our new method
|
|
102
106
|
end
|
|
103
107
|
|
|
104
108
|
orders = Order.all
|
|
105
109
|
first_order = Order.find(1)
|
|
106
|
-
#
|
|
110
|
+
# See Net::HTTPResponse#[] method
|
|
107
111
|
orders.my_response['content-length']
|
|
108
112
|
# => "3831"
|
|
109
113
|
first_order.my_response['content-length']
|
|
110
|
-
|
|
111
|
-
#
|
|
114
|
+
# => "260"
|
|
115
|
+
# Connection also always has the last HTTP response object. To access it, use the http_response method:
|
|
112
116
|
Order.connection.http_response.to_hash
|
|
113
117
|
# => {"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"]}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
### Custom get
|
|
117
|
-
You can access response from result of custom get method
|
|
118
|
-
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Custom `get` Method
|
|
121
|
+
You can access the response from the result of a custom `get` method.
|
|
122
|
+
|
|
123
|
+
Example:
|
|
124
|
+
|
|
119
125
|
```ruby
|
|
120
126
|
class Country < ActiveResource::Base
|
|
121
127
|
self.site = 'http://0.0.0.0:3000/'
|
|
122
|
-
add_response_method :http #
|
|
128
|
+
add_response_method :http # Our new method
|
|
123
129
|
end
|
|
130
|
+
|
|
124
131
|
cities = Country.find(1).get(:cities)
|
|
125
|
-
cities.http
|
|
126
|
-
```
|
|
132
|
+
cities.http # Method from the Country class is available
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Headers and Cookies Methods
|
|
136
|
+
You can get cookies and headers from the response.
|
|
137
|
+
|
|
138
|
+
Example:
|
|
127
139
|
|
|
128
|
-
### Headers and cookies methods
|
|
129
|
-
You can get cookies and headers from response
|
|
130
|
-
Example
|
|
131
140
|
```ruby
|
|
132
141
|
class Country < ActiveResource::Base
|
|
133
142
|
self.site = 'http://0.0.0.0:3000/'
|
|
134
|
-
add_response_method :my_response #
|
|
143
|
+
add_response_method :my_response # Our new method
|
|
135
144
|
end
|
|
145
|
+
|
|
136
146
|
countries = Country.all
|
|
137
147
|
countries.my_response.headers
|
|
138
148
|
|
|
139
|
-
#
|
|
149
|
+
# Collection with symbolized keys:
|
|
150
|
+
# {:content_type=>["application/json; charset=utf-8"], :x_ua_compatible=>["IE=Edge"], ..., :set_cookie=>["bar=foo; path=/", "foo=bar; path=/"]}
|
|
140
151
|
|
|
141
152
|
countries.my_response.cookies
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
### Note About
|
|
146
|
-
|
|
147
|
-
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
|
|
153
|
+
# => {"bar"=>"foo", "foo"=>"bar"}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Note About HTTP Response
|
|
157
|
+
The HTTP response is an object of `Net::HTTPOK`, `Net::HTTPClientError`, or one of the other subclasses of the `Net::HTTPResponse` class. For more information, see the documentation: [Net::HTTPResponse](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTPResponse.html).
|
|
148
158
|
|
|
149
159
|
### Testing with ActiveResource::HttpMock
|
|
150
|
-
Add this line to your test to patch http_mock
|
|
160
|
+
Add this line to your test to patch `http_mock`:
|
|
151
161
|
|
|
152
162
|
```ruby
|
|
153
|
-
|
|
163
|
+
require "active_resource_response/http_mock"
|
|
154
164
|
```
|
|
155
165
|
|
|
156
166
|
### Contributing
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
167
|
+
1. Fork it
|
|
168
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
169
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
170
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
171
|
+
5. Create a new Pull Request
|
|
162
172
|
|
|
163
|
-
|
|
164
|
-
#### Please, feel free to contact me if you have any questions
|
|
173
|
+
#### Please feel free to contact me if you have any questions:
|
|
165
174
|
fedoronchuk(at)gmail.com
|
|
166
175
|
|
|
176
|
+
|
|
@@ -8,12 +8,13 @@ Gem::Specification.new do |s|
|
|
|
8
8
|
s.version = ActiveResourceResponse::Version::VERSION
|
|
9
9
|
s.authors = ["Igor Fedoronchuk"]
|
|
10
10
|
s.email = ["fedoronchuk@gmail.com"]
|
|
11
|
-
s.homepage = "
|
|
11
|
+
s.homepage = "https://github.com/didww/activeresource-response"
|
|
12
12
|
s.summary = %q{activeresource extension}
|
|
13
13
|
s.description = %q{This gem adds possibility to access http response object from result of ActiveResource::Base find method }
|
|
14
14
|
s.license = 'MIT'
|
|
15
15
|
|
|
16
|
-
s.add_runtime_dependency('activeresource', ['>= 6.1', '< 6.
|
|
16
|
+
s.add_runtime_dependency('activeresource', ['>= 6.1', '< 6.3'])
|
|
17
|
+
s.add_dependency 'activesupport'
|
|
17
18
|
s.add_dependency "jruby-openssl" if RUBY_PLATFORM == "java"
|
|
18
19
|
s.add_development_dependency "minitest"
|
|
19
20
|
s.add_development_dependency 'rake'
|
|
@@ -22,6 +22,18 @@
|
|
|
22
22
|
#++
|
|
23
23
|
module ActiveResourceResponse
|
|
24
24
|
module Connection
|
|
25
|
+
class Current < ActiveSupport::CurrentAttributes
|
|
26
|
+
attribute :http_responses, default: {}
|
|
27
|
+
|
|
28
|
+
def http_response(klass)
|
|
29
|
+
http_responses[klass]
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def set_http_response(klass, response)
|
|
33
|
+
http_responses[klass] = response
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
25
37
|
def self.included(base)
|
|
26
38
|
base.class_eval do
|
|
27
39
|
alias_method :origin_handle_response, :handle_response
|
|
@@ -38,15 +50,11 @@ module ActiveResourceResponse
|
|
|
38
50
|
end
|
|
39
51
|
|
|
40
52
|
def http_response
|
|
41
|
-
|
|
53
|
+
Current.http_response(self.class)
|
|
42
54
|
end
|
|
43
55
|
|
|
44
56
|
def http_response=(response)
|
|
45
|
-
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def http_storage
|
|
49
|
-
Thread.current
|
|
57
|
+
Current.set_http_response self.class, response
|
|
50
58
|
end
|
|
51
59
|
end
|
|
52
60
|
end
|
|
@@ -50,6 +50,8 @@ class ActiveResourceResponseTest < Minitest::Test
|
|
|
50
50
|
mock.get "/streets/1/city.json", {}, @city.to_json, 200, {"X-total"=>'1'}
|
|
51
51
|
mock.get "/streets/1.json", {}, @street.to_json, 200, {"X-total"=>'1'}
|
|
52
52
|
end
|
|
53
|
+
|
|
54
|
+
ActiveResourceResponse::Connection::Current.reset
|
|
53
55
|
end
|
|
54
56
|
|
|
55
57
|
|
|
@@ -115,7 +117,7 @@ class ActiveResourceResponseTest < Minitest::Test
|
|
|
115
117
|
end
|
|
116
118
|
|
|
117
119
|
def test_get_headers_from_find_when_404_custom_prefix
|
|
118
|
-
Status.all
|
|
120
|
+
Status.all(params: { country_id: 1 }).to_a
|
|
119
121
|
assert_equal Status.http_response.code, 404
|
|
120
122
|
end
|
|
121
123
|
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: activeresource-response
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Igor Fedoronchuk
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activeresource
|
|
@@ -19,7 +18,7 @@ dependencies:
|
|
|
19
18
|
version: '6.1'
|
|
20
19
|
- - "<"
|
|
21
20
|
- !ruby/object:Gem::Version
|
|
22
|
-
version: '6.
|
|
21
|
+
version: '6.3'
|
|
23
22
|
type: :runtime
|
|
24
23
|
prerelease: false
|
|
25
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -29,7 +28,21 @@ dependencies:
|
|
|
29
28
|
version: '6.1'
|
|
30
29
|
- - "<"
|
|
31
30
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: '6.
|
|
31
|
+
version: '6.3'
|
|
32
|
+
- !ruby/object:Gem::Dependency
|
|
33
|
+
name: activesupport
|
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '0'
|
|
39
|
+
type: :runtime
|
|
40
|
+
prerelease: false
|
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
33
46
|
- !ruby/object:Gem::Dependency
|
|
34
47
|
name: minitest
|
|
35
48
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -103,11 +116,10 @@ files:
|
|
|
103
116
|
- test/fixtures/street.rb
|
|
104
117
|
- test/lint_test.rb
|
|
105
118
|
- test/test_helper.rb
|
|
106
|
-
homepage:
|
|
119
|
+
homepage: https://github.com/didww/activeresource-response
|
|
107
120
|
licenses:
|
|
108
121
|
- MIT
|
|
109
122
|
metadata: {}
|
|
110
|
-
post_install_message:
|
|
111
123
|
rdoc_options: []
|
|
112
124
|
require_paths:
|
|
113
125
|
- lib
|
|
@@ -122,8 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
122
134
|
- !ruby/object:Gem::Version
|
|
123
135
|
version: '0'
|
|
124
136
|
requirements: []
|
|
125
|
-
rubygems_version:
|
|
126
|
-
signing_key:
|
|
137
|
+
rubygems_version: 4.0.4
|
|
127
138
|
specification_version: 4
|
|
128
139
|
summary: activeresource extension
|
|
129
140
|
test_files:
|