class Drillbit::Middleware::ApiRequestValidator

Constants

JSON_API_MIME_TYPE_PATTERN

Public Class Methods

new(app) click to toggle source
# File lib/drillbit/middleware/api_request_validator.rb, line 15
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source

rubocop:disable Metrics/LineLength

# File lib/drillbit/middleware/api_request_validator.rb, line 20
def call(env)
  env['HTTP_X_APPLICATION_NAME'] = Drillbit.configuration.application_name

  request               = Requests::Base.resolve(env)
  subdomain_matcher     = Matchers::Subdomain.new
  accept_header_matcher = Matchers::AcceptHeader.new

  return Responses::InvalidSubdomain.call(env)  unless subdomain_matcher.matches?(request)
  return Responses::InvalidApiRequest.call(env) if     subdomain_matcher.matches_api_subdomain?(request) &&
                                                       !accept_header_matcher.matches?(request)

  env['CONTENT_TYPE'] = env['CONTENT_TYPE']
                          .to_s
                          .gsub(JSON_API_MIME_TYPE_PATTERN, 'application/json')

  @app.call(env)
end