No commit activity in last 3 years
No release in over 3 years
A Rack middleware for automatically formatting response body
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme
Rack::FormatResponse
====================

  A Rack middleware for automatically formatting response body


Install
=======

  gem install rack_format_response


Usage
=====

    use Rack::FormatResponse

  When non-string value is given in response body,
  it will be passed to Rack::FormatResponse#format_<OBJECT_CLASS>.


Example
=======

  Let's consider about generating json response.

    get "/ping" do
      {"message"=>"pong"}
    end

  This code generates "messagepong" response due to response#to_s.
  So we must write like following with concern about Content-Type.

    get "/ping" do
      content_type :json
      hash = {"message"=>"pong"}
      JSON.dump(hash)
    end

  This is a pain and not fun!

  Well, with this module, we can enjoy rack with various objects.

    # use Rack::FormatResponse
    get "/ping" do
      {"message"=>"pong"}
    end
  ->
    Content-Type: application/json;charset=utf-8
    {"message":"pong"}


Customize
=========

  When you want automatic formatter from Hash to XML,
  It would be like this.

    require "rack/format_response"
    require "active_suport"

    class Rack::FormatResponse
      def format_hash(hash)
        hash.to_xml
      end
    end
    Rack::FormatResponse::MAPPINGS["Hash"] = "format_hash"

  or just write code in block.

    Rack::FormatResponse::MAPPINGS["Hash"] = lambda{|hash| hash.to_xml}


NOTE
====

  In default, this module converts hash to json with Yajl gem.
  Install it first for json use.

    gem install yajl-ruby

  # Yajl is a fastest json library where I know


Homepage
========

 http://github.com/maiha/rack_format_response


Author
======
Maiha <maiha@wota.jp>