Sha256: 48e0473031941656021a6d04396790de15ce3ca793347ca751d285df076b578b

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

module Raisin
  module Middleware
    class Header < Base

      def self.merge(base, other)
        base_options, other_options = base.pop, other.pop
        [base.concat(other), base_options.merge!(other_options)]
      end

      attr_reader :options, :versions

      def initialize(app, versions, options = {})
        super
        @options = options
        @versions = Array(versions)
      end

      def call(env)
        @env = env
        return [406, {}, ["You shall not pass!"]] unless verify_http_accept_header
        super
      end

      private

      def verify_http_accept_header
        header = @env['HTTP_ACCEPT']
        if (matches = %r{application/vnd\.(?<vendor>[a-z]+)-(?<version>v[0-9]+)\+(?<format>[a-z]+)?}.match(header)) &&
           (versions.include?(matches[:version]) || versions.include?(Raisin::Router::Version::ALL)) &&
           (!options.key?(:vendor) || options[:vendor] == matches[:vendor])

          @env['raisin.request.formats'] = [Mime::Type.lookup("application/#{matches[:format]}")]
          return true
        end
        false
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
raisin-0.0.2 lib/raisin/middleware/header.rb
raisin-0.0.1 lib/raisin/middleware/header.rb