Ruby microframework for web development.
Sinatra's little daughter
Minimal Ruby microframework for web development inspired in Sinatra and Cuba
Install the gem:
$ gem install nancy
or add it to your Gemfile:
gem "nancy"
Here's a simple application:
# hello.rb
require "nancy"
class Hello < Nancy::Base
use Rack::Session::Cookie, secret: ENV['SECRET_TOKEN'] # for sessions
include Nancy::Render # for templates
before do
if request.path_info == "/protected" && !session[:authenticated]
halt 401, "unauthorized"
end
end
after do
if request.path_info ~= /\.json$/
response['Content-Type'] = 'application/json'
else
response['Content-Type'] = 'text/html'
end
end
get "/" do
"Hello World"
end
get "/hello" do
response.redirect "/"
end
get "/hello/:name" do
"Hello #{params['name']}"
end
post "/hello" do
"Hello #{params['name']}"
end
get "/template" do
@message = "Hello world"
render("views/hello.erb")
end
post "/login" do
@user = User.find(params['username'])
halt 401, "unauthorized" unless @user.authenticate(params['password'])
session[:authenticated] = true
render("views/layout.erb") { render("views/welcome.erb") }
end
get "/protected" do
"Protected area!!!"
end
get "/users/:id.json" do
@user = User.find(params['id'])
halt 404 unless @user
UserSerializer.new(@user).to_json
end
map "/resque" do
run Resque::Server
end
map "/nancy" do
run AnotherNancyApp.new
end
end
To run it, you can create a config.ru
file:
# config.ru
require "./hello"
run Hello.new
You can now run rackup
and enjoy what you have just created.
Check examples folder for a detailed example.
ERB
from stdlibresponse.redirect '/uri'
tilt
dependency, to use Nancy::Render add it manually to appRack::Response
object anymoregit checkout -b my-new-feature
)git commit -am 'Added some feature'
)git push origin my-new-feature
)Copyright (c) 2012-2014 Guillermo Iguaran. See LICENSE for further details.