wrest 1.0.0.beta1 → 1.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. data/CHANGELOG +14 -2
  2. data/LICENCE +7 -0
  3. data/README.rdoc +15 -69
  4. data/lib/wrest/components/container/alias_accessors.rb +1 -1
  5. data/lib/wrest/components/container/alias_accessors.rbc +905 -0
  6. data/lib/wrest/components/container/typecaster.rb +5 -5
  7. data/lib/wrest/components/container/typecaster.rbc +1985 -0
  8. data/lib/wrest/components/container.rb +11 -12
  9. data/lib/wrest/components/container.rbc +2466 -0
  10. data/lib/wrest/components/mutators/base.rbc +702 -0
  11. data/lib/wrest/components/mutators/camel_to_snake_case.rbc +360 -0
  12. data/lib/wrest/components/mutators/xml_mini_type_caster.rbc +679 -0
  13. data/lib/wrest/components/mutators/xml_simple_type_caster.rbc +559 -0
  14. data/lib/wrest/components/mutators.rbc +650 -0
  15. data/lib/wrest/components/translators/content_types.rbc +339 -0
  16. data/lib/wrest/components/translators/json.rbc +462 -0
  17. data/lib/wrest/components/translators/xml.rbc +457 -0
  18. data/lib/wrest/components/translators.rbc +517 -0
  19. data/lib/wrest/components.rbc +217 -0
  20. data/lib/wrest/core_ext/hash/conversions.rbc +501 -0
  21. data/lib/wrest/core_ext/hash.rbc +182 -0
  22. data/lib/wrest/core_ext/string/conversions.rbc +471 -0
  23. data/lib/wrest/core_ext/string.rbc +182 -0
  24. data/lib/wrest/curl/response.rb +8 -0
  25. data/lib/wrest/curl.rbc +704 -0
  26. data/lib/wrest/exceptions.rbc +312 -0
  27. data/lib/wrest/http_shared/headers.rb +0 -7
  28. data/lib/wrest/http_shared/headers.rbc +353 -0
  29. data/lib/wrest/http_shared/standard_headers.rbc +336 -0
  30. data/lib/wrest/http_shared/standard_tokens.rbc +316 -0
  31. data/lib/wrest/http_shared.rbc +279 -0
  32. data/lib/wrest/native/connection_factory.rbc +445 -0
  33. data/lib/wrest/native/delete.rbc +371 -0
  34. data/lib/wrest/native/get.rb +26 -0
  35. data/lib/wrest/native/get.rbc +855 -0
  36. data/lib/wrest/native/options.rbc +347 -0
  37. data/lib/wrest/native/post.rbc +388 -0
  38. data/lib/wrest/native/put.rbc +388 -0
  39. data/lib/wrest/native/redirection.rbc +520 -0
  40. data/lib/wrest/native/request.rb +4 -2
  41. data/lib/wrest/native/request.rbc +1675 -0
  42. data/lib/wrest/native/response.rb +53 -2
  43. data/lib/wrest/native/response.rbc +2150 -0
  44. data/lib/wrest/native/session.rbc +1372 -0
  45. data/lib/wrest/native.rbc +513 -0
  46. data/lib/wrest/resource/base.rbc +2115 -0
  47. data/lib/wrest/resource.rbc +163 -0
  48. data/lib/wrest/uri.rbc +2599 -0
  49. data/lib/wrest/uri_template.rbc +505 -0
  50. data/lib/wrest/version.rb +1 -1
  51. data/lib/wrest/version.rbc +372 -0
  52. data/lib/wrest.rb +1 -1
  53. data/lib/wrest.rbc +1900 -0
  54. metadata +99 -102
  55. data/Rakefile +0 -495
  56. data/VERSION.yml +0 -5
  57. data/bin/jwrest +0 -3
  58. data/examples/delicious.rb +0 -72
  59. data/examples/facebook.rb +0 -101
  60. data/examples/imgur_multipart.rb +0 -24
  61. data/examples/keep_alive.rb +0 -37
  62. data/examples/redirection.rb +0 -27
  63. data/examples/twitter.rb +0 -78
  64. data/examples/twitter_public_timeline.rb +0 -32
  65. data/examples/wow_realm_status.rb +0 -67
  66. data/init.rb +0 -0
  67. data/spec/custom_matchers/custom_matchers.rb +0 -2
  68. data/spec/rcov.opts +0 -4
  69. data/spec/spec.opts.disable +0 -6
  70. data/spec/unit/spec_helper.rb +0 -35
  71. data/spec/unit/wrest/components/attributes_container/alias_accessors_spec.rb +0 -49
  72. data/spec/unit/wrest/components/attributes_container/typecaster_spec.rb +0 -83
  73. data/spec/unit/wrest/components/attributes_container_spec.rb +0 -254
  74. data/spec/unit/wrest/components/mutators/base_spec.rb +0 -42
  75. data/spec/unit/wrest/components/mutators/camel_to_snake_spec.rb +0 -22
  76. data/spec/unit/wrest/components/mutators/xml_mini_type_caster_spec.rb +0 -75
  77. data/spec/unit/wrest/components/mutators/xml_simple_type_caster_spec.rb +0 -47
  78. data/spec/unit/wrest/components/mutators_spec.rb +0 -21
  79. data/spec/unit/wrest/components/translators/xml_spec.rb +0 -16
  80. data/spec/unit/wrest/components/translators_spec.rb +0 -18
  81. data/spec/unit/wrest/core_ext/hash/conversions_spec.rb +0 -22
  82. data/spec/unit/wrest/core_ext/string/conversions_spec.rb +0 -39
  83. data/spec/unit/wrest/curl/request_spec.rb +0 -19
  84. data/spec/unit/wrest/curl/response_spec.rb +0 -16
  85. data/spec/unit/wrest/http/response_spec.rb +0 -24
  86. data/spec/unit/wrest/native/redirection_spec.rb +0 -42
  87. data/spec/unit/wrest/native/request_spec.rb +0 -71
  88. data/spec/unit/wrest/native/response_spec.rb +0 -72
  89. data/spec/unit/wrest/native/session_spec.rb +0 -74
  90. data/spec/unit/wrest/resource/base_spec.rb +0 -274
  91. data/spec/unit/wrest/uri_spec.rb +0 -273
  92. data/spec/unit/wrest/uri_template_spec.rb +0 -38
  93. data/wrest.gemspec +0 -168
@@ -1,42 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper'
2
-
3
- describe Wrest::Native::Redirection do
4
-
5
- it "should make a request to the url in its location header and return the response" do
6
- mock_net_http_response = mock(Net::HTTPRedirection)
7
- redirect_url = 'http://redirect.com'
8
- redirect_uri = redirect_url.to_uri
9
- mock_net_http_response.should_receive(:[]).with('location').and_return(redirect_url)
10
- mock_net_http_response.should_receive(:code).and_return('200')
11
-
12
- Wrest::Uri.should_receive(:new).with(redirect_url, anything).and_return(redirect_uri)
13
-
14
-
15
- after_redirect_request = Wrest::Native::Get.new(redirect_uri)
16
- final_mock_response = mock(Wrest::Native::Response)
17
- after_redirect_request.should_receive(:invoke).and_return(final_mock_response)
18
-
19
- Wrest::Native::Get.should_receive(:new).with(redirect_uri, {}, {}, {:username=>nil, :password=>nil}).and_return(after_redirect_request)
20
-
21
- response = Wrest::Native::Redirection.new(mock_net_http_response)
22
- response.follow(:follow_redirects_count => 0, :follow_redirects_limit => 5).should == final_mock_response
23
- end
24
-
25
- it "should raise a Wrest::Exceptions::AutoRedirectLimitExceeded if there are more redirections than the limit" do
26
- request_url = 'http://redirect.com'
27
-
28
- response = mock(Net::HTTPRedirection)
29
- response.stub!(:code).and_return('301')
30
- response.stub!(:message).and_return('')
31
- response.stub!(:body).and_return('')
32
- response.should_receive(:[]).with('location').exactly(5).times.and_return(request_url)
33
-
34
- http_connection = mock(Net::HTTP)
35
- http_connection.stub!(:read_timeout=)
36
- http_connection.should_receive(:request).exactly(5).times.and_return(response)
37
-
38
- Net::HTTP.should_receive(:new).exactly(5).times.and_return(http_connection)
39
-
40
- lambda{ request_url.to_uri(:follow_redirects_limit => 5).get }.should raise_error(Wrest::Exceptions::AutoRedirectLimitExceeded)
41
- end
42
- end
@@ -1,71 +0,0 @@
1
- # Copyright 2009 Sidu Ponnappa
2
-
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- # Unless required by applicable law or agreed to in writing, software distributed under the License
7
- # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8
- # See the License for the specific language governing permissions and limitations under the License.
9
-
10
- require File.dirname(__FILE__) + '/../../spec_helper'
11
-
12
- describe Wrest::Native::Request do
13
- it "should convert all symbols in header keys to strings" do
14
- Wrest::Native::Request.new(
15
- 'http://localhost/foo'.to_uri, Net::HTTP::Get, {},
16
- nil, 'Content-Type' => 'application/xml', :per_page => '10'
17
- ).headers.should == {
18
- 'Content-Type' => 'application/xml',
19
- 'per_page' => '10'
20
- }
21
- end
22
-
23
- it "should default the 'follow_redirects' option to true for a Get" do
24
- Wrest::Native::Get.new('http://localhost/foo'.to_uri).follow_redirects.should be_true
25
- end
26
-
27
- it "should default the 'follow_redirects_count' option to 0" do
28
- Wrest::Native::Get.new('http://localhost/foo'.to_uri).follow_redirects_count.should == 0
29
- end
30
-
31
- it "should default the 'follow_redirects_limit' option to 5" do
32
- Wrest::Native::Get.new('http://localhost/foo'.to_uri).follow_redirects_limit.should == 5
33
- end
34
-
35
- it "should allow Gets to disable redirect follows" do
36
- Wrest::Native::Get.new('http://localhost/foo'.to_uri, {}, {}, {:follow_redirects => false}).follow_redirects.should be_false
37
- end
38
-
39
- it "should increment the follow_redirects_count for every redirect leving the count unaffected in previous requests" do
40
- uri = 'http://localhost/foo'.to_uri
41
- request = Wrest::Native::Get.new(uri)
42
- redirect_location = 'http://coathangers.com'
43
- redirected_request = mock('Request to http://coathangers.com')
44
-
45
- mock_connection = mock('Http Connection')
46
- uri.stub!(:create_connection).and_return(mock_connection)
47
-
48
- raw_response = mock(Net::HTTPRedirection)
49
- raw_response.stub!(:code).and_return('301')
50
- raw_response.stub!(:message).and_return('')
51
- raw_response.stub!(:body).and_return('')
52
- raw_response.stub!(:[]).with('location').and_return(redirect_location)
53
-
54
- response = Wrest::Native::Redirection.new(raw_response)
55
-
56
- mock_connection.should_receive(:request).and_return(raw_response)
57
-
58
- Wrest::Native::Response.should_receive(:new).and_return(response)
59
- redirected_request.stub!(:get)
60
- redirect_location.should_receive(:to_uri).with(hash_including(:follow_redirects_count=>1)).and_return(redirected_request)
61
-
62
- request.invoke
63
- request.follow_redirects_count.should == 0
64
- end
65
-
66
- it "should default the 'follow_redirects' option to false for a Post, Put or Delete" do
67
- Wrest::Native::Post.new('http://localhost/foo'.to_uri).follow_redirects.should_not be_true
68
- Wrest::Native::Put.new('http://localhost/foo'.to_uri).follow_redirects.should_not be_true
69
- Wrest::Native::Delete.new('http://localhost/foo'.to_uri).follow_redirects.should_not be_true
70
- end
71
- end
@@ -1,72 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper'
2
-
3
- module Wrest
4
- describe Native::Response do
5
- it "should build a Redirection instead of a normal response if the code is 301..303 or 305..3xx" do
6
- http_response = mock(Net::HTTPRedirection)
7
- http_response.stub!(:code).and_return('301')
8
-
9
- Native::Response.new(http_response).class.should == Wrest::Native::Redirection
10
- end
11
-
12
- it "should build a normal response if the code is 304" do
13
- http_response = mock(Net::HTTPRedirection)
14
- http_response.stub!(:code).and_return('304')
15
-
16
- Native::Response.new(http_response).class.should == Wrest::Native::Response
17
- end
18
-
19
- it "should build a normal Response for non 3xx codes" do
20
- http_response = mock(Net::HTTPResponse)
21
- http_response.stub!(:code).and_return('200')
22
-
23
- Native::Response.new(http_response).class.should == Wrest::Native::Response
24
- end
25
-
26
- it "should know how to delegate to a translator" do
27
- http_response = mock('response')
28
- http_response.stub!(:code).and_return('200')
29
- Components::Translators::Xml.should_receive(:deserialise).with(http_response)
30
- Native::Response.new(http_response).deserialise_using(Components::Translators::Xml)
31
- end
32
-
33
- it "should know how to load a translator based on content type" do
34
- http_response = mock('response')
35
- http_response.stub!(:code).and_return('422')
36
- http_response.should_receive(:content_type).and_return('application/xml')
37
-
38
- response = Native::Response.new(http_response)
39
- response.should_receive(:deserialise_using).with(Components::Translators::Xml)
40
-
41
- response.deserialise
42
- end
43
-
44
- it "should simply return itself when asked to follow (null object behaviour - see MovedPermanently for more context)" do
45
- http_response = mock('response')
46
- http_response.stub!(:code).and_return('422')
47
-
48
- response = Native::Response.new(http_response)
49
- response.follow.equal?(response).should be_true
50
- end
51
-
52
- describe 'Keep-Alive' do
53
- it "should know when a connection has been closed" do
54
- http_response = mock('response')
55
- http_response.stub!(:code).and_return('200')
56
- http_response.should_receive(:[]).with(Wrest::H::Connection).and_return('Close')
57
-
58
- response = Native::Response.new(http_response)
59
- response.should be_connection_closed
60
- end
61
-
62
- it "should know when a keep-alive connection has been estalished" do
63
- http_response = mock('response')
64
- http_response.stub!(:code).and_return('200')
65
- http_response.should_receive(:[]).with(Wrest::H::Connection).and_return('')
66
-
67
- response = Native::Response.new(http_response)
68
- response.should_not be_connection_closed
69
- end
70
- end
71
- end
72
- end
@@ -1,74 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper'
2
-
3
- module Wrest
4
- describe Native::Session do
5
- describe 'Construction' do
6
- it "should accept a string uri and convert it to a Wrest::Uri" do
7
- uri = "http://localhost:3000"
8
- Native::Session.new(uri).uri.should == uri.to_uri
9
- end
10
-
11
- it "should accept a Wrest::Uri" do
12
- uri = "http://localhost:3000"
13
- Native::Session.new(uri.to_uri).uri.should == uri.to_uri
14
- end
15
- end
16
-
17
- it "should know how to use the connection provided to make requests" do
18
- uri = "http://localhost:3000".to_uri
19
- uri.should_not be_https
20
-
21
- http = mock(Net::HTTP)
22
- Net::HTTP.should_receive(:new).with('localhost', 3000).and_return(http)
23
- http.should_receive(:read_timeout=).with(60)
24
-
25
- request_one = Net::HTTP::Get.new('/glassware?owner=Kai&type=bottle', {H::Connection=>T::KeepAlive})
26
- request_two = Net::HTTP::Get.new('/bottles.xml', {H::Connection=>T::KeepAlive})
27
-
28
- Net::HTTP::Get.should_receive(:new).with('/glassware?owner=Kai&type=bottle', {H::Connection=>T::KeepAlive}).and_return(request_one)
29
- Net::HTTP::Get.should_receive(:new).with('/bottles.xml', {H::Connection=>T::KeepAlive}).and_return(request_two)
30
-
31
- ok_response = build_ok_response
32
- ok_response.should_receive(:[]).with(Native::StandardHeaders::Connection).twice.and_return(Native::StandardTokens::KeepAlive)
33
-
34
- http.should_receive(:request).with(request_one, nil).and_return(ok_response)
35
- http.should_receive(:request).with(request_two, nil).and_return(ok_response)
36
-
37
- Native::Session.new(uri) do |session|
38
- session.get('/glassware', ActiveSupport::OrderedHash.new.merge!([[:owner, 'Kai'],[:type, 'bottle']]))
39
- session.get '/bottles.xml'
40
- end
41
- end
42
-
43
- it "should destroy the current connection if a response is returned with a Connection: Close" do
44
- uri = "http://localhost:3000".to_uri
45
- uri.should_not be_https
46
-
47
- http = mock(Net::HTTP)
48
- Net::HTTP.should_receive(:new).with('localhost', 3000).and_return(http)
49
- http.should_receive(:read_timeout=).with(60)
50
-
51
- request_one = Net::HTTP::Get.new('/glassware?owner=Kai&type=bottle', {H::Connection=>T::KeepAlive})
52
- request_two = Net::HTTP::Get.new('/bottles.xml', {H::Connection=>T::KeepAlive})
53
-
54
- Net::HTTP::Get.should_receive(:new).with('/glassware?owner=Kai&type=bottle', {H::Connection=>T::KeepAlive}).and_return(request_one)
55
- Net::HTTP::Get.should_receive(:new).with('/bottles.xml', {H::Connection=>T::KeepAlive}).and_return(request_two)
56
-
57
- ok_response = build_ok_response
58
- ok_response.should_receive(:[]).with(Native::StandardHeaders::Connection).once.and_return(Native::StandardTokens::KeepAlive)
59
-
60
- ok_response_with_connection_close = build_ok_response
61
- ok_response_with_connection_close.should_receive(:[]).with(Native::StandardHeaders::Connection).once.and_return(Native::StandardTokens::Close)
62
-
63
- http.should_receive(:request).with(request_one, nil).and_return(ok_response)
64
- http.should_receive(:request).with(request_two, nil).and_return(ok_response_with_connection_close)
65
-
66
- Native::Session.new(uri) do |session|
67
- session.get('/glassware', ActiveSupport::OrderedHash.new.merge!([[:owner, 'Kai'],[:type, 'bottle']]))
68
- session.instance_variable_get('@connection').should == http
69
- session.get '/bottles.xml'
70
- session.instance_variable_get('@connection').should be_nil
71
- end
72
- end
73
- end
74
- end
@@ -1,274 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../spec_helper'
2
-
3
- class Glassware < Wrest::Resource::Base
4
- set_host "http://localhost:3000"
5
- end
6
-
7
- class BottledUniverse < Glassware
8
- set_host "http://localhost:3001"
9
- set_default_format :xml
10
- end
11
-
12
- module Wrest
13
- describe Wrest::Resource::Base do
14
- it "should not affect other classes when setting up its macros" do
15
- Class.should_not respond_to(:host=)
16
- Object.should_not respond_to(:host=)
17
- end
18
-
19
- it "should not affect itself when subclasses use its macros" do
20
- Resource::Base.should_not respond_to(:host)
21
- end
22
-
23
- describe 'subclasses' do
24
- before(:each) do
25
- @BottledUniverse = Class.new(Wrest::Resource::Base)
26
- @BottledUniverse.class_eval do
27
- set_resource_name 'BottledUniverse'
28
- end
29
- end
30
-
31
- describe 'equality' do
32
- it "should be equal if it is the same instance" do
33
- universe = @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
34
- (universe == universe).should be_true
35
- end
36
-
37
- it "should be equal if it has the same state" do
38
- (
39
- @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1) == @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
40
- ).should be_true
41
- end
42
-
43
- it "should not be equal to nil" do
44
- (@BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1) == nil).should be_false
45
- end
46
-
47
- it "should not be equal if it is not the same class" do
48
- (
49
- @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1) == Glassware.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
50
- ).should be_false
51
- end
52
-
53
- it "should not be equal if it is has a different state" do
54
- (
55
- @BottledUniverse.new(:universe_id=>3, :name=>"Wooz", :id=>1) == @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
56
- ).should be_false
57
- end
58
-
59
- it "should be symmetric" do
60
- universe_one = @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
61
- universe_two = @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
62
- (universe_one == universe_one).should be_true
63
- (universe_two == universe_two).should be_true
64
- end
65
-
66
- it "should be transitive" do
67
- universe_one = @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
68
- universe_two = @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
69
- universe_three = @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
70
- (universe_one == universe_two).should be_true
71
- (universe_two == universe_three).should be_true
72
- (universe_one == universe_three).should be_true
73
- end
74
-
75
- it "should ensure that the hashcode is a fixnum" do
76
- @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1).hash.should be_kind_of(Fixnum)
77
- end
78
-
79
- it "should ensure that instances with the same ids have the same hashcode" do
80
- universe_one = @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
81
- universe_two = @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
82
- universe_one.hash.should == universe_two.hash
83
- end
84
-
85
- it "should ensure that instances with different ids have the different hashcodes" do
86
- universe_one = @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
87
- universe_two = @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>2)
88
- universe_one.hash.should_not == universe_two.hash
89
- end
90
- end
91
-
92
- it "should know its name as a resource by default" do
93
- BottledUniverse.resource_name.should == 'bottled_universe'
94
- end
95
-
96
- it "should allow its name as a resource to be configured for anonymous classes" do
97
- @BottledUniverse.resource_name.should == 'bottled_universe'
98
- end
99
-
100
- it "should know how to create an instance using deserialised attributes" do
101
- universe = @BottledUniverse.new "name"=>"Wooz", "id"=>'1', "universe_id"=>nil, 'owner_id'=>nil
102
- universe.name.should == "Wooz"
103
- universe.owner_id.should be_nil
104
- universe.id.should == 1
105
- end
106
-
107
- it "should allow instantiation with no attributes" do
108
- lambda{ @BottledUniverse.new }.should_not raise_error
109
- end
110
-
111
- it "should have a method to set the host url" do
112
- @BottledUniverse.should respond_to(:set_host)
113
- end
114
-
115
- it "should have a method to retrive the host url after it is set" do
116
- @BottledUniverse.class_eval{ set_host "http://localhost:3000" }
117
- @BottledUniverse.should respond_to(:host)
118
- end
119
-
120
- it "should know what its site is" do
121
- @BottledUniverse.class_eval{ set_host "http://localhost:3000" }
122
- @BottledUniverse.host.should == "http://localhost:3000"
123
- end
124
-
125
- it "should not use the same string" do
126
- url = "http://localhost:3000"
127
- @BottledUniverse.class_eval{ set_host url }
128
- url.upcase!
129
- @BottledUniverse.host.should == "http://localhost:3000"
130
- end
131
-
132
- it "should know its resource collection name" do
133
- Glassware.resource_collection_name.should == 'glasswares'
134
- end
135
-
136
- it "should know its uri template for find one" do
137
- Glassware.find_one_uri_template.to_uri(
138
- :host => 'http://localhost:3000',
139
- :resource_collection_name => 'glasswares',
140
- :id => 1,
141
- :format => 'json'
142
- ).should == 'http://localhost:3000/glasswares/1.json'.to_uri
143
- end
144
-
145
- it "should know how to serialise itself to xml" do
146
- BottledUniverse.new(:name => 'Foo').to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bottled-universe>\n <name>Foo</name>\n</bottled-universe>\n"
147
- end
148
-
149
- describe 'finders' do
150
- # Json =>
151
- # body => {"lead_bottle": {"name": "Wooz", "id": 1, "universe_id": null}}
152
- # hash => {"lead_bottle"=>{"name"=>"Wooz", "id"=>1, "universe_id"=>nil}}
153
- # Xml =>
154
- # body =>
155
- # <?xml version="1.0" encoding="UTF-8"?>
156
- # <lead-bottle>
157
- # <id type="integer">1</id>
158
- # <name>Wooz</name>
159
- # <universe-id type="integer" nil="true"></universe-id>
160
- # </lead-bottle>
161
- # hash =>
162
- # {"lead-bottle"=>{"name"=>{"__content__"=>"Wooz"}, "universe-id"=>{"type"=>"integer", "nil"=>"true"}, "id"=>{"__content__"=>"1", "type"=>"integer"}}}
163
- # typecast =>
164
- # {"lead_bottle"=>{"name"=>"Wooz", "id"=>1, "universe_id"=>nil}}
165
- it "should know how to find a resource by id" do
166
- uri = 'http://localhost:3001/bottled_universe/1.xml'.to_uri
167
- Wrest::Uri.should_receive(:new).with('http://localhost:3001/bottled_universes/1.xml', {}).and_return(uri)
168
- response = mock(Wrest::Native::Response)
169
- uri.should_receive(:get).with(no_args).and_return(response)
170
- response.should_receive(:deserialise).and_return({"bottled-universe"=>{"name"=>{"__content__"=>"Wooz"}, "universe-id"=>{"type"=>"integer", "nil"=>"true"}, "id"=>{"__content__"=>"1", "type"=>"integer"}}})
171
-
172
- BottledUniverse.find(1).should == BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
173
- end
174
- end
175
- end
176
-
177
- describe 'subclasses of sublasses' do
178
- it "should configure its host without affecting its superclass" do
179
- Glassware.host.should == "http://localhost:3000"
180
- BottledUniverse.host.should == "http://localhost:3001"
181
- end
182
-
183
- it "should know its resource collection name when it is a subclass of a subclass" do
184
- BottledUniverse.resource_collection_name.should == 'bottled_universes'
185
- end
186
-
187
-
188
- it "should know how to create a new resource" do
189
- uri = mock(Uri)
190
- mock_http_response = mock(Net::HTTPResponse)
191
- mock_http_response.stub!(:code).and_return('201')
192
- mock_http_response.stub!(:content_type).and_return('application/xml')
193
- mock_http_response.stub!(:body).and_return("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bottled-universe>\n <name>Woot</name>\n <id>1</id>\n </bottled-universe>\n")
194
-
195
- Uri.should_receive(:new).with("http://localhost:3001/bottled_universes.xml", {}).and_return(uri)
196
- uri.should_receive(:post).with(
197
- "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bottled-universe>\n <name>Woot</name>\n</bottled-universe>\n",
198
- 'Content-Type' => 'application/xml'
199
- ).and_return(Wrest::Native::Response.new(mock_http_response))
200
- ware = BottledUniverse.create(:name => 'Woot')
201
- end
202
- end
203
-
204
- describe 'attribute interface' do
205
- it "should fail when getter methods for attributes that don't exist are invoked" do
206
- universe = Glassware.new(:owner => 'Kai Wren', :guardian => 'Lung Shan')
207
- lambda{ universe.ooga }.should raise_error(NoMethodError)
208
- end
209
-
210
- it "should provide getter methods for attributes" do
211
- universe = Glassware.new(:owner => 'Kai Wren', :guardian => 'Lung Shan')
212
- universe.owner.should == 'Kai Wren'
213
- universe.guardian.should == 'Lung Shan'
214
- end
215
-
216
- it "should respond to getter methods for attributes" do
217
- universe = Glassware.new(:owner => 'Kai Wren', :guardian => 'Lung Shan')
218
- universe.should respond_to(:owner)
219
- universe.should respond_to(:guardian)
220
- end
221
-
222
- it "should not respond to getter methods for attributes that don't exist" do
223
- universe = Glassware.new(:owner => 'Kai Wren', :guardian => 'Lung Shan')
224
- universe.should_not respond_to(:theronic)
225
- end
226
-
227
- it "should create a setter method when one is invoked for attributes that don't exist" do
228
- universe = Glassware.new(:owner => 'Kai Wren', :guardian => 'Lung Shan')
229
- universe.fu_dog = 'Shiriki'
230
- universe.attributes[:fu_dog].should == 'Shiriki'
231
- universe.fu_dog.should == 'Shiriki'
232
- end
233
-
234
- it "should provide setter methods for attributes" do
235
- universe = Glassware.new(:owner => 'Kai Wren', :guardian => 'Lung Shan')
236
- universe.guardian = 'Effervescent Tiger'
237
- universe.attributes[:guardian].should == 'Effervescent Tiger'
238
- end
239
-
240
- it "should respond to setter methods for attributes" do
241
- universe = Glassware.new(:owner => 'Kai Wren', :guardian => 'Lung Shan')
242
- universe.should respond_to(:owner=)
243
- universe.should respond_to(:guardian=)
244
- end
245
-
246
- it "should not respond to setter methods for attributes that don't exist" do
247
- universe = Glassware.new(:owner => 'Kai Wren', :guardian => 'Lung Shan')
248
- universe.should_not respond_to(:theronic=)
249
- end
250
-
251
- it "should return false when query methods for attributes that don't exist are invoked" do
252
- universe = Glassware.new(:owner => 'Kai Wren', :guardian => 'Lung Shan')
253
- universe.ooga?.should be_false
254
- end
255
-
256
- it "should provide query methods for attributes" do
257
- universe = Glassware.new(:owner => 'Kai Wren', :guardian => nil)
258
- universe.owner?.should be_true
259
- universe.guardian?.should be_false
260
- end
261
-
262
- it "should respond to query methods for attributes" do
263
- universe = Glassware.new(:owner => 'Kai Wren', :guardian => 'Lung Shan')
264
- universe.should respond_to(:owner?)
265
- universe.should respond_to(:guardian?)
266
- end
267
-
268
- it "should not respond to query methods for attributes that don't exist" do
269
- universe = Glassware.new(:owner => 'Kai Wren', :guardian => 'Lung Shan')
270
- universe.should_not respond_to(:theronic?)
271
- end
272
- end
273
- end
274
- end