activeresource-response 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NWEwMjAxODIwODM1OTg5MGJlYjUyZWIwNjIxNDZkOTkyZjQ5YmViMA==
5
- data.tar.gz: !binary |-
6
- YWNlYTBkMjM3NWRkMDMxYTg0M2VkNDc1OWExYjI4OTIyMjhmMDJhZQ==
2
+ SHA256:
3
+ metadata.gz: c12ab32aee7b9d83f5d71af0fe372188b7aab41618aec5e52ade786e6fe069dd
4
+ data.tar.gz: 63c1b88d91b907be17ca321eefa29652764310ef26bb46e319f2d3a0d2633742
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- N2U5Y2MxODhlYjEzMjFiZjhhY2FlMzA2Mjk5ZTFiZjIzY2RiZWM2ZDc2ZGY0
10
- M2M2NTliMThiNjA5N2MzMjBjYmJlMzk1Y2VjMDZhZDFmYjRjYWI1OTBjOGFj
11
- NWY3OGI4ZmNjMzc3YjkwMWY0NGI2ZjEzNzFiZDFkMzE5NzM3Njc=
12
- data.tar.gz: !binary |-
13
- OGEyZDA2OTBlZGNkYjUxYzNmNDM3ZmM5OWM2NGFjYTg5ZDk2NzY5NzE4ZTAz
14
- MWRmNGM5MDVhNTRjYmIwNDE0NjE4NjhiNTgzNTViNTNkMzZmM2U2OTc5ZmM5
15
- NzU1MjhhNjU4NzVlNDc0NGIxNjg1NzZjMTliMGY4NTM1YzlmYmM=
6
+ metadata.gz: 9db8b4905c290ffd6570c2f1b2a27802947214a8fbc1dafea58042cb83d06c80a7632a475c20d352d509678a2fb15730511e965df25b32ebf4ea2f6f7c5ede08
7
+ data.tar.gz: bde35f4d766636db84404ac2f6c0a9b4823f74acaa9ad25847db798b4ac49ebb86a3058dd33fd463209eca4bd6198e55af48dee8df09970839533611da926b59
@@ -1,6 +1,3 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
- - jruby-19mode
5
- - 2.1.0
6
- - jruby-head
3
+ - 2.3.0
data/Gemfile CHANGED
@@ -1,4 +1,8 @@
1
1
  source "http://rubygems.org"
2
+ group :test do
3
+ gem 'coveralls', '0.8.5', require: false # Test coverage website. Go to https://coveralls.io
4
+ gem 'tins', '< 1.3.4', platforms: [:ruby_19, :jruby]
5
+ gem 'term-ansicolor', '< 1.4.0', platforms: [:ruby_19, :jruby]
6
+ end
2
7
 
3
- # Specify your gem's dependencies in activeresource-response.gemspec
4
8
  gemspec
@@ -0,0 +1,170 @@
1
+ ## Activeresource-response
2
+ ####This gem adds possibility to access http response (Net::HTTPResponse) object from result (single object or collection) of activeresource call (methods : find, all, first, last, get )
3
+
4
+ [![Build Status](http://img.shields.io/travis/Fivell/activeresource-response.png)](https://travis-ci.org/Fivell/activeresource-response)
5
+ [![Coverage Status](http://img.shields.io/coveralls/Fivell/activeresource-response.svg)](https://coveralls.io/r/Fivell/activeresource-response)
6
+
7
+
8
+ #### Why It can be used?
9
+ Such functionallity can be used for easily implementing pagination in a REST API so that an ActiveResource client can navigate paginated results.
10
+
11
+ #### How to use?
12
+ Add dependency to your Gemfile
13
+
14
+ ```ruby
15
+ gem "activeresource-response"
16
+ ```
17
+
18
+ Just open your ActiveResource class and add
19
+
20
+ ```ruby
21
+ add_response_method :your_method_name
22
+ ```
23
+
24
+ You can add method to ActiveResource::Base to use it in all subclasses
25
+
26
+ ```ruby
27
+ class ActiveResource::Base
28
+ add_response_method :my_response
29
+ end
30
+ ```
31
+
32
+ You can remove method from ActiveResource subclass
33
+
34
+ ```ruby
35
+ class Order < ActiveResource::Base
36
+ remove_response_method
37
+ end
38
+ ```
39
+
40
+ ## Full example of usage with kaminari gem
41
+
42
+ ActiveResource Class
43
+
44
+ ```ruby
45
+ class Order < ActiveResource::Base
46
+ self.format = :json
47
+ self.site = 'http://0.0.0.0:3000/'
48
+ self.element_name = "order"
49
+ add_response_method :http_response # our new method for returned objects
50
+ end
51
+ ```
52
+
53
+ ```ruby
54
+ Server Side
55
+
56
+ class OrdersController < ApplicationController
57
+ def index
58
+ @orders = Order.page(params[:page]).per(params[:per_page] || 10) #default 10 per page
59
+ response.headers["X-total"] = @orders.total_count.to_s
60
+ response.headers["X-offset"] = @orders.offset_value.to_s
61
+ response.headers["X-limit"] = @orders.limit_value.to_s
62
+ respond_with(@orders)
63
+ end
64
+ end
65
+ ```
66
+
67
+ Client Side
68
+
69
+ ```ruby
70
+ class OrdersController < ApplicationController
71
+ def index
72
+ orders = Order.all(params: params)
73
+ @orders = Kaminari::PaginatableArray.new(orders,{
74
+ limit: orders.http_response['X-limit'].to_i,
75
+ offset: orders.http_response['X-offset'].to_i,
76
+ total_count: orders.http_response['X-total'].to_i
77
+ })
78
+ end
79
+ end
80
+ ```
81
+
82
+ ### will_paginate compatibility
83
+ will_paginate has a little different API, so to populate headers
84
+
85
+ ```ruby
86
+ response.headers["X-total"] = @orders.total_entries.to_s
87
+ response.headers["X-offset"] = @orders.offset.to_s
88
+ response.headers["X-limit"] = @orders.per_page.to_s
89
+ ```
90
+
91
+ On the API client side you might also use will_paginate, in that case you can just require will_paginate/array (in initializer for example)
92
+ ```ruby
93
+ orders = Order.all(params: params)
94
+ @orders = WillPaginate::Collection.create(params[:page] || 1, params[:per_page] || 10, orders.http_response['X-total'].to_i) do |pager|
95
+ pager.replace orders
96
+ end
97
+ ```
98
+
99
+ ### Every time when http connection invoked ActiveResource connection object stores http response. You can access it with http_response method.
100
+ Example
101
+ ```ruby
102
+ class Order < ActiveResource::Base
103
+ self.site = 'http://0.0.0.0:3000/'
104
+ self.element_name = "order"
105
+ add_response_method :my_response # our new method
106
+ end
107
+
108
+ orders = Order.all
109
+ first_order = Order.find(1)
110
+ #see Net::HTTPResponse#[] method
111
+ orders.my_response['content-length']
112
+ # => "3831"
113
+ first_order.my_response['content-length']
114
+ #=> "260"
115
+ #connection also always has last http response object , to access it use http_response method
116
+ Order.connection.http_response.to_hash
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"]}
118
+ ```
119
+
120
+ ### Custom get method
121
+ You can access response from result of custom get method
122
+ Example
123
+ ```ruby
124
+ class Country < ActiveResource::Base
125
+ self.site = 'http://0.0.0.0:3000/'
126
+ add_response_method :http # our new method
127
+ end
128
+ cities = Country.find(1).get(:cities)
129
+ cities.http #method from Country class is available
130
+ ```
131
+
132
+ ### Headers and cookies methods
133
+ You can get cookies and headers from response
134
+ Example
135
+ ```ruby
136
+ class Country < ActiveResource::Base
137
+ self.site = 'http://0.0.0.0:3000/'
138
+ add_response_method :my_response # our new method
139
+ end
140
+ countries = Country.all
141
+ countries.my_response.headers
142
+
143
+ # collection with symbolized keys => {:content_type=>["application/json; charset=utf-8"], :x_ua_compatible=>["IE=Edge"], ..., :set_cookie=>["bar=foo; path=/", "foo=bar; path=/"]}
144
+
145
+ countries.my_response.cookies
146
+ # => {"bar"=>"foo", "foo"=>"bar"}
147
+ ```
148
+
149
+ ### Note About Http response
150
+ http response is object of ```Net::HTTPOK```, ```Net::HTTPClientError``` or one of other subclasses
151
+ 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
152
+
153
+ ### Testing with ActiveResource::HttpMock
154
+ Add this line to your test to patch http_mock
155
+
156
+ ```ruby
157
+ require "active_resource_response/http_mock"
158
+ ```
159
+
160
+ ### Contributing
161
+ Fork it
162
+ Create your feature branch (git checkout -b my-new-feature)
163
+ Commit your changes (git commit -am 'Add some feature')
164
+ Push to the branch (git push origin my-new-feature)
165
+ Create new Pull Request
166
+
167
+
168
+ #### Please, feel free to contact me if you have any questions
169
+ fedoronchuk(at)gmail.com
170
+
@@ -13,14 +13,11 @@ Gem::Specification.new do |s|
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', ['>= 3', '< 5'])
16
+ s.add_runtime_dependency('activeresource', ['>= 3', '< 6'])
17
17
  s.add_dependency "jruby-openssl" if RUBY_PLATFORM == "java"
18
- s.add_development_dependency "test-unit",'~> 2.5'
18
+ s.add_development_dependency "minitest" , '~> 5.3'
19
19
  s.add_development_dependency 'rake', '~> 10'
20
20
 
21
- s.extra_rdoc_files = %w( README.rdoc )
22
- s.rdoc_options.concat ['--main', 'README.rdoc']
23
-
24
21
 
25
22
  s.files = `git ls-files | sed '/.gitignore/d'`.split("\n")
26
23
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -33,14 +33,22 @@ module ActiveResourceResponse
33
33
  raise
34
34
  ensure
35
35
  response.extend HttpResponse
36
- Thread.current[:ActiveResourceResponse] = response
36
+ self.http_response=(response)
37
37
  end
38
38
  end
39
39
 
40
40
  def http_response
41
- Thread.current[:ActiveResourceResponse]
41
+ http_storage[:ActiveResourceResponse]
42
+ end
43
+
44
+ def http_response=(response)
45
+ http_storage[:ActiveResourceResponse] = response
46
+ end
47
+
48
+ def http_storage
49
+ Thread.current
42
50
  end
43
51
  end
44
52
  end
45
53
  end
46
- end
54
+ end
@@ -32,6 +32,7 @@ module ActiveResourceResponse
32
32
  Hash[@headers.map{|k, value| [k, Array.wrap(value)] } ]
33
33
  end
34
34
 
35
+ remove_method :[]
35
36
  def [](key)
36
37
  @headers[key]
37
38
  end
@@ -23,6 +23,6 @@
23
23
 
24
24
  module ActiveResourceResponse
25
25
  module Version
26
- VERSION = "1.1.1"
26
+ VERSION = "1.2.0"
27
27
  end
28
28
  end
@@ -20,21 +20,9 @@
20
20
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  #++
23
+ require_relative 'test_helper'
23
24
 
24
- lib = File.expand_path("#{File.dirname(__FILE__)}/../lib")
25
- unit_tests = File.expand_path("#{File.dirname(__FILE__)}/../test")
26
- $:.unshift(lib)
27
- $:.unshift(unit_tests)
28
-
29
- require 'test/unit'
30
- require 'active_resource_response'
31
- require "fixtures/country"
32
- require "fixtures/city"
33
- require "fixtures/region"
34
- require "fixtures/street"
35
- require "active_resource_response/http_mock"
36
-
37
- class ActiveResourceResponseTest < Test::Unit::TestCase
25
+ class ActiveResourceResponseTest < MiniTest::Test
38
26
 
39
27
 
40
28
  def setup
@@ -83,17 +71,17 @@ class ActiveResourceResponseTest < Test::Unit::TestCase
83
71
  end
84
72
 
85
73
 
86
- def test_get_headers_from_custom_methods
74
+ def test_headers_from_custom
87
75
  cities = Region.get("cities")
88
76
  assert cities.respond_to?(:http_response)
89
77
  assert_equal cities.http_response.headers[:x_total].first.to_i, 2
90
78
  assert_equal cities.http_response['X-total'].to_i, 2
91
- count = Country.find(1).get("population")
79
+ population = Country.find(1).get("population")
92
80
 
93
81
  #immutable objects doing good
94
82
  some_numeric = 45000000
95
- assert_equal count, some_numeric
96
- assert count.respond_to?(:http)
83
+ assert_equal population['count'], some_numeric
84
+ assert population.respond_to?(:http)
97
85
  assert !some_numeric.respond_to?(:http)
98
86
 
99
87
  assert_equal Country.connection.http_response.headers[:x_total].first.to_i, 1
@@ -103,20 +91,17 @@ class ActiveResourceResponseTest < Test::Unit::TestCase
103
91
  assert cities.respond_to?(:http), "Cities should respond to http"
104
92
  assert_equal cities.http.headers[:x_total].first.to_i, 1, "Cities total value should be 1"
105
93
  regions_population = Region.get("population")
106
- assert_equal regions_population.to_i, 45000000
94
+ assert_equal regions_population['count'], 45000000
107
95
  cities = Region.find(1).get("cities")
108
96
  assert cities.respond_to?(:http_response)
109
97
  assert_equal cities.http_response.headers[:x_total], ['1']
110
-
111
98
  end
112
99
 
113
-
114
100
  def test_methods_without_http
115
101
  cities = City.all
116
102
  assert_kind_of City, cities.first
117
- count = cities.first.get("population")
103
+ count = cities.first.get("population")['count']
118
104
  assert_equal count.to_i, 2500000
119
-
120
105
  end
121
106
 
122
107
  def test_get_headers_from_find
@@ -131,10 +116,9 @@ class ActiveResourceResponseTest < Test::Unit::TestCase
131
116
  #from class
132
117
  assert_equal Country.http_response.cookies['foo'], 'bar'
133
118
  assert_equal Country.http_response.cookies['bar'], 'foo'
134
-
135
119
  end
136
120
 
137
- def test_get_headers_after_exception
121
+ def test_headers_after_exception
138
122
  country = Country.create(@country[:country])
139
123
  assert_equal Country.http_response.headers[:x_total], ['1']
140
124
  assert_equal Country.http_response.code, 422
@@ -154,7 +138,7 @@ class ActiveResourceResponseTest < Test::Unit::TestCase
154
138
  assert region.respond_to?(ActiveResourceResponseBase.http_response_method)
155
139
  end
156
140
 
157
- def test_active_model_naming_methods
141
+ def test_model_naming_methods
158
142
  street = Country.find(1)
159
143
  assert street.class.respond_to?(:model_name)
160
144
  end
@@ -21,25 +21,15 @@
21
21
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  #++
23
23
 
24
- lib = File.expand_path("#{File.dirname(__FILE__)}/../lib")
25
- unit_tests = File.expand_path("#{File.dirname(__FILE__)}/../test")
26
- $:.unshift(lib)
27
- $:.unshift(unit_tests)
24
+ require_relative 'test_helper'
28
25
 
29
26
 
30
- require 'active_resource_response'
31
- require "fixtures/country"
32
- require "fixtures/city"
33
- require "fixtures/region"
34
- require "fixtures/street"
35
- require "active_resource_response/http_mock"
36
-
37
27
 
38
28
  #made pass lint test because of "to_key should return nil when `persisted?` returns false" error
39
29
  require "active_resource_response/lint"
40
30
  ActiveResource::Base.send :include, ActiveResourceResponse::Lint
41
31
 
42
- class ActiveResourceResponseLintTest < Test::Unit::TestCase
32
+ class ActiveResourceResponseLintTest < MiniTest::Test
43
33
 
44
34
  include ActiveModel::Lint::Tests
45
35
 
@@ -47,8 +37,11 @@ class ActiveResourceResponseLintTest < Test::Unit::TestCase
47
37
  @street = {:street => {:id => 1, :name => "Deribasovskaya", :population => 2300}}
48
38
  ActiveResource::HttpMock.respond_to do |mock|
49
39
  mock.get "/streets/1.json", {}, @street.to_json, 200, {"X-total"=>'1'}
50
- end
40
+ end
51
41
  @model = Street.find(1)
52
42
  end
43
+
44
+
45
+
53
46
  end
54
47
 
@@ -0,0 +1,15 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+ require 'minitest/autorun'
4
+ require 'i18n'
5
+ lib = File.expand_path("#{File.dirname(__FILE__)}/../lib")
6
+ unit_tests = File.expand_path("#{File.dirname(__FILE__)}/../test")
7
+ $:.unshift(lib)
8
+ $:.unshift(unit_tests)
9
+ I18n.config.enforce_available_locales = true
10
+ require 'active_resource_response'
11
+ require "fixtures/country"
12
+ require "fixtures/city"
13
+ require "fixtures/region"
14
+ require "fixtures/street"
15
+ require "active_resource_response/http_mock"
metadata CHANGED
@@ -1,76 +1,75 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeresource-response
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Fedoronchuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-29 00:00:00.000000000 Z
11
+ date: 2018-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeresource
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3'
20
- - - <
20
+ - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '5'
22
+ version: '6'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ! '>='
27
+ - - ">="
28
28
  - !ruby/object:Gem::Version
29
29
  version: '3'
30
- - - <
30
+ - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '5'
32
+ version: '6'
33
33
  - !ruby/object:Gem::Dependency
34
- name: test-unit
34
+ name: minitest
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - ~>
37
+ - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '2.5'
39
+ version: '5.3'
40
40
  type: :development
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
- - - ~>
44
+ - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: '2.5'
46
+ version: '5.3'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rake
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - ~>
51
+ - - "~>"
52
52
  - !ruby/object:Gem::Version
53
53
  version: '10'
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
- - - ~>
58
+ - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: '10'
61
- description: ! 'This gem adds possibility to access http response object from result
61
+ description: 'This gem adds possibility to access http response object from result
62
62
  of ActiveResource::Base find method '
63
63
  email:
64
64
  - fedoronchuk@gmail.com
65
65
  executables: []
66
66
  extensions: []
67
- extra_rdoc_files:
68
- - README.rdoc
67
+ extra_rdoc_files: []
69
68
  files:
70
- - .travis.yml
69
+ - ".travis.yml"
71
70
  - Gemfile
72
71
  - LICENSE
73
- - README.rdoc
72
+ - README.md
74
73
  - Rakefile
75
74
  - activeresource-response.gemspec
76
75
  - lib/active_resource_response.rb
@@ -88,29 +87,28 @@ files:
88
87
  - test/fixtures/region.rb
89
88
  - test/fixtures/street.rb
90
89
  - test/lint_test.rb
90
+ - test/test_helper.rb
91
91
  homepage: http://fivell.github.com/activeresource-response/
92
92
  licenses:
93
93
  - MIT
94
94
  metadata: {}
95
95
  post_install_message:
96
- rdoc_options:
97
- - --main
98
- - README.rdoc
96
+ rdoc_options: []
99
97
  require_paths:
100
98
  - lib
101
99
  required_ruby_version: !ruby/object:Gem::Requirement
102
100
  requirements:
103
- - - ! '>='
101
+ - - ">="
104
102
  - !ruby/object:Gem::Version
105
103
  version: '0'
106
104
  required_rubygems_version: !ruby/object:Gem::Requirement
107
105
  requirements:
108
- - - ! '>='
106
+ - - ">="
109
107
  - !ruby/object:Gem::Version
110
108
  version: '0'
111
109
  requirements: []
112
110
  rubyforge_project:
113
- rubygems_version: 2.2.2
111
+ rubygems_version: 2.7.7
114
112
  signing_key:
115
113
  specification_version: 4
116
114
  summary: activeresource extension
@@ -121,3 +119,4 @@ test_files:
121
119
  - test/fixtures/region.rb
122
120
  - test/fixtures/street.rb
123
121
  - test/lint_test.rb
122
+ - test/test_helper.rb
@@ -1,131 +0,0 @@
1
- = Activeresource-response
2
- This gem adds possibility to access http response (Net::HTTPResponse) object from result (single object or collection) of activeresource call (methods : find, all, first, last, get )
3
-
4
- == Build status {<img src="https://secure.travis-ci.org/Fivell/activeresource-response.png" />}[http://travis-ci.org/Fivell/activeresource-response]
5
-
6
-
7
- == Why It can be used?
8
- Such functionallity can be used for easily implementing pagination in a REST API so that an ActiveResource client can navigate paginated results.
9
-
10
- == How to use?
11
- Add dependency to your Gemfile
12
-
13
- gem "activeresource-response"
14
-
15
- Just open your ActiveResource class and add
16
-
17
- add_response_method :your_method_name
18
-
19
- You can add method to ActiveResource::Base to use it in all subclasses
20
-
21
- class ActiveResource::Base
22
- add_response_method :my_response
23
- end
24
-
25
-
26
- You can remove method from ActiveResource subclass
27
-
28
- class Order < ActiveResource::Base
29
- remove_response_method
30
- end
31
-
32
-
33
- == Full example of usage with kaminari gem
34
-
35
- Rest Client
36
-
37
- class Order < ActiveResource::Base
38
- self.format = :json
39
- self.site = 'http://0.0.0.0:3000/'
40
- self.element_name = "order"
41
- add_response_method :http_response # our new method for returned objects
42
- end
43
-
44
- Server Side
45
-
46
- class OrdersController < ApplicationController
47
- def index
48
- @orders = Order.page(params[:page]).per(params[:per_page])
49
- response.headers["X-total"] = @orders.total_count.to_s
50
- response.headers["X-offset"] = @orders.offset_value.to_s
51
- response.headers["X-limit"] = @orders.limit_value.to_s
52
- respond_with(@orders)
53
- end
54
- end
55
-
56
- Client Side
57
-
58
- class OrdersController < ApplicationController
59
- def index
60
- orders = Order.all(:params=>params)
61
- @orders = Kaminari::PaginatableArray.new(
62
- orders,{
63
- :limit => orders.http_response['X-limit'].to_i,
64
- :offset =>orders.http_response['X-offset'].to_i,
65
- :total_count => orders.http_response['X-total'].to_i
66
- })
67
- end
68
- end
69
-
70
-
71
- == Every time when http connection invoked ActiveResource connection object stores http response. You can access it with http_response method.
72
- Example
73
-
74
- class Order < ActiveResource::Base
75
- self.site = 'http://0.0.0.0:3000/'
76
- self.element_name = "order"
77
- add_response_method :my_response # our new method
78
- end
79
-
80
- orders = Order.all
81
- first_order = Order.find(1)
82
- #see Net::HTTPResponse#[] method
83
- orders.my_response['content-length']
84
- # => "3831"
85
- first_order.my_response['content-length']
86
- #=> "260"
87
- #connection also always has last http response object , to access it use http_response method
88
- Order.connection.http_response.to_hash
89
- # => {"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"]}
90
-
91
- == Custom get method
92
- You can access response from result of custom get method
93
- Example
94
-
95
- class Country < ActiveResource::Base
96
- self.site = 'http://0.0.0.0:3000/'
97
- add_response_method :http # our new method
98
- end
99
- cities = Country.find(1).get(:cities)
100
- cities.http #method from Country class is available
101
-
102
-
103
- == Headers and cookies methods
104
- You can get cookies and headers from response
105
- Example
106
-
107
- class Country < ActiveResource::Base
108
- self.site = 'http://0.0.0.0:3000/'
109
- add_response_method :my_response # our new method
110
- end
111
- countries = Country.all
112
- countries.my_response.headers
113
-
114
- # collection with symbolized keys => {:content_type=>["application/json; charset=utf-8"], :x_ua_compatible=>["IE=Edge"], ..., :set_cookie=>["bar=foo; path=/", "foo=bar; path=/"]}
115
-
116
-
117
- countries.my_response.cookies
118
- # => {"bar"=>"foo", "foo"=>"bar"}
119
-
120
- == About Http response
121
- http response is object of Net::HTTPOK, Net::HTTPClientError or one of other subclasses
122
- 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
123
-
124
- == Testing with ActiveResource::HttpMock
125
- Add this line to your test to patch http_mock
126
-
127
- # require "active_resource_response/http_mock"
128
-
129
- == Please, feel free to contact me if you have any questions
130
- fedoronchuk(at)gmail.com
131
-