Coder Social home page Coder Social logo

mike-park / sparrow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rohde-schwarz/sparrow

1.0 1.0 0.0 1.54 MB

Define your request and response JSON format with a Rack middleware!

License: MIT License

Ruby 94.36% JavaScript 1.03% CSS 0.88% HTML 3.73%

sparrow's Introduction

Sparrow

A Rack middleware for converting the params keys and JSON response keys of a Rack application.

Build Status Gem Version

Installation

Add this line to your application's Gemfile:

gem 'cp-sparrow', require: 'sparrow'

And then execute:

$ bundle

Or install it yourself as:

$ gem install cp-sparrow

Usage

If you're using Rails, that's it. You haven't to do anything more. If you're not using Rails, you will have to add to your config.ru:

require 'sparrow'

use Sparrow::Middleware

Configuration

There are various configuration options as well as HTTP headers available to make the middleware act as you want it to.

in your configuration file (such as application.rb if you are using Rails)

Sparrow.configure do |config|
  config.excluded_routes = [
    Regexp.new('api/model/certificates')
  ]
end

Options may be:

excluded_routes

Array(String|Regexp). Default: []

An Array of Strings and/or Regexps defining which paths should not be touched by the middleware. The entries should matchs paths for your application. They should not start with a leading slash.

Example: config.excluded_routes = ['api/model/certificates']

default_json_request_key_transformation_strategy

String. Default: camelize

Defines how the middleware should treat incoming parameters via Request. Which means how they get tranformed, i.e. defining underscore here means that incoming parameters get underscore. Possible values are underscore and camelize

Example: config.default_json_request_key_transformation_strategy = "underscore"

default_json_response_key_transformation_strategy

String. Default: camelize

Same as default_json_request_key_transformation_strategy, but for responses. I.e. this defines to which format the keys get transformed when the response gets sent.

Example: config.default_json_response_key_transformation_strategy = "camelize"

json_request_format_header

String. Default: request-json-format

Defines the HTTP Header key which sets the request transformation strategy as in default_json_request_key_transformation_strategy*. This definition has higher priority than the default definition. Any valid HTTP Header String value is possible. Defaults to request-json-format.

Example:

config.json_request_format_header = "request-json-format"

json_response_format_header

String. Default: response-json-format

Same as json_request_format_header, but for the response handling. Defaults to response-json-format

Example: config.json_response_format_header = "response-json-format"

camelize_ignore_uppercase_keys

Boolean. Default: true

A boolean that indicates to not camelize keys that are all Uppercase, like CountryCodes "EN", etc.

Example: config.camelize_ignore_uppercase_keys = true

allowed_content_types

Array(String). Default: %w[application/json application/x-www-form-urlencoded text/x-json]

A list of HTTP content types upon which the middleware shall trigger and possibly start conversion. Defaults to ['application/json', 'application/x-www-form-urlencoded', 'text/x-json']. If nil is present in the list, requests/responses with no Content-Type header will be processed as well. Possible values can also be the start of the content-type-header like application/ which matches everything that starts with application/ like application/json

Example: config.allowed_content_types = ['application/json', 'text/x-json']

allowed_accepts

Array(String). Default: ['application/json', 'application/x-www-form-urlencoded', 'text/x-json', nil]

Same as [allowed_content_types], but reacts to the HTTP Accept Header. Applies to the same possible options, behavior. Defaults to the same set of MIME types, but also includes nil by default, which ignores checking the Accept header in default configuration

Example: config.allowed_accepts = ['application/json', 'text/x-json']

enable_logging

Boolean. Default: false

Determines logging while the middleware is active. Primary output is which conversion strategy gets chosen upon which request/response.

Example: config.enable_logging = true

Tests

Just run rake and you are off to go. This runs the whole suite including specs for unit- & integrationtests for Rails and Rack including different versions of Rails.

If you want to test a specific version of Rails:

export RAILS_VERSION=3.2.17; bundle update; bundle exec rspec

sparrow's People

Contributors

anmuel avatar kampfschlaefer avatar dsci avatar enricogenauck avatar mike-park avatar

Stargazers

Dr. Lawrence Gerschel    avatar

Watchers

James Cloos 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.