activeresource-response 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -87,7 +87,17 @@ Example
87
87
  Order.connection.http_response.to_hash
88
88
  # => {"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"]}
89
89
 
90
+ == Custom get method
91
+ You can access response from result of custom get method
92
+ Example
90
93
 
94
+ class Country < ActiveResource::Base
95
+ self.site = 'http://0.0.0.0:3000/'
96
+ add_response_method :http # our new method
97
+ end
98
+ cities = Country.find(1).get(:cities)
99
+ cities.http #method from Country class is available
100
+
91
101
 
92
102
  == Headers and cookies methods
93
103
  You can get cookies and headers from response
@@ -26,8 +26,10 @@ require "active_resource_response/version"
26
26
  require "active_resource_response/http_response"
27
27
  require "active_resource_response/connection"
28
28
  require "active_resource_response/response_method"
29
+ require "active_resource_response/custom_methods"
29
30
  ActiveResource::Connection.send :include, ActiveResourceResponse::Connection
30
31
  ActiveResource::Base.send :include, ActiveResourceResponse::ResponseMethod
32
+ ActiveResource::Base.send :include, ActiveResourceResponse::CustomMethods
31
33
  if defined? ActiveResource::Response
32
34
  require "active_resource_response/response"
33
35
  ActiveResource::Response.send :include, ActiveResourceResponse::Response
@@ -26,14 +26,11 @@ module ActiveResourceResponse
26
26
  base.class_eval do
27
27
  alias_method :origin_handle_response, :handle_response
28
28
  def handle_response(response)
29
-
30
29
  begin
31
30
  origin_handle_response(response)
32
31
  rescue
33
-
34
32
  raise
35
33
  ensure
36
-
37
34
  response.extend HttpResponse
38
35
  Thread.current[:ActiveResourceResponse] = response
39
36
  end
@@ -0,0 +1,39 @@
1
+ #--
2
+ # Copyright (c) 2012 Igor Fedoronchuk
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+ module ActiveResourceResponse
24
+ module CustomMethods
25
+
26
+ def get(custom_method_name, options = {})
27
+ result = super(custom_method_name, options)
28
+ if self.class.http_response_method
29
+ result.instance_variable_set(:@http_response, connection.http_response)
30
+ (class << result; self; end).send(:define_method, self.class.http_response_method ) do
31
+ @http_response
32
+ end rescue nil
33
+ end
34
+ result
35
+ end
36
+
37
+ end
38
+ end
39
+
@@ -27,7 +27,7 @@ module ActiveResourceResponse
27
27
  def self.included(base)
28
28
  base.extend ClassMethods
29
29
  end
30
-
30
+
31
31
  module ClassMethods
32
32
 
33
33
  def http_response
@@ -36,10 +36,10 @@ module ActiveResourceResponse
36
36
 
37
37
 
38
38
  def add_response_method(method_name = :http_response)
39
-
40
-
41
-
42
-
39
+
40
+ class_attribute :http_response_method
41
+ self.http_response_method = method_name
42
+
43
43
  remove_response_method if methods.map(&:to_sym).include?(:find_without_http_response)
44
44
  [:find, :get].each do |method|
45
45
  instance_eval <<-EOS
@@ -23,6 +23,6 @@
23
23
 
24
24
  module ActiveResourceResponse
25
25
  module Version
26
- VERSION = "0.3.0"
26
+ VERSION = "0.4.0"
27
27
  end
28
28
  end
@@ -16,16 +16,21 @@ class ActiveResourceResponseTest < Test::Unit::TestCase
16
16
  def setup
17
17
 
18
18
  @country = {:country => {:id => 1, :name => "Ukraine", :iso=>"UA"}}
19
-
19
+ @city = {:city => {:id => 1, :name => "Odessa", :population => 2500000 }}
20
20
  ActiveResource::HttpMock.respond_to do |mock|
21
+
22
+
21
23
  mock.get "/countries.json", {}, [@country].to_json, 200, {"X-total"=>'1'}
22
24
  mock.get "/countries/1.json", {}, @country.to_json, 200, {"X-total"=>'1', 'Set-Cookie'=>['path=/; expires=Tue, 20-Jan-2015 15:03:14 GMT, foo=bar, bar=foo']}
23
- mock.get "/countries/1/count.json", {}, {:count => 1155}.to_json, 200, {"X-total"=>'1'}
25
+ mock.get "/countries/1/population.json", {}, {:count => 45000000}.to_json, 200, {"X-total"=>'1'}
24
26
  mock.post "/countries.json" , {}, @country.to_json, 422, {"X-total"=>'1'}
25
-
27
+ mock.get "/countries/1/cities.json", {}, [@city].to_json, 200, {"X-total"=>'1'}
26
28
  end
27
29
 
28
30
 
31
+
32
+
33
+
29
34
  end
30
35
 
31
36
 
@@ -49,13 +54,15 @@ class ActiveResourceResponseTest < Test::Unit::TestCase
49
54
 
50
55
 
51
56
  def test_get_headers_from_custom_methods
52
- count = Country.find(1).get("count")
53
- assert_equal count.to_i, 1155
57
+ count = Country.find(1).get("population")
58
+ assert_equal count.to_i, 45000000
54
59
  assert_equal Country.connection.http_response['X-total'].to_i, 1
55
60
  assert_equal Country.connection.http_response.headers[:x_total].to_i, 1
56
61
  assert_equal Country.http_response['X-total'].to_i ,1
57
62
 
58
-
63
+ cities = Country.find(1).get("cities")
64
+ assert cities.respond_to?(:http)
65
+ assert_equal cities.http['X-total'].to_i, 1
59
66
  end
60
67
 
61
68
 
@@ -79,15 +86,9 @@ class ActiveResourceResponseTest < Test::Unit::TestCase
79
86
 
80
87
 
81
88
  def test_get_headers_after_exception
82
-
83
89
  Country.create(@country[:country])
84
-
85
90
  assert_equal Country.http_response['X-total'].to_i, 1
86
-
87
91
  assert_equal Country.http_response.code, 422
88
-
89
-
90
-
91
92
  end
92
93
 
93
94
 
@@ -1,4 +1,9 @@
1
- class Country < ActiveResource::Base
1
+ class ActiveResourceResponseBase < ActiveResource::Base
2
2
  self.site = "http://37s.sunrise.i:3000"
3
+ add_response_method :http_response
4
+ end
5
+
6
+
7
+ class Country < ActiveResourceResponseBase
3
8
  add_response_method :http
4
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeresource-response
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-01 00:00:00.000000000 Z
12
+ date: 2012-10-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activeresource
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &70242253517800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '3.0'
24
+ version_requirements: *70242253517800
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: test-unit
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &70242253516380 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ! '>='
@@ -37,15 +32,10 @@ dependencies:
37
32
  version: '0'
38
33
  type: :development
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
35
+ version_requirements: *70242253516380
46
36
  - !ruby/object:Gem::Dependency
47
37
  name: rake
48
- requirement: !ruby/object:Gem::Requirement
38
+ requirement: &70242253515460 !ruby/object:Gem::Requirement
49
39
  none: false
50
40
  requirements:
51
41
  - - ! '>='
@@ -53,12 +43,7 @@ dependencies:
53
43
  version: '0'
54
44
  type: :development
55
45
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
46
+ version_requirements: *70242253515460
62
47
  description: ! 'This gem adds possibility to access http response object from result
63
48
  of ActiveResource::Base find method '
64
49
  email:
@@ -76,6 +61,7 @@ files:
76
61
  - activeresource-response.gemspec
77
62
  - lib/active_resource_response.rb
78
63
  - lib/active_resource_response/connection.rb
64
+ - lib/active_resource_response/custom_methods.rb
79
65
  - lib/active_resource_response/http_response.rb
80
66
  - lib/active_resource_response/response.rb
81
67
  - lib/active_resource_response/response_method.rb
@@ -97,21 +83,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
83
  - - ! '>='
98
84
  - !ruby/object:Gem::Version
99
85
  version: '0'
100
- segments:
101
- - 0
102
- hash: 3062498070536117201
103
86
  required_rubygems_version: !ruby/object:Gem::Requirement
104
87
  none: false
105
88
  requirements:
106
89
  - - ! '>='
107
90
  - !ruby/object:Gem::Version
108
91
  version: '0'
109
- segments:
110
- - 0
111
- hash: 3062498070536117201
112
92
  requirements: []
113
93
  rubyforge_project:
114
- rubygems_version: 1.8.24
94
+ rubygems_version: 1.8.17
115
95
  signing_key:
116
96
  specification_version: 3
117
97
  summary: activeresource extension