/raccept

Rack middleware that converts Ruby objects into xml or json based on Accepts header

Primary LanguageRubyMIT LicenseMIT

Raccept (Rack Accepts Helper)

Converts Ruby objects to JSON or XML based on accepts headers.

Ignores legal Rack responses like “string” and [“string”,“string”]

Install

gem install raccept

Use

# in config.ru
use Raccept
run lambda {|env| [200,{"Content-Type" => "text/plain"}, {:this => 'is a Hash'} ] }

# Will respond with ...
# Accepts application/xml or text/xml
<?xml version="1.0" encoding="UTF-8"?>
<hash>
  <this>is a Hash</this>
</hash>

# Accepts application/json or text/json
{"this":"is a Hash"}

Todo, maybe

  • Get something leaner than ActiveSupport for to_xml?

  • Support custom objects somehow?

  • Add tests for it.

Copyright © 2009 Sam Schenkman-Moore. See LICENSE for details.