class Superintendent::Request::Bouncer

Constants

DEFAULT_OPTS

Public Class Methods

new(app, opts={}) click to toggle source
# File lib/superintendent/request/bouncer.rb, line 14
def initialize(app, opts={})
  @app, @options = app, DEFAULT_OPTS.merge(opts)
end

Public Instance Methods

call(env) click to toggle source
# File lib/superintendent/request/bouncer.rb, line 18
def call(env)
  @request = ActionDispatch::Request.new(env)

  if required_keys_missing?
    return respond_400(
      {
        code: 'headers-missing',
        title: 'Headers missing',
        detail: 'Required headers were not present in the request'
      }
    )
  end

  if %w[POST PUT PATCH].include? @request.request_method
    if unsupported_content_type?
      return respond_400(
        {
          code: 'content-type-unsupported',
          title: 'Request content-type is unsupported',
          detail: "#{@request.content_type} is not a supported content-type"
        }
      )
    end
  end

  @app.call(env)
end

Private Instance Methods

required_keys_missing?() click to toggle source
# File lib/superintendent/request/bouncer.rb, line 55
def required_keys_missing?
  @options[:required_headers].any? { |key| !@request.headers.include?(key) }
end
respond_400(attributes) click to toggle source
# File lib/superintendent/request/bouncer.rb, line 59
def respond_400(attributes)
  [400,
   {'Content-Type' => 'application/vnd.api+json'},
   [
     {
       errors: [
         {
           attributes: {
             id: @request.headers[Id::X_REQUEST_ID],
             status: 400
           }.merge(attributes),
           type: 'errors'
         }
       ]
     }.to_json
   ]
  ]
end
unsupported_content_type?() click to toggle source
# File lib/superintendent/request/bouncer.rb, line 48
def unsupported_content_type?
  content_type = @request.content_type
  return false if content_type.nil? || content_type.empty?

  !@options[:supported_content_types].include? content_type
end