class Stitches::ValidMimeType
A middleware that requires all API calls to be for versioned JSON or Protobuf.
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.
Or in the case of a protobuf encoded payload the header should be like so:
application/protobuf
There isn't an accepted standard for protobuf encoded payloads but this form is common.
Protected Instance Methods
do_call(env)
click to toggle source
# File lib/stitches/valid_mime_type.rb, line 21 def do_call(env) accept = String(env["HTTP_ACCEPT"]) if (%r{application/json}.match?(accept) && %r{version=\d+}.match?(accept)) || %r{application/protobuf}.match?(accept) @app.call(env) else not_acceptable_response(accept) end end
Private Instance Methods
not_acceptable_response(accept_header)
click to toggle source
# File lib/stitches/valid_mime_type.rb, line 32 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 or application/protobuf" header = { "WWW-Authenticate" => accept_header } Rack::Response.new(body, status, header).finish end