Skip to content

3scale/roda-parse-request

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

roda-parse-request

roda plugin which automatically parses JSON and url-encoded requests

Usage

In your Gemfile:

gem 'roda-parse-request'

In your roda app:

require 'roda'
require 'roda-parse-request'

class App < Roda
  plugin :parse_request

  route do |r|

    r.post do
      # You can use request.parsed_body here
      # By default, it will contain response.body parsed as a json or url-encoded
      # request (if the request was sent with the appropiate content-type headers)
    end
  end
end

Customization

It is possible to add your own parsers to the plugin, like so:

require 'roda'
require 'roda-parse-request'

class App < Roda
  plugin :parse_request, parsers: {
    'upcase'  => ->(data) { data.upcase },
  }

  route do |r|

    r.post do
      # You can still use json and url-encoded parsed requests in request.parsed_body
      # But now, if you get a request with Content-Type = 'upcase', the body
      # will be automatically set to all caps.
    end
  end
end

See the specs for more examples

Running specs

Clone the repo, run bundle and then rspec.

License

MIT License (see LICENSE)

About

roda plugin which automatically parses JSON and url-encoded requests

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages