Sha256: aa16b9d65bd30b56d4c8eb94795150f7e021e77b196e358da6c8a9c3d5e74416

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

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
        not_acceptable_response(accept)
      end
    end

    private

    def not_acceptable_response(accept_header)
      status = 406
      body = "Not Acceptable - '#{accept_header}' didn't have the right mime type or version number. We only accept application/json with a version"
      header = { "WWW-Authenticate" => accept_header }
      Rack::Response.new(body, status, header).finish
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
stitches-4.0.0 lib/stitches/valid_mime_type.rb
stitches-4.0.0.RC1 lib/stitches/valid_mime_type.rb
stitches-3.8.3 lib/stitches/valid_mime_type.rb
stitches-3.8.2 lib/stitches/valid_mime_type.rb