Sha256: 97f18214b103aa2d736db3f42c1ce6088b0fef4cbec26f9aeddf9bc907818241

Contents?: true

Size: 993 Bytes

Versions: 5

Compression:

Stored size: 993 Bytes

Contents

require_relative 'allowlist_middleware'
module Stitches
  # A middleware that requires all API calls to be for versioned JSON.  This means that the Accept
  # header (available to Rack apps as HTTP_ACCEPT) should be like so:
  #
  #     application/json; version=1
  #
  # This just checks that you've specified some numeric version.  ApiVersionConstraint should be used
  # to "lock down" the versions you accept.
  class ValidMimeType < Stitches::AllowlistMiddleware

  protected

    def do_call(env)
      accept = String(env["HTTP_ACCEPT"])
      if accept =~ %r{application/json} && accept =~ %r{version=\d+}
        @app.call(env)
      else
        NotAcceptableResponse.new(accept)
      end
    end

    private

    class NotAcceptableResponse < Rack::Response
      def initialize(accept_header)
        super("Not Acceptable - '#{accept_header}' didn't have the right mime type or version number. We only accept application/json with a version", 406)
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
stitches-3.8.1 lib/stitches/valid_mime_type.rb
stitches-3.8.0 lib/stitches/valid_mime_type.rb
stitches-3.7.3 lib/stitches/valid_mime_type.rb
stitches-3.7.2 lib/stitches/valid_mime_type.rb
stitches-3.7.0 lib/stitches/valid_mime_type.rb