activeresource-response 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -23,6 +23,13 @@ You can add method to ActiveResource::Base to use it in all subclasses
23
23
  end
24
24
 
25
25
 
26
+ You can remove method from ActiveResource subclass
27
+
28
+ class Order < ActiveResource::Base
29
+ remove_response_method
30
+ end
31
+
32
+
26
33
  == Full example of usage with kaminari gem
27
34
 
28
35
  Rest Client
@@ -112,4 +119,4 @@ http://stackoverflow.com/questions/5972429/active-resource-responses-how-to-get-
112
119
 
113
120
 
114
121
  == Please, feel free to contact me if you have any questions
115
- fedoronchuk(at)gmail.com
122
+ fedoronchuk(at)gmail.com
data/Rakefile CHANGED
@@ -1 +1,13 @@
1
1
  require 'bundler/gem_tasks'
2
+
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+
6
+ task :default => [:test_units]
7
+
8
+ desc "Run basic tests"
9
+ Rake::TestTask.new("test_units") { |t|
10
+ t.pattern = 'test/*_test.rb'
11
+ t.verbose = true
12
+ t.warning = true
13
+ }
@@ -15,11 +15,12 @@ Gem::Specification.new do |s|
15
15
 
16
16
  s.add_dependency('activeresource', '>= 3.0')
17
17
 
18
-
18
+ s.add_development_dependency "test-unit"
19
+ s.add_development_dependency 'rake'
19
20
 
20
21
  s.extra_rdoc_files = %w( README.rdoc )
21
22
  s.rdoc_options.concat ['--main', 'README.rdoc']
22
-
23
+
23
24
 
24
25
  s.files = `git ls-files | sed '/.gitignore/d'`.split("\n")
25
26
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -4,4 +4,9 @@ require "activeresource-response/http_response"
4
4
  require "activeresource-response/connection"
5
5
  require "activeresource-response/response_method"
6
6
  ActiveResource::Connection.send :include, ActiveresourceResponse::Connection
7
- ActiveResource::Base.send :include, ActiveresourceResponse::ResponseMethod
7
+ ActiveResource::Base.send :include, ActiveresourceResponse::ResponseMethod
8
+ if defined? ActiveResource::Response
9
+ require "activeresource-response/active_resource_response"
10
+ ActiveResource::Response.send :include, ActiveResourceResponse::Response
11
+
12
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveResourceResponse
2
+ module Response
3
+ def self.included(base)
4
+ base.class_eval do
5
+ def to_hash
6
+ @headers
7
+ end
8
+ # to avoid method name conflict with ActiveresourceResponse:HttpResponse:headers
9
+ def [](key)
10
+ @headers[key]
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -9,7 +9,8 @@ module ActiveresourceResponse
9
9
  rescue StandardError => e
10
10
  raise e
11
11
  end
12
- response.extend(ActiveresourceResponse::HttpResponse)
12
+ response.extend(ActiveresourceResponse::HttpResponse)
13
+
13
14
  Thread.current[:ActiveResourceHttpResponse] = response
14
15
  end
15
16
  def http_response
@@ -1,27 +1,32 @@
1
1
  module ActiveresourceResponse
2
- module HttpResponse
3
- def headers
4
- @_active_resource_response_headers || symbolize_keys(to_hash)
5
- end
6
- def cookies
7
- unless @_active_resource_response_cookies
8
- @_active_resource_response_cookies = (self.headers[:set_cookie] || {}).inject({}) do |out, cookie_str|
9
- CGI::Cookie::parse(cookie_str).each do |key, cookie|
10
- out[key] = cookie.value.first unless ['expires', 'path'].include? key
11
- end
12
- out
13
- end
14
- end
15
- @_active_resource_response_cookies
16
- end
2
+ module HttpResponse
3
+
4
+ def headers
5
+ unless defined? @_active_resource_response_headers
6
+ @_active_resource_response_headers = symbolize_keys(to_hash)
7
+ end
8
+ @_active_resource_response_headers
9
+ end
10
+
11
+ def cookies
12
+ unless defined? @_active_resource_response_cookies
13
+ @_active_resource_response_cookies = (self.headers[:set_cookie] || {}).inject({}) do |out, cookie_str|
14
+ CGI::Cookie::parse(cookie_str).each do |key, cookie|
15
+ out[key] = cookie.value.first unless ['expires', 'path'].include? key
16
+ end
17
+ out
18
+ end
19
+ end
20
+ @_active_resource_response_cookies
21
+ end
22
+
23
+ private
24
+ def symbolize_keys(hash)
25
+ hash.inject({}) do |out, (key, value)|
26
+ out[key.gsub(/-/, '_').downcase.to_sym] = value
27
+ out
28
+ end
29
+ end
17
30
 
18
- private
19
- def symbolize_keys(hash)
20
- hash.inject({}) do |out, (key, value)|
21
- out[key.gsub(/-/, '_').downcase.to_sym] = value
22
- out
23
- end
24
- end
25
-
26
- end
31
+ end
27
32
  end
@@ -9,7 +9,7 @@ module ActiveresourceResponse
9
9
  module ClassMethods
10
10
  def add_response_method(method_name = :http_response)
11
11
 
12
- remove_response_method if methods.include?(:find_without_http_response)
12
+ remove_response_method if methods.map(&:to_sym).include?(:find_without_http_response)
13
13
  [:find, :get].each do |method|
14
14
  instance_eval <<-EOS
15
15
  alias #{method}_without_http_response #{method}
@@ -1,5 +1,5 @@
1
1
  module ActiveresourceResponse
2
2
  module Version
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -0,0 +1,73 @@
1
+ lib = File.expand_path("#{File.dirname(__FILE__)}/../lib")
2
+ test = File.expand_path("#{File.dirname(__FILE__)}/../test")
3
+ $:.unshift(lib)
4
+ $:.unshift(test)
5
+
6
+ require 'test/unit'
7
+ require 'active_resource'
8
+ require 'active_resource/http_mock'
9
+ require 'activeresource-response'
10
+ require "fixtures/country"
11
+
12
+
13
+ class ActiveResourceResponseTest < Test::Unit::TestCase
14
+
15
+
16
+ def setup
17
+
18
+ @country = {:country => {:id => 1, :name => "Ukraine", :iso=>"UA"}}
19
+ ActiveResource::HttpMock.respond_to do |mock|
20
+ mock.get "/countries.json", {}, [@country].to_json, 200, {"X-total"=>'1'}
21
+ 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']}
22
+ mock.get "/countries/1/count.json", {}, {:count => 1155}.to_json, 200, {"X-total"=>'1'}
23
+
24
+
25
+ end
26
+
27
+
28
+ end
29
+
30
+
31
+ def test_methods_appeared
32
+ countries = Country.all
33
+ assert countries.respond_to?(:http)
34
+ assert countries.http.respond_to?(:cookies)
35
+ assert countries.http.respond_to?(:headers)
36
+ end
37
+
38
+ def test_get_headers_from_all
39
+ countries = Country.all
40
+ assert_kind_of Country, countries.first
41
+ assert_equal "UA", countries.first.iso
42
+
43
+ assert_equal countries.http['X-total'].to_i, 1
44
+ assert_equal countries.http.headers[:x_total].to_i, 1
45
+
46
+ end
47
+
48
+
49
+ def test_get_headers_from_custom_methods
50
+ count = Country.find(1).get("count")
51
+ assert_equal count.to_i, 1155
52
+ assert_equal Country.connection.http_response['X-total'].to_i, 1
53
+ assert_equal Country.connection.http_response.headers[:x_total].to_i, 1
54
+
55
+ end
56
+
57
+
58
+ def test_get_headers_from_find
59
+ country = Country.find(1)
60
+ assert_equal country.http['X-total'].to_i, 1
61
+ assert_equal country.http.headers[:x_total].to_i, 1
62
+
63
+ end
64
+
65
+
66
+ def test_get_cookies
67
+ country = Country.find(1)
68
+ assert_equal country.http.cookies['foo'] , 'bar'
69
+ assert_equal country.http.cookies['bar'] , 'foo'
70
+ end
71
+
72
+
73
+ end
@@ -0,0 +1,4 @@
1
+ class Country < ActiveResource::Base
2
+ self.site = "http://37s.sunrise.i:3000"
3
+ add_response_method :http
4
+ 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.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-05 00:00:00.000000000 Z
12
+ date: 2012-07-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activeresource
@@ -27,6 +27,38 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: test-unit
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
30
62
  description: ! 'This gem adds possibility to access http response object from result
31
63
  of ActiveResource::Base find method '
32
64
  email:
@@ -42,10 +74,13 @@ files:
42
74
  - Rakefile
43
75
  - activeresource-response.gemspec
44
76
  - lib/activeresource-response.rb
77
+ - lib/activeresource-response/active_resource_response.rb
45
78
  - lib/activeresource-response/connection.rb
46
79
  - lib/activeresource-response/http_response.rb
47
80
  - lib/activeresource-response/response_method.rb
48
81
  - lib/activeresource-response/version.rb
82
+ - test/active_resource_response_test.rb
83
+ - test/fixtures/country.rb
49
84
  homepage: http://fivell.github.com/activeresource-response/
50
85
  licenses: []
51
86
  post_install_message:
@@ -68,8 +103,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
103
  version: '0'
69
104
  requirements: []
70
105
  rubyforge_project:
71
- rubygems_version: 1.8.21
106
+ rubygems_version: 1.8.24
72
107
  signing_key:
73
108
  specification_version: 3
74
109
  summary: activeresource extension
75
- test_files: []
110
+ test_files:
111
+ - test/active_resource_response_test.rb
112
+ - test/fixtures/country.rb