Sha256: 2e02b267a85e8b0c0d95d2b68a7580929eef7ab544ada465274df8f7756212cd

Contents?: true

Size: 998 Bytes

Versions: 12

Compression:

Stored size: 998 Bytes

Contents

require_relative 'whitelisting_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::WhitelistingMiddleware

  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

12 entries across 12 versions & 1 rubygems

Version Path
stitches-3.6.1 lib/stitches/valid_mime_type.rb
stitches-3.6.0 lib/stitches/valid_mime_type.rb
stitches-3.6.0.RC1 lib/stitches/valid_mime_type.rb
stitches-3.5.0 lib/stitches/valid_mime_type.rb
stitches-3.4.0 lib/stitches/valid_mime_type.rb
stitches-3.3.0 lib/stitches/valid_mime_type.rb
stitches-3.2.3 lib/stitches/valid_mime_type.rb
stitches-3.2.2 lib/stitches/valid_mime_type.rb
stitches-3.2.1 lib/stitches/valid_mime_type.rb
stitches-3.2.0 lib/stitches/valid_mime_type.rb
stitches-3.1.0 lib/stitches/valid_mime_type.rb
stitches-3.0.0 lib/stitches/valid_mime_type.rb