module Roda::RodaPlugins::JsonParser

  1. lib/roda/plugins/json_parser.rb

The json_parser plugin parses request bodies in json format if the request's content type specifies json. This is mostly designed for use with JSON API sites.

This only parses the request body as JSON if the Content-Type header for the request includes “json”.


Public Class

  1. configure


DEFAULT_ERROR_HANDLER = proc{|r| r.halt [400, {}, []]}  
DEFAULT_PARSER = JSON.method(:parse)  

Public Class methods

configure (app, opts=OPTS)

Handle options for the json_parser plugin:


A proc to call if an exception is raised when parsing a JSON request body. The proc is called with the request object, and should probably call halt on the request or raise an exception.


The parser to use for parsing incoming json. Should be an object that responds to +call(str)+ and returns the parsed data. The default is to call JSON.parse.


If true, the parser will be called with the request object as the second argument, so the parser needs to respond to +call(str, request)+.

[show source]
# File lib/roda/plugins/json_parser.rb, line 27
def self.configure(app, opts=OPTS)
  app.opts[:json_parser_error_handler] = opts[:error_handler] || app.opts[:json_parser_error_handler] || DEFAULT_ERROR_HANDLER
  app.opts[:json_parser_parser] = opts[:parser] || app.opts[:json_parser_parser] || DEFAULT_PARSER
  app.opts[:json_parser_include_request] = opts[:include_request] if opts.has_key?(:include_request)