Sha256: 4f42af489be1ecc75dd62ad2a568cfe895659f9a0fe5e2290ef13ed3172300fb

Contents?: true

Size: 1.26 KB

Versions: 4

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

require 'drillbit/configuration'
require 'drillbit/matchers/subdomain'
require 'drillbit/matchers/accept_header'
require 'drillbit/requests/base'
require 'drillbit/responses/invalid_api_request'
require 'drillbit/responses/invalid_subdomain'

module  Drillbit
module  Middleware
class   ApiRequestValidator
  JSON_API_MIME_TYPE_PATTERN = %r{application/vnd\.api\+json(?=\z|;)}

  def initialize(app)
    @app = app
  end

  # rubocop:disable Metrics/LineLength
  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
  # rubocop:enable Metrics/LineLength
end
end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
drillbit-3.0.3 lib/drillbit/middleware/api_request_validator.rb
drillbit-3.0.2 lib/drillbit/middleware/api_request_validator.rb
drillbit-3.0.1 lib/drillbit/middleware/api_request_validator.rb
drillbit-3.0.0 lib/drillbit/middleware/api_request_validator.rb