Permalink
Browse files

Make GET work more for real ;)

  • Loading branch information...
1 parent cadfab0 commit f80ae5af6dd854dc282546e6f5331050c0681e05 Yehuda Katz committed Jul 17, 2008
@@ -1,11 +1,14 @@
class MerbRestServer::Rest < MerbRestServer::Application
+ provides :json
def index
""
end
def get
- ""
+ type = Object.full_const_get(params[:type].camel_case)
+ @object = type.get!(params[:id])
+ display @object
end
def post
@@ -54,17 +54,24 @@ def self.deactivate
def self.setup_router(scope)
# example of a named route
scope.match(%r{(.*)}).defer_to do |req, params|
- parts = req.path.match(%r{^/rest/?(.*)/?$})[1].split("/")
+ m = req.path.match(%r{^/rest/?(.*?)(?:\.(.*))?$})
+ parts = m[1].split("/")
+ format = m[2]
nests = parts.map { [parts.shift, parts.shift] }
nests << [parts.shift] unless parts.empty?
if nests.last.last
{ :controller => "merb_rest_server/rest",
:action => req.method.to_s,
- :id => nests.last.last}
+ :type => nests.last.first,
+ :nests => nests[0..-2],
+ :id => nests.last.last,
+ :format => format}
else
{ :controller => "merb_rest_server/rest",
- :action => "index"}
+ :nests => nests[0..-2],
+ :action => "index",
+ :format => format}
end
end
# scope.match('/index.:format').to(:controller => 'main', :action => 'index').name(:merb_rest_server_index)
@@ -22,12 +22,22 @@
end
describe "GET" do
+ before do
+ @controller = get("/rest/foo/1", {}, :http_accept => "application/json")
+ end
+
it "routes GET /rest/foo/1 to Rest#show :id => 1" do
- controller = get("/rest/foo/1")
- controller.action_name.should == "get"
- controller.assigns(:object).should == Foo.get!(1)
- controller.params[:id].should == "1"
- end
+ @controller.action_name.should == "get"
+ @controller.params[:id].should == "1"
+ end
+
+ it "assigns the object as Foo.get!(1)" do
+ @controller.assigns(:object).should == Foo.get!(1)
+ end
+
+ it "returns the object's attributes as JSON" do
+ @controller.body.should == Foo.get!(1).to_json
+ end
end
describe "POST" do
@@ -1,6 +1,11 @@
require "dm-core"
+require "dm-serializer"
DataMapper.setup(:default, "sqlite3::memory:")
+module DataMapper::Resource
+ include DataMapper::Serialize
+end
+
class Foo
include DataMapper::Resource

0 comments on commit f80ae5a

Please sign in to comment.