Class: Rack::Pjax

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rack/pjax.rb,
lib/rack/pjax/version.rb

Constant Summary collapse

VERSION =
"0.5.9"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Pjax

Returns a new instance of Pjax.



7
8
9
# File 'lib/rack/pjax.rb', line 7

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rack/pjax.rb', line 11

def call(env)
  status, headers, body = @app.call(env)
  headers = HeaderHash.new(headers)

  if pjax?(env)
    new_body = ""
    body.each do |b|
      parsed_body = Hpricot.XML(b)
      container = parsed_body.at("[@data-pjax-container]")
      if container
        children = container.children
        title = parsed_body.at("title")

        new_body << title.to_s if title
        new_body << container.inner_html
      else
        new_body << b
      end
    end

    body.close if body.respond_to?(:close)
    body = [new_body]

    headers['Content-Length'] &&= bytesize(new_body).to_s
    headers['X-PJAX-URL'] = env['REQUEST_URI'] if env['REQUEST_URI']
  end
  [status, headers, body]
end