Skip to content

Repository files navigation

Rack::Parser

Rack::Parser is a Rack Middleware for parsing post body data for JSON, XML, and custom content types using MultiJson, MultiXML, or any thing that you want to use.

What this allows your rack application to do is decode/parse incoming post data into param hashes for your applications to use.

Installation

install it via rubygems:

gem install rack-parser

or put it in your Gemfile:

# Gemfilegem'rack-parser',:require=>'rack/parser'

Usage

In a Sinatra or Padrino application, it would probably be something like:

# app.rbuseRack::Parser

Content Type Parsing

By default, Rack::Parser uses MultiJson and MultiXml to decode/parse your JSON/XML Data. these can be overwritten if you choose not to use them. However, through using them you can just as easily leverage the engine of your choice by setting the engine like so:

# app.rbMultiJson.engine=:yajl# Yajl-ruby for json decodingMultiXml.parser=:libxml# libxml for XML parsinguseRack::Parser

To set your own custom engine that perhaps neither MultiJson or MultiXml support, just make it a Proc:

useRack::Parser,:content_types=>{'application/json'=>Proc.new{ |body| MyCustomJsonEngine.do_itbody},'application/xml'=>Proc.new{ |body| MyCustomXmlEngine.decodebody},'application/roll'=>Proc.new{ |body| 'never gonna give you up'}}

Error Handling

Rack::Parser comes with a default error handling response that is sent if an error is to occur. If a logger is present, it will try to warn with the content type and error message.

You can additionally customize the error handling response as well to whatever it is you like:

useRack::Parser,:error_responses=>{'default'=>Proc.new{ |e,content_type| [500,{},["boo hoo"]]},'application/json'=>Proc.new{ |e,content_type| [400,{'Content-Type'=>content_type},["broke"]]}}

The error handler expects to pass both the error and content_type so that you can use them within your responses. In addition, you can override the default response as well.

If no content_type error handling response is present, it will use the default.

Inspirations

This project came to being because of:

  • Niko Dittmann'srack-post-body-to-params which some of its ideas are instilled in this middleware.
  • Rack::PostBodyContentTypeParser from rack-contrib which proved to be an inspiration for both libraries.

External Sources/Documentations

Contributors

  • Stephen Becker IV - For initial custom error response handling work.
  • Tom May - skip loading post body unless content type is set.

Copyright

Copyright © 2011 Arthur Chiu. See MIT-LICENSE for details.

About

Rack Middleware for parsing post body data for JSON, XML and various content types

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages