Giter Club home page Giter Club logo

rack-action's Introduction

Rack::Action

Rack::Action is a small, simple framework for generating Rack responses.

Installation

Add this line to your application's Gemfile:

gem 'rack-action'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-action

Usage

Rack::Action provides functionality to generate a Rack response. To use Rack::Action, you should subclass Rack::Action and provide your own implementation of respond. The simplest Rack action is one that just returns a string from respond:

require 'rack/action'

class MyAction < Rack::Action
  def respond
    "Hello, World!"
  end
end

run MyAction

The class itself is a rack app, so the previous code example is a valid rackup file. Rack::Action is meant to be used with one action per page/endpoint in your application, so it is typically used in conjuction with something like Rack::Router, which would look something like this:

require 'rack/action'
require 'rack/router'

class FooAction < Rack::Action
  def respond
    "foo"
  end
end

class BarAction < Rack::Action
  def respond
    "bar"
  end
end

router = Rack::Router.new do
  get "/foo" => FooAction
  get "/bar" => BarAction
end

run router

Rack::Action makes an instance of Rack::Request and Rack::Response available which can be used to set headers, cookies, etc.

class ArticleAction < Rack::Action
  def respond
    article = Article.find(params["id"])
    response['Content-Type'] = "text/xml"
    article.to_xml
  end
end

You can use before filters to do things before respond is called:

class AccountAction < Rack::Action
  before_filter :load_current_user

  def load_current_user
    @current_user = User.find(params["id"])
  end

  def respond
    "Welcome Back, #{@current_user.name}"
  end
end

and you can of course share functionality across actions with inheritance:

class ApplicationAction < Rack::Action
  before_filter :login_required

  def login_required
    redirect_to "/login" unless logged_in?
  end
end

class PublicAction < ApplicationAction
  skip_before_filter :login_required

  def respond
    "Hello"
  end
end

class PrivateAction < ApplicationAction
  def respond
    "It's A Secret To Everybody."
  end
end

Before filters will execute in the order they are defined. If a before filter writes to the response, subsequent filters will not be executed and the respond method will not be executed. As long as no before filters write to the response, subsequent filters and the respond method will be called.

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.