Skip to content
This repository has been archived by the owner on Nov 17, 2017. It is now read-only.

Commit

Permalink
Added support for Rails like routing, e.g `get "/", :to => "site#inde…
Browse files Browse the repository at this point in the history
…x"`.
  • Loading branch information
fnando committed Aug 5, 2011
1 parent 0e44a0b commit 0b6a08c
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
pkg
tmp
docs
*.rdb
22 changes: 22 additions & 0 deletions examples/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
$:.push(File.dirname(__FILE__) + "/../lib")

# Just run `ruby examples/controller.rb` and then use something like
# `curl http://localhost:2345/api/v1/`.

require "rack/api"

class Hello < Rack::API::Controller
def index
{:message => "Hello, awesome API!"}
end
end

Rack::API.app do
prefix "api"

version :v1 do
get "/", :to => "hello#index"
end
end

Rack::Handler::Thin.run Rack::API, :Port => 2345
14 changes: 14 additions & 0 deletions examples/without_version_and_prefix.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
$:.push(File.dirname(__FILE__) + "/../lib")

# Just run `ruby examples/without_version_and_prefix.rb` and then use something like
# `curl http://localhost:2345/`.

require "rack/api"

Rack::API.app do
get "/" do
{:message => "Hello, awesome API!"}
end
end

Rack::Handler::Thin.run Rack::API, :Port => 2345
41 changes: 37 additions & 4 deletions lib/rack/api/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,42 @@ def route_set # :nodoc:
# end
# end
#
# You don't have to use +version+ or +prefix+.
#
# class MyAPI < Rack::API
# get "users(.:format)" do
# # do something
# end
# end
#
# Alternatively, you can define your routes pretty much like Rails.
#
# class MyAPI < Rack::API
# get "users(.:format)", :to => "users#index"
# end
#
# The route above will require a class +Users+ with an instance method +index+.
#
# class Users < Rack::API::Controller
# def index
# # do something
# end
# end
#
# Note that your controller <b>must</b> inherit from Rack::API::Controller. Otherwise,
# your world will explode.
#
def route(method, path, requirements = {}, &block)
path = Rack::Mount::Strexp.compile mount_path(path), requirements, %w[ / . ? ]
route_set.add_route(build_app(block), :path_info => path, :request_method => method)
controller_class = Controller

if requirements[:to]
controller_name, action_name = requirements.delete(:to).split("#")
controller_class = controller_name.camelize.constantize
block = proc { __send__(action_name) }
end

route_set.add_route(build_app(controller_class, block), :path_info => path, :request_method => method)
end

HTTP_METHODS.each do |method|
Expand Down Expand Up @@ -258,8 +291,8 @@ def default_format # :nodoc:
(option(:formats).first || "json").to_s
end

def build_app(handler) # :nodoc:
app = Controller.new({
def build_app(controller, handler) # :nodoc:
app = controller.new({
:handler => handler,
:default_format => default_format,
:version => option(:version),
Expand All @@ -277,7 +310,7 @@ def build_app(handler) # :nodoc:
# Add middleware for format validation.
builder.use Rack::API::Middleware::Format, default_format, option(:formats)

# Add middlewares to executation stack.
# Add middlewares to execution stack.
option(:middlewares, :merge).each {|middleware| builder.use(*middleware)}

# Apply helpers to app.
Expand Down
14 changes: 14 additions & 0 deletions spec/rack-api/controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "spec_helper"

describe Rack::API do
before do
Rack::API.app do
get("/", :to => "my_controller#index")
end
end

it "renders action from MyApp" do
get "/", :name => "John"
last_response.body.should == {"name" => "John"}.to_json
end
end
5 changes: 5 additions & 0 deletions spec/support/mycontroller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MyController < Rack::API::Controller
def index
{:name => params[:name]}
end
end

0 comments on commit 0b6a08c

Please sign in to comment.