Class: OpallaMiddleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ OpallaMiddleware

Returns a new instance of OpallaMiddleware.



2
3
4
# File 'lib/opalla/middleware.rb', line 2

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



6
7
8
9
10
11
# File 'lib/opalla/middleware.rb', line 6

def call(env)
  s, h, r = @app.call(env)
  return [s, h, r] unless h['Content-Type'] =~ %r{text/html}
  html = r.body.gsub("<body>", "<body>#{js_routes}")
  [s, h, [html]]
end

#js_routesObject



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

def js_routes
  <<~JS
    <script>
      window.opalla_data = #{ Opalla::Util.data_dump.to_json }
    </script>
  JS
end