Class: TurbolinksRender::Middleware

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

Defined Under Namespace

Classes: Request, Response

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



68
69
70
# File 'lib/turbolinks_render/middleware.rb', line 68

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/turbolinks_render/middleware.rb', line 72

def call(env)
  rack_request = Rack::Request.new(env)
  request = Request.new(rack_request)
  rack_request.set_header('X-Turbolinks-Render-Candidate', request.candidate_for_turbolinks?)

  rack_status, rack_headers, rack_response = @app.call(env)
  response = Response.new(rack_status, rack_headers, rack_response)

  return [rack_status, rack_headers, rack_response] unless render_with_turbolinks?(request, response)

  rack_headers['Content-Type'] = 'text/javascript'
  rack_headers['Content-Length'] = response.turbolinks_body.bytesize.to_s

  [rack_status, rack_headers, [response.turbolinks_body]]
end