GithubHelp home page GithubHelp logo

kjgarza / rack-parser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from achiurizo/rack-parser

0.0 2.0 0.0 51 KB

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

Home Page: twitter.com/arthur_chiu

License: MIT License

Ruby 100.00%

rack-parser's Introduction

Rack::Parser

Looking for a new maintainer!

Rack::Parser is a rack middleware that allows your application to do decode/parse incoming post data into param hashes for your applications to use. You can provide a custom Parser for things like JSON, XML, MSGPACK using your library of choice.

Installation

install it via rubygems:

gem install rack-parser

or put it in your Gemfile:

# Gemfile

gem 'rack-parser', :require => 'rack/parser'

Usage

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

# app.rb

use Rack::Parser, :parsers => { 'application/json' => proc { |data| JSON.parse data },
                               'application/xml'  => proc { |data| XML.parse data },
                               %r{msgpack}        => proc { |data| Msgpack.parse data }
                             }

Content Type Parsing

By default, Rack::Parser uses JSON decode/parse your JSON Data. This can be overwritten if you choose not to use them. You can do it like so:

use Rack::Parser, :parsers => {
  'application/json' => proc { |body| MyCustomJsonEngine.do_it body },
  'application/xml'  => proc { |body| MyCustomXmlEngine.decode body },
  'application/roll' => proc { |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:

use Rack::Parser, :handlers => {
  'application/json' => proc { |e, type| [400, { 'Content-Type' => 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 return 400

Do note, the error handler rescues exceptions that are descents of StandardError. See http://www.mikeperham.com/2012/03/03/the-perils-of-rescue-exception/

Regex Matching

With version 0.4.0, you can specify regex matches for the content types that you want the parsers and handlers to match.

NOTE: you need to explicitly pass a Regexp for it to regex match.

parser  = proc { |data| JSON.parse data }
handler = proc { |e, type| [400, {}, 'boop'] }
use Rack::Parser, :parsers  => { %r{json}   => parser },
                  :handlers => { %r{heyyyy} => handler }

Inspirations

This project came to being because of:

  • Niko Dittmann's rack-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.
  • Moonsik Kang - skip rack parser for content types that are not explicitly set.
  • Guillermo Iguaran - Updating multi_xml version dependency for XML/YAML exploit
  • Doug Orleans - Handle only post-body parsing errors and let upstream errors propogate downstream
  • Akshay Moghe - Make default error handler rack compliant by responding to #each and use StandardError

Copyright

Copyright © 2011,2012,2013,2014,2015,2016 Arthur Chiu. See MIT-LICENSE for details.

rack-parser's People

Contributors

achiurizo avatar achiu avatar amoghe avatar deepblue avatar dougo avatar guilleiguaran avatar lksv avatar havenwood avatar sbeckeriv avatar xxuejie avatar

Watchers

James Cloos avatar  avatar

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.