Skip to content

Instantly share code, notes, and snippets.

@esebastian
Created February 18, 2014 11:11
Show Gist options
  • Save esebastian/9068949 to your computer and use it in GitHub Desktop.
Save esebastian/9068949 to your computer and use it in GitHub Desktop.
Mount routes from extension in Sinatra
# add requires as needed...
# main app
class App < Sinatra::Base
register Sinatra::Namespace
# mount routes from extension
register Sinatra::Guides
mount_guides
# mount routes from extension inside a namespace
namespace '/tours' do
register Sinatra::Tours
mount_tours
end
get '/' do
{ say: "hello API" }.to_json
end
end
# modules
module Sinatra
module Tours
def mount_tours
get '/' do
{ say: "tours API" }.to_json
end
get '/add' do
{ add: "yeah" }.to_json
end
end
end
register Tours
module Guides
def mount_guides
get '/' do
{ say: "guides API" }.to_json
end
get '/add' do
{ add: "nope" }.to_json
end
end
end
register Guides
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment