Home

Awesome

Nancy

Sinatra's little daughter

Frank and Nancy by classic film scans

Description

Minimal Ruby microframework for web development inspired in Sinatra and Cuba

Requires Ruby 2.7 or greater.

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"
require "nancy/render"

class Hello < Nancy::Base
  use Rack::Session::Cookie, secret: ENV['SECRET_TOKEN'] # for sessions
  include Nancy::Render

  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 "/files/:root/*path" do
    "From #{params['root']}, download #{params['path']}"
  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

  before("/protected") do
    halt 401, "unauthorized" unless session[:authenticated]
  end

  get "/protected" do
    "Protected area!!!"
  end

  after(".{+extension}") do |params|
    case params['extension']
    when 'json'
      response['Content-Type'] = 'application/json'
    else
      response['Content-Type'] = 'text/html'
    end
  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.6.0 (unreleased)

0.5.0 (unreleased)

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.