Class: Apill::Middleware::ApiRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/apill/middleware/api_request.rb

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ApiRequest

Returns a new instance of ApiRequest.



15
16
17
# File 'lib/apill/middleware/api_request.rb', line 15

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

rubocop:disable Metrics/LineLength, Metrics/AbcSize



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/apill/middleware/api_request.rb', line 20

def call(env)
  env['HTTP_X_APPLICATION_NAME'] = Apill.configuration.application_name

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

  return Responses::InvalidSubdomain.call(env)      unless subdomain_matcher.matches?(request)
  return Responses::InvalidApiRequest.call(env)     unless !subdomain_matcher.matches_api_subdomain?(request) ||
                                                           accept_header_matcher.matches?(request)
  return Responses::InvalidToken.call(env,
                                      application_name: request.application_name) \
         unless token.valid?

  env['X_DECRYPTED_JSON_WEB_TOKEN'] = token.to_h
  env['QUERY_STRING']               = Parameters.process(env['QUERY_STRING'])
  env['CONTENT_TYPE']               = env['CONTENT_TYPE'].
                                      to_s.
                                      gsub! JSON_API_MIME_TYPE_PATTERN,
                                            'application/json'

  @app.call(env)
end