Nancy

Ruby microframework for web development.

View the Project on GitHub guilleiguaran/nancy

Nancy

Sinatra's little daughter

Frank and Nancy by classic film scans

Description

Minimal Ruby microframework for web development inspired in Sinatra and Cuba

Installation

Install the gem:

$ gem install nancy

or add it to your Gemfile:

gem "nancy"

Usage

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.

Features

Version history

0.4.0 (unreleased)

0.3.0 (Octuber 3, 2012)

0.2.0 (April 12, 2012)

0.1.0 (April 4, 2012)

0.0.1 (March 20, 2012)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Copyright

Copyright (c) 2012-2014 Guillermo Iguaran. See LICENSE for further details.