Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Index and GET work. TODO: query types.

  • Loading branch information...
commit 108c7114e643b02b656d9eb308bad86cb92f315e 1 parent f80ae5a
Yehuda Katz authored
View
16 merb-rest-server/app/controllers/rest.rb
@@ -1,14 +1,24 @@
class MerbRestServer::Rest < MerbRestServer::Application
provides :json
+
+ before do
+ @type = Object.full_const_get(params[:type].camel_case)
+ end
def index
- ""
+ types = @type.properties.map {|x| x.name}
+ hsh = {}
+ types.each do |type|
+ hsh[type] = params[type].split(",") if params[type]
+ end
+ display @type.all(hsh)
end
def get
- type = Object.full_const_get(params[:type].camel_case)
- @object = type.get!(params[:id])
+ @object = @type.get!(params[:id])
display @object
+ rescue DataMapper::ObjectNotFoundError
+ self.status = 404
end
def post
View
21 merb-rest-server/lib/merb_rest_server.rb
@@ -60,19 +60,14 @@ def self.setup_router(scope)
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,
- :type => nests.last.first,
- :nests => nests[0..-2],
- :id => nests.last.last,
- :format => format}
- else
- { :controller => "merb_rest_server/rest",
- :nests => nests[0..-2],
- :action => "index",
- :format => format}
- end
+ params = req.params.merge({ :controller => "merb_rest_server/rest",
+ :type => nests.last.first,
+ :nests => nests[0..-2],
+ :format => format})
+
+ params.merge!(:action => nests.last.last ? req.method.to_s : "index")
+ params.merge!(:id => nests.last.last) if nests.last.last
+ params
end
# scope.match('/index.:format').to(:controller => 'main', :action => 'index').name(:merb_rest_server_index)
end
View
58 merb-rest-server/spec/controllers/rest_spec.rb
@@ -15,28 +15,58 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe "GET index" do
- it "routes GET /rest/foo to Rest#index" do
- controller = get("/rest/foo")
- controller.action_name.should == "index"
+ describe "plain index" do
+ before do
+ @controller = get("/rest/foo", {}, :http_accept => "application/json")
+ end
+
+ it "routes GET /rest/foo to Rest#index" do
+ @controller.action_name.should == "index"
+ end
+
+ it "returns all of the resources" do
+ @controller.body.should == Foo.all.to_json
+ end
+ end
+
+ describe "index with query parameters" do
+ it "provides the objects that match ids" do
+ controller = get("/rest/foo", {:id => "1,2"}, :http_accept => "application/json")
+ controller.body.should == Foo.all(:id => [1,2]).to_json
+ end
+
+ it "provides the objects that match other params" do
+ controller = get("/rest/foo", {:name => "Mock1"}, :http_accept => "application/json")
+ controller.body.should == Foo.all(:name => "Mock1").to_json
+ end
end
end
describe "GET" do
- before do
- @controller = get("/rest/foo/1", {}, :http_accept => "application/json")
- end
+ describe "when the resource exists" 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.action_name.should == "get"
- @controller.params[:id].should == "1"
- end
+ it "routes GET /rest/foo/1 to Rest#show :id => 1" do
+ @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 "assigns the object as Foo.get!(1)" do
- @controller.assigns(:object).should == Foo.get!(1)
+ it "returns the object's attributes as JSON" do
+ @controller.body.should == Foo.get!(1).to_json
+ end
end
- it "returns the object's attributes as JSON" do
- @controller.body.should == Foo.get!(1).to_json
+ describe "when the resource doesn't exist" do
+ it "returns a 404" do
+ @controller = get("/rest/foo/10", {}, :http_accept => "application/json")
+ @controller.status.should == 404
+ end
end
end
View
4 merb-rest-server/spec/setup-database.rb
@@ -15,4 +15,6 @@ class Foo
Foo.auto_migrate!
-Foo.create(:name => "Hello")
+Foo.create(:name => "Mock1")
+Foo.create(:name => "Mock2")
+Foo.create(:name => "Mock3")
Please sign in to comment.
Something went wrong with that request. Please try again.