Permalink
Browse files

Added support for simple create

  • Loading branch information...
kaiwren committed Jun 27, 2009
1 parent 249d34f commit 911e0e7600d3762d30648aa062a5a6434eff49f8
Showing with 83 additions and 39 deletions.
  1. +1 −0 .gitignore
  2. 0 init.rb
  3. +29 −17 lib/wrest/resource/base.rb
  4. +39 −11 spec/wrest/resource/base_spec.rb
  5. +14 −11 spec/wrest/uri_spec.rb
View
@@ -2,5 +2,6 @@ coverage
rdoc
doc
pkg
+tmp
spec/wrest/meh_spec.rb
.DS_Store
View
No changes.
View
@@ -17,17 +17,21 @@ class Base
always_has :id
typecast :id => as_integer
attr_reader :attributes
-
+
def ==(other)
return true if self.equal?(other)
return false unless other.class == self.class
return self.attributes == other.attributes
end
-
+
def hash
id.hash
end
-
+
+ def to_xml(options={})
+ attributes.to_xml({:root => self.class.resource_name.gsub('_', '-')}.merge(options))
+ end
+
class << self
def inherited(klass)
klass.set_resource_name klass.name
@@ -39,7 +43,7 @@ def inherited(klass)
# we often do while writing tests.
# By default, the resource name is set to the name of the class.
def set_resource_name(resource_name)
- self.class_eval "def self.resource_name; '#{resource_name}';end"
+ self.class_eval "def self.resource_name; '#{resource_name.underscore}';end"
end
# Allows the host url at which the resource is found to be configured
@@ -60,28 +64,36 @@ def set_default_format(format)
def set_redirect_handler(method_object)
end
- def resource_path
- @resource_path ||= "/#{resource_name.underscore.pluralize}"
- end
-
- def resource_collection_url
- "#{host}#{resource_path}"
- end
-
- def find_all
+ def resource_collection_name
+ @resource_collection_name ||= "#{resource_name.underscore.pluralize}"
end
- def find(resource_type = [:one, :collection, :singleton], from = "")
+ def find_one_uri_template
+ @find_one_template ||= Wrest::UriTemplate.new(':host/:resource_collection_name/:id.:format')
end
def find(id)
- reponse = "#{resource_collection_url}/#{id}.#{default_format}".to_uri.get
- self.new(reponse.deserialise.mutate_using(
+ response = find_one_uri_template.to_uri(
+ :host => host,
+ :resource_collection_name => resource_collection_name,
+ :id => id,
+ :format => default_format
+ ).get
+ self.new(response.deserialise.mutate_using(
Wrest::Components::Mutators.chain(:xml_mini_type_caster, :camel_to_snake_case)
).shift.last)
end
- def objectify(hash)
+ def create(attributes = {})
+ response = Wrest::UriTemplate.new(':host/:resource_collection_name.:format').to_uri(
+ :host => host,
+ :resource_collection_name => resource_collection_name,
+ :format => default_format
+ ).post(self.new(attributes).to_xml, 'Content-Type' => "application/#{default_format}")
+
+ self.new(response.deserialise.mutate_using(
+ Wrest::Components::Mutators.chain(:xml_mini_type_caster, :camel_to_snake_case)
+ ).shift.last)
end
end
end
@@ -36,7 +36,7 @@ module Wrest
it "should be equal if it has the same state" do
(
- @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1) == @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
+ @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1) == @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
).should be_true
end
@@ -46,13 +46,13 @@ module Wrest
it "should not be equal if it is not the same class" do
(
- @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1) == Glassware.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
+ @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1) == Glassware.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
).should be_false
end
it "should not be equal if it is has a different state" do
(
- @BottledUniverse.new(:universe_id=>3, :name=>"Wooz", :id=>1) == @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
+ @BottledUniverse.new(:universe_id=>3, :name=>"Wooz", :id=>1) == @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
).should be_false
end
@@ -71,11 +71,11 @@ module Wrest
(universe_two == universe_three).should be_true
(universe_one == universe_three).should be_true
end
-
+
it "should ensure that the hashcode is a fixnum" do
@BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1).hash.should be_kind_of(Fixnum)
end
-
+
it "should ensure that instances with the same ids have the same hashcode" do
universe_one = @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
universe_two = @BottledUniverse.new(:universe_id=>nil, :name=>"Wooz", :id=>1)
@@ -90,11 +90,11 @@ module Wrest
end
it "should know its name as a resource by default" do
- BottledUniverse.resource_name.should == 'BottledUniverse'
+ BottledUniverse.resource_name.should == 'bottled_universe'
end
it "should allow its name as a resource to be configured for anonymous classes" do
- @BottledUniverse.resource_name.should == 'BottledUniverse'
+ @BottledUniverse.resource_name.should == 'bottled_universe'
end
it "should know how to create an instance using deserialised attributes" do
@@ -129,10 +129,23 @@ module Wrest
@BottledUniverse.host.should == "http://localhost:3000"
end
- it "should know its resource path" do
- Glassware.resource_path.should == '/glasswares'
+ it "should know its resource collection name" do
+ Glassware.resource_collection_name.should == 'glasswares'
end
+ it "should know its uri template for find one" do
+ Glassware.find_one_uri_template.to_uri(
+ :host => 'http://localhost:3000',
+ :resource_collection_name => 'glasswares',
+ :id => 1,
+ :format => 'json'
+ ).should == 'http://localhost:3000/glasswares/1.json'.to_uri
+ end
+
+ it "should know how to serialise itself to xml" do
+ 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"
+ end
+
describe 'finders' do
# Json =>
# body => {"lead_bottle": {"name": "Wooz", "id": 1, "universe_id": null}}
@@ -167,8 +180,23 @@ module Wrest
BottledUniverse.host.should == "http://localhost:3001"
end
- it "should know its resource path when it is a subclass of a subclass" do
- BottledUniverse.resource_path.should == '/bottled_universes'
+ it "should know its resource collection name when it is a subclass of a subclass" do
+ BottledUniverse.resource_collection_name.should == 'bottled_universes'
+ end
+
+
+ it "should know how to create a new resource" do
+ uri = mock(Uri)
+ mock_http_response = mock(Net::HTTPResponse)
+ mock_http_response.stub!(:content_type).and_return('application/xml')
+ 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")
+
+ Uri.should_receive(:new).with("http://localhost:3001/bottled_universes.xml").and_return(uri)
+ uri.should_receive(:post).with(
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bottled-universe>\n <name>Woot</name>\n</bottled-universe>\n",
+ 'Content-Type' => 'application/xml'
+ ).and_return(Wrest::Response.new(mock_http_response))
+ ware = BottledUniverse.create(:name => 'Woot')
end
end
View
@@ -26,20 +26,23 @@ def build_ok_response(body = '')
Uri.new('http://localhost:3000').should_not be_https
end
- it "should understand equality" do
- Uri.new('https://localhost:3000/ooga').should_not == 'https://localhost:3000/ooga'
- Uri.new('https://localhost:3000/ooga').should_not == Uri.new('https://localhost:3000/booga')
- Uri.new('https://localhost:3000').should_not == Uri.new('https://localhost:3500')
- Uri.new('https://localhost:3000').should_not == Uri.new('http://localhost:3000')
- Uri.new('http://localhost:3000').should == Uri.new('http://localhost:3000')
- end
+ describe 'Equals' do
+ it "should understand equality" do
+ Uri.new('https://localhost:3000/ooga').should_not == nil
+ Uri.new('https://localhost:3000/ooga').should_not == 'https://localhost:3000/ooga'
+ Uri.new('https://localhost:3000/ooga').should_not == Uri.new('https://localhost:3000/booga')
+ Uri.new('https://localhost:3000').should_not == Uri.new('https://localhost:3500')
+ Uri.new('https://localhost:3000').should_not == Uri.new('http://localhost:3000')
+ Uri.new('http://localhost:3000').should == Uri.new('http://localhost:3000')
+ end
- it "should have the same hash code if it is the same uri" do
- Uri.new('https://localhost:3000').hash.should == Uri.new('https://localhost:3000').hash
- Uri.new('https://localhost:3001').hash.should_not == Uri.new('https://localhost:3000').hash
+ it "should have the same hash code if it is the same uri" do
+ Uri.new('https://localhost:3000').hash.should == Uri.new('https://localhost:3000').hash
+ Uri.new('https://localhost:3001').hash.should_not == Uri.new('https://localhost:3000').hash
+ end
end
-
+
describe 'Get' do
it "should know how to get" do
uri = "http://localhost:3000/glassware".to_uri

0 comments on commit 911e0e7

Please sign in to comment.