activeresource-response 0.5.1 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -6,6 +6,7 @@ rvm:
6
6
  - jruby-19mode
7
7
  - rbx-18mode
8
8
  - rbx-19mode
9
- - ruby-head
10
9
  - 1.8.7
11
- - ree
10
+ - ree
11
+ - ruby-head
12
+ - jruby-head
data/README.rdoc CHANGED
@@ -1,5 +1,5 @@
1
1
  = Activeresource-response
2
- This gem adds possibility to access http response object from result (single object or collection) of activeresource call (methods : find, all, first, last, get )
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
3
 
4
4
  == Build status {<img src="https://secure.travis-ci.org/Fivell/activeresource-response.png" />}[http://travis-ci.org/Fivell/activeresource-response]
5
5
 
@@ -74,11 +74,12 @@ Example
74
74
  class Order < ActiveResource::Base
75
75
  self.site = 'http://0.0.0.0:3000/'
76
76
  self.element_name = "order"
77
- add_response_method :my_response # our new method
77
+ add_response_method :my_response # our new method
78
78
  end
79
79
 
80
80
  orders = Order.all
81
81
  first_order = Order.find(1)
82
+ #see Net::HTTPResponse#[] method
82
83
  orders.my_response['content-length']
83
84
  # => "3831"
84
85
  first_order.my_response['content-length']
@@ -121,12 +122,5 @@ http response is object of Net::HTTPOK, Net::HTTPClientError or one of other sub
121
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
122
123
 
123
124
 
124
- == Inspirators
125
-
126
- http://phpblog.com.ua/2012/01/rails-activeresource-i-zagolovki/
127
-
128
- http://stackoverflow.com/questions/5972429/active-resource-responses-how-to-get-them
129
-
130
-
131
125
  == Please, feel free to contact me if you have any questions
132
126
  fedoronchuk(at)gmail.com
@@ -30,8 +30,7 @@ require "active_resource_response/custom_methods"
30
30
  ActiveResource::Connection.send :include, ActiveResourceResponse::Connection
31
31
  ActiveResource::Base.send :include, ActiveResourceResponse::ResponseMethod
32
32
  ActiveResource::Base.send :include, ActiveResourceResponse::CustomMethods
33
- if defined? ActiveResource::Response
34
- require "active_resource_response/response"
35
- ActiveResource::Response.send :include, ActiveResourceResponse::Response
36
33
 
37
- end
34
+ if defined?(Rails) and Rails.env.test?
35
+ require "active_resource_response/http_mock"
36
+ end
@@ -34,9 +34,7 @@ module ActiveResourceResponse
34
34
  result = self.merge_response_to_result(result)
35
35
  end
36
36
  result
37
-
38
37
  end
39
-
40
38
  end
41
39
  end
42
40
 
@@ -20,19 +20,20 @@
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 'active_resource/http_mock'
23
24
  module ActiveResourceResponse
24
- module Response
25
- def self.included(base)
26
- base.class_eval do
27
- def to_hash
28
- @headers
29
- end
30
-
31
- # to avoid method name conflict with Response:HttpResponse:headers
32
- def [](key)
33
- @headers[key]
25
+ module HttpMock
26
+ module Response
27
+ #to avoid methods conflict with Net:HttpResponse and ActiveResource::Response (HttpMock)
28
+ def self.included(base)
29
+ base.class_eval do
30
+ def to_hash
31
+ @headers
32
+ end
34
33
  end
35
34
  end
36
35
  end
37
36
  end
38
37
  end
38
+
39
+ ActiveResource::Response.send :include, ActiveResourceResponse::HttpMock::Response
@@ -26,7 +26,6 @@ module ActiveResourceResponse
26
26
 
27
27
  def self.included(base)
28
28
  base.extend ClassMethods
29
-
30
29
  end
31
30
 
32
31
  module ClassMethods
@@ -36,26 +35,18 @@ module ActiveResourceResponse
36
35
  end
37
36
 
38
37
  def add_response_method(method_name = :http_response)
39
-
40
38
  class_attribute :http_response_method
41
- self.http_response_method = method_name
42
-
43
-
44
-
39
+ self.http_response_method = method_name
45
40
  class << self
46
41
  alias :find_without_http_response :find
47
42
 
48
43
  def find(*arguments)
49
44
  result = find_without_http_response(*arguments)
50
45
  self.merge_response_to_result(result)
51
-
52
46
  end
53
47
  end unless methods.map(&:to_sym).include?(:find_without_http_response)
54
-
55
48
  end
56
-
57
-
58
-
49
+
59
50
  def remove_response_method
60
51
  class << self
61
52
  undef :find
@@ -63,20 +54,19 @@ module ActiveResourceResponse
63
54
  undef :find_without_http_response
64
55
  undef :http_response_method
65
56
  undef :http_response_method=
66
-
67
57
  end
68
58
  end
69
59
 
70
60
  def merge_response_to_result(result)
71
- result.instance_variable_set(:@http_response, connection.http_response)
72
- (class << result; self; end).send(:define_method, self.http_response_method) do
73
- @http_response
74
- end rescue nil
75
-
61
+ begin
62
+ result.instance_variable_set(:@http_response, connection.http_response)
63
+ (class << result; self; end).send(:define_method, self.http_response_method) do
64
+ @http_response
65
+ end
66
+ rescue StandardError
67
+ end
76
68
  result
77
69
  end
78
-
79
-
80
70
  end
81
71
  end
82
72
  end
@@ -23,6 +23,6 @@
23
23
 
24
24
  module ActiveResourceResponse
25
25
  module Version
26
- VERSION = "0.5.1"
26
+ VERSION = "0.5.3"
27
27
  end
28
28
  end
@@ -27,26 +27,23 @@ $:.unshift(lib)
27
27
  $:.unshift(unit_tests)
28
28
 
29
29
  require 'test/unit'
30
- require 'active_resource'
31
- require 'active_resource/http_mock'
32
30
  require 'active_resource_response'
33
31
  require "fixtures/country"
34
32
  require "fixtures/city"
35
33
  require "fixtures/region"
36
34
  require "fixtures/street"
37
-
35
+ require "active_resource_response/http_mock"
38
36
  class ActiveResourceResponseTest < Test::Unit::TestCase
39
37
 
40
38
 
41
39
  def setup
42
-
43
40
  @country = {:country => {:id => 1, :name => "Ukraine", :iso=>"UA"}}
41
+ @country_create_error = {"errors"=>{:base => ["Country exists"]}}
44
42
  @city = {:city => {:id => 1, :name => "Odessa", :population => 2500000}}
45
43
  @region = {:region => {:id => 1, :name => "Odessa region", :population => 4500000}}
46
44
  @street = {:street => {:id => 1, :name => "Deribasovskaya", :population => 2300}}
45
+
47
46
  ActiveResource::HttpMock.respond_to do |mock|
48
-
49
-
50
47
  mock.get "/countries.json", {}, [@country].to_json, 200, {"X-total"=>'1'}
51
48
  mock.get "/regions.json", {}, [@region].to_json, 200, {"X-total"=>'1'}
52
49
  mock.get "/regions/1.json", {}, @region.to_json, 200, {"X-total"=>'1'}
@@ -64,8 +61,6 @@ class ActiveResourceResponseTest < Test::Unit::TestCase
64
61
  mock.get "/streets/1/city.json", {}, @city.to_json, 200, {"X-total"=>'1'}
65
62
  mock.get "/streets/1.json", {}, @street.to_json, 200, {"X-total"=>'1'}
66
63
  end
67
-
68
-
69
64
  end
70
65
 
71
66
 
@@ -75,46 +70,34 @@ class ActiveResourceResponseTest < Test::Unit::TestCase
75
70
  assert countries.http.respond_to?(:cookies)
76
71
  assert countries.http.respond_to?(:headers)
77
72
  assert Country.respond_to?(:http_response)
78
-
79
73
  regions = Region.all
80
74
  assert regions.respond_to?(:http_response)
81
-
82
75
  end
83
76
 
84
77
  def test_get_headers_from_all
85
78
  countries = Country.all
86
79
  assert_kind_of Country, countries.first
87
80
  assert_equal "UA", countries.first.iso
88
-
89
- assert_equal countries.http['X-total'].to_i, 1
90
81
  assert_equal countries.http.headers[:x_total].to_i, 1
91
-
92
82
  end
93
83
 
94
84
 
95
85
  def test_get_headers_from_custom_methods
96
86
  cities = Region.get("cities")
97
-
98
87
  assert cities.respond_to?(:http_response)
99
- assert_equal cities.http_response['X-total'].to_i, 2
100
-
101
-
88
+ assert_equal cities.http_response.headers[:x_total].to_i, 2
102
89
  count = Country.find(1).get("population")
103
90
  assert_equal count.to_i, 45000000
104
- assert_equal Country.connection.http_response['X-total'].to_i, 1
105
91
  assert_equal Country.connection.http_response.headers[:x_total].to_i, 1
106
- assert_equal Country.http_response['X-total'].to_i, 1
92
+ assert_equal Country.http_response.headers[:x_total].to_i, 1
107
93
  cities = Country.find(1).get("cities")
108
94
  assert cities.respond_to?(:http), "Cities should respond to http"
109
- assert_equal cities.http['X-total'].to_i, 1, "Cities total value should be 1"
110
-
95
+ assert_equal cities.http.headers[:x_total].to_i, 1, "Cities total value should be 1"
111
96
  regions_population = Region.get("population")
112
97
  assert_equal regions_population.to_i, 45000000
113
-
114
98
  cities = Region.find(1).get("cities")
115
99
  assert cities.respond_to?(:http_response)
116
- assert_equal cities.http_response['X-total'].to_i, 1
117
-
100
+ assert_equal cities.http_response.headers[:x_total].to_i, 1
118
101
 
119
102
  end
120
103
 
@@ -124,18 +107,13 @@ class ActiveResourceResponseTest < Test::Unit::TestCase
124
107
  assert_kind_of City, cities.first
125
108
  count = cities.first.get("population")
126
109
  assert_equal count.to_i, 2500000
127
-
128
110
  end
129
111
 
130
-
131
112
  def test_get_headers_from_find
132
113
  country = Country.find(1)
133
- assert_equal country.http['X-total'].to_i, 1
134
114
  assert_equal country.http.headers[:x_total].to_i, 1
135
-
136
115
  end
137
116
 
138
-
139
117
  def test_get_cookies
140
118
  country = Country.find(1)
141
119
  assert_equal country.http.cookies['foo'], 'bar'
@@ -146,16 +124,13 @@ class ActiveResourceResponseTest < Test::Unit::TestCase
146
124
 
147
125
  end
148
126
 
149
-
150
127
  def test_get_headers_after_exception
151
- Country.create(@country[:country])
152
- assert_equal Country.http_response['X-total'].to_i, 1
128
+ country = Country.create(@country[:country])
129
+ assert_equal Country.http_response.headers[:x_total].to_i, 1
153
130
  assert_equal Country.http_response.code, 422
154
131
  end
155
132
 
156
-
157
133
  def test_remove_method
158
-
159
134
  street = Street.find(:first)
160
135
  assert !(street.respond_to?(ActiveResourceResponseBase.http_response_method))
161
136
  city = Street.find(1).get('city')
@@ -165,9 +140,6 @@ class ActiveResourceResponseTest < Test::Unit::TestCase
165
140
  assert country.respond_to?(Country.http_response_method)
166
141
  region = Region.find(1)
167
142
  assert region.respond_to?(ActiveResourceResponseBase.http_response_method)
168
-
169
-
170
143
  end
171
144
 
172
-
173
145
  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.5.1
4
+ version: 0.5.3
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-10-19 00:00:00.000000000 Z
12
+ date: 2013-04-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activeresource
16
- requirement: &70232052579580 !ruby/object:Gem::Requirement
16
+ requirement: &70124761807700 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70232052579580
24
+ version_requirements: *70124761807700
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: test-unit
27
- requirement: &70232052578700 !ruby/object:Gem::Requirement
27
+ requirement: &70124761805760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70232052578700
35
+ version_requirements: *70124761805760
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70232052577620 !ruby/object:Gem::Requirement
38
+ requirement: &70124758023700 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70232052577620
46
+ version_requirements: *70124758023700
47
47
  description: ! 'This gem adds possibility to access http response object from result
48
48
  of ActiveResource::Base find method '
49
49
  email:
@@ -62,8 +62,8 @@ files:
62
62
  - lib/active_resource_response.rb
63
63
  - lib/active_resource_response/connection.rb
64
64
  - lib/active_resource_response/custom_methods.rb
65
+ - lib/active_resource_response/http_mock.rb
65
66
  - lib/active_resource_response/http_response.rb
66
- - lib/active_resource_response/response.rb
67
67
  - lib/active_resource_response/response_method.rb
68
68
  - lib/active_resource_response/version.rb
69
69
  - lib/activeresource-response.rb
@@ -104,3 +104,4 @@ test_files:
104
104
  - test/fixtures/country.rb
105
105
  - test/fixtures/region.rb
106
106
  - test/fixtures/street.rb
107
+ has_rdoc: