Class: Drillbit::Middleware::ApiRequestValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/drillbit/middleware/api_request_validator.rb

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ApiRequestValidator

Returns a new instance of ApiRequestValidator.



15
16
17
# File 'lib/drillbit/middleware/api_request_validator.rb', line 15

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

rubocop:disable Metrics/LineLength



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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