Sha256: 4cbca7a2da07a1b2a71feabee2e97aed1a8e4f2c42838ec91dd9d8eb4f746e21

Contents?: true

Size: 772 Bytes

Versions: 3

Compression:

Stored size: 772 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 = /application\/vnd\.(?<vendor>[a-z]+)-(?<version>v[0-9]+)\+(?<format>[a-z]+)?/

    def initialize(app)
      @app = app
      @vendor = Configuration.version.vendor
    end

    def call(env)
      @env = env
      verify_accept_header
      @app.call(@env)
    end

    private

    def verify_accept_header
      if (matches = ACCEPT_REGEXP.match(@env['HTTP_ACCEPT'])) && @vendor == matches[:vendor]
        @env['raisin.version']  = matches[:version]
        @env['raisin.format']   = "application/#{matches[:format]}"
        true
      else
        false
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
raisin-0.1.2 lib/raisin/middleware.rb
raisin-0.1.1 lib/raisin/middleware.rb
raisin-0.1.0 lib/raisin/middleware.rb