Sha256: b2f8aabb279220c0607a6b63577d3bc7cfe4b5ac10c95d2237fb0eb5e99b7184

Contents?: true

Size: 811 Bytes

Versions: 1

Compression:

Stored size: 811 Bytes

Contents

module Raisin
  #
  # Middleware responsable to filter HTTP Accept header
  # It stores version and format accepted by the client in the env.
  #
  class Middleware
    ACCEPT_REGEXP = /\Aapplication\/vnd\.(?<vendor>[a-z]+).(?<version>v[0-9]+)\+(?<format>[a-z]+)\Z/

    def initialize(app)
      @app    = app
      @vendor = Raisin.vendor
    end

    def call(env)
      extract_version_from_accept_header(ActionDispatch::Request.new(env))
      @app.call(env)
    end

    private

    def extract_version_from_accept_header(req)
      header = req.get_header('HTTP_ACCEPT'.freeze).to_s.strip

      if (matches = ACCEPT_REGEXP.match(header)) && @vendor == matches[:vendor]
        req.set_header('raisin.version'.freeze, matches[:version])
        req.format = matches[:format]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
raisin-0.2.0 lib/raisin/middleware.rb