Home

Awesome

Hobbit::Contrib Build Status Code Climate Code Climate Coverage Dependency Status Gem Version

Contributed Hobbit extensions.

Installation

Add this line to your application's Gemfile:

gem 'hobbit-contrib', require: 'hobbit/contrib'
# or this if you want to use master
# gem 'hobbit-contrib', github: 'patriciomacadden/hobbit-contrib', require: 'hobbit/contrib'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hobbit-contrib

Optional dependencies

Usage

Each extension may have its own usage. In general, including the module will be enough.

require 'hobbit'
require 'hobbit/contrib'

class App < Hobbit::Base
  # include hobbit session extension
  include Hobbit::Session

  # define your application
end

Available extensions

Hobbit::Environment

This extension allows you to control the application environment by using the provided methods. To use this extension just include the module:

require 'hobbit'
require 'hobbit/contrib'

class App < Hobbit::Base
  include Hobbit::Environment

  get '/' do
    "currently in #{environment}"
  end
end

run App.new

Available methods

Note: All methods are available at class and instance context.

Hobbit::ErrorHandling

This extension provides a way of handling errors raised by your application. To use this extension just include the module:

require 'hobbit'
require 'hobbit/contrib'

class App < Hobbit::Base
  include Hobbit::ErrorHandling

  error Exception
    exception = env['hobbit.error']
    exception.message
  end

  get '/' do
    raise Exception, 'Oops'
  end
end

run App.new

Available methods

Note: If you define more than one handler per exception the last one defined will have precedence over the others.

Hobbit::Filter

This extension provides a way of calling blocks before and after the evaluation of a route (just like sinatra's filters). To use this extension just include the module:

require 'hobbit'
require 'hobbit/contrib'

class App < Hobbit::Base
  include Hobbit::Filter

  def authenticate_user!
    # ...
  end

  before do
    authenticate_user!
  end

  get '/' do
    # ...
  end
end

run App.new

Available methods

Note: It is recommended to include Hobbit::Filter before Hobbit::ErrorHandling if you want to use both extensions.

Hobbit::Mote

This module provides rendering to your hobbit application using mote. To use this extension just include the module:

require 'mote'
require 'hobbit'
require 'hobbit/contrib'

class App < Hobbit::Base
  include Hobbit::Mote

  get '/' do
    # will render views/index.mote using views/layouts/application.mote as layout
    render 'index'
  end
end

Available methods

NOTE: Use {{content}} within your layout to yield the rendered page.

Hobbit::Render

This module provides rendering to your hobbit application using tilt. To use this extension just include the module:

require 'tilt'
require 'hobbit'
require 'hobbit/contrib'

class App < Hobbit::Base
  include Hobbit::Render

  get '/' do
    # will render views/index.erb using views/layouts/application.erb as layout
    render 'index'
  end
end

Available methods

Hobbit::Session

This module provides helper methods for handling user sessions. To use this extension just include the module:

require 'hobbit'
require 'hobbit/contrib'

class App < Hobbit::Base
  include Hobbit::Session
  use Rack::Session::Cookie, secret: SecureRandom.hex(64)

  post '/' do
    session[:name] = 'hobbit'
  end

  get '/' do
    "Hello #{session[:name]}!"
  end
end

Available methods

Contributing

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

License

See the LICENSE.