Class: Integral::Middleware::PageRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/integral/middleware/page_router.rb

Overview

Handles dynamic page routing. Checks all GET requests to see if the PATH matches an Integral::Page path If a match is found the PATH is rewritten from human readable into something Rails router understands. i.e. ‘/company/who-we-are’ -> /pages/12

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ PageRouter

Returns a new instance of PageRouter.



9
10
11
# File 'lib/integral/middleware/page_router.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object

Handles dynamic Integral::Page routing.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/integral/middleware/page_router.rb', line 14

def call(env)
  request = Rack::Request.new(env)

  # Return early if request is not a GET
  return @app.call(env) unless request.get?

  # Return early if request is within backend area
  backend_path = "/#{Integral.backend_namespace}/"
  return @app.call(env) if request.path_info.starts_with?(backend_path)

  # Rewrites path if the request linked to an Integral::Page
  rewrite_path(env, request.path_info)

  @app.call(env)
end