Class: Ronflex::Rest

Inherits:
Object
  • Object
show all
Defined in:
lib/ronflex/rest.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rest

Initializes the middleware with the given app

Parameters:

  • app (Object)

    The application instance that this middleware wraps It will call the next middleware or application in the stack.



11
12
13
# File 'lib/ronflex/rest.rb', line 11

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Array

Rack middleware call method that processes the incoming request.

It checks if the request should be granted access, and if not, it returns the maintenance page or proceeds with the normal request handling.

Parameters:

  • env (Hash)

    Rack environment hash which contains information about the incoming request, such as headers, parameters, etc.

Returns:

  • (Array)

    Rack response in the form of [status, headers, body] If access is allowed, the request is passed to the next middleware/application. Otherwise, a 503 status with the maintenance page is returned.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ronflex/rest.rb', line 26

def call(env)
  request = Rack::Request.new(env)
  model = Ronflex.configuration.provider.call(env)

  # If the request is always accessible (excluded path), pass the request to the next app
  return @app.call(env) if always_access?(request)

  # If the system is enabled and the model is present and authorized, proceed with the request
  if Ronflex.configuration.enable
    if model_present?(model) && routes_authorized?(model, request)
      return @app.call(env)
    else
      # If conditions are not met, return maintenance page
      return [503, { "Content-Type" => "text/html" }, [maintenance_page]]
    end
  end

  # Default pass-through for the request
  @app.call(env)
end