Class: JSONAPI::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/easy/jsonapi/middleware.rb

Overview

The middleware of the gem and also the contact point between the the gem and the rack application using it

Instance Method Summary collapse

Constructor Details

#initialize(app, &block) ⇒ Middleware

Returns a new instance of Middleware.

Parameters:

  • app

    The Rack Application



13
14
15
16
17
18
19
# File 'lib/easy/jsonapi/middleware.rb', line 13

def initialize(app, &block)
  @app = app
  return unless block_given?

  @config_manager = JSONAPI::ConfigManager.new
  block.call(@config_manager)
end

Instance Method Details

#call(env) ⇒ Object

If not in maintenance_mode and the request is intended to be JSONAPI,

it checks headers, params, and body it for compliance and raises
and error if any section is found to be non-compliant.

Parameters:

  • env

    The rack envirornment hash



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/easy/jsonapi/middleware.rb', line 25

def call(env)
  if in_maintenance_mode?
    return maintenance_response
  end

  if jsonapi_request?(env)
    error_response = check_compliance(env, @config_manager)
    return error_response unless error_response.nil?
  end

  @app.call(env)
end