rack-pjax 0.5.3 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,16 +3,26 @@ Rack-pjax [![stillmaintained](http://stillmaintained.com/eval/rack-pjax.png)](ht
3
3
 
4
4
  Rack-pjax is middleware that lets you serve 'chrome-less' pages in respond to [pjax-requests](https://github.com/defunkt/jquery-pjax).
5
5
 
6
- While responding to a pjax-request is quite easy on a per application basis (e.g. just skip the layout), there are less optimal situations.
7
- For example when you have:
6
+ It does this by simply filtering the generated page; only the title and inner-html of the pjax-container are sent to the client.
8
7
 
9
- - a rack stack, consisting of several pieces of middleware
10
- - an application you can't or don't want to customize server-side (e.g. Spree, Radiant)
8
+ While this won't save you any time rendering the page, it gives you more flexibility where or how to define the pjax-container.
9
+ Ryan Bates featured [rack-pjax on Railscasts](http://railscasts.com/episodes/294-playing-with-pjax) and explains how this gem compares to [pjax_rails](https://github.com/rails/pjax_rails).
11
10
 
12
- Usage
11
+ Installation
13
12
  ------------
14
13
 
15
- I. Include **rack-pjax** as middleware to your application(-stack)
14
+ Check out the [Railscast notes](http://railscasts.com/episodes/294-playing-with-pjax) how to integrate rack-pjax in your Rails 3.1 application.
15
+
16
+ The more generic installation comes down to:
17
+
18
+ I. Add the gem to your Gemfile
19
+
20
+ ```ruby
21
+ # Gemfile
22
+ gem "rack-pjax"
23
+ ```
24
+
25
+ II. Include **rack-pjax** as middleware to your application(-stack)
16
26
 
17
27
  ```ruby
18
28
  # config.ru
@@ -20,7 +30,8 @@ I. Include **rack-pjax** as middleware to your application(-stack)
20
30
  use Rack::Pjax
21
31
  run RackApp::Application
22
32
  ```
23
- II. Install [jquery-pjax](https://github.com/defunkt/jquery-pjax). Make sure to add the 'data-pjax-container'-attribute to the container.
33
+
34
+ III. Install [jquery-pjax](https://github.com/defunkt/jquery-pjax). Make sure to add the 'data-pjax-container'-attribute to the container.
24
35
 
25
36
  ```html
26
37
  <head>
@@ -41,19 +52,13 @@ II. Install [jquery-pjax](https://github.com/defunkt/jquery-pjax). Make sure to
41
52
  </body>
42
53
  ```
43
54
 
44
- III. Fire up your [pushState-enabled browser](http://caniuse.com/#search=pushstate) and enjoy!
45
-
46
-
47
- Installation
48
- ------------
49
-
50
- $ gem install rack-pjax
55
+ IV. Fire up your [pushState-enabled browser](http://caniuse.com/#search=pushstate) and enjoy!
51
56
 
52
57
 
53
58
  Requirements
54
59
  ------------
55
60
 
56
- - Nokogiri
61
+ - Hpricot
57
62
 
58
63
 
59
64
  Author
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Pjax
3
- VERSION = "0.5.3"
3
+ VERSION = "0.5.4"
4
4
  end
5
5
  end
data/lib/rack/pjax.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'nokogiri'
1
+ require 'hpricot'
2
2
 
3
3
  module Rack
4
4
  class Pjax
@@ -14,13 +14,16 @@ module Rack
14
14
 
15
15
  if pjax?(env)
16
16
  new_body = ""
17
- body.each do |r|
18
- container = Nokogiri::HTML(r).at_css("[@data-pjax-container]")
17
+ body.each do |b|
18
+ parsed_body = Hpricot(b)
19
+ container = parsed_body.at("[@data-pjax-container]")
19
20
  if container
20
- title = Nokogiri::HTML(r).at_css("title")
21
- new_body << title.to_s << container.inner_html.strip
21
+ title = parsed_body.at("title")
22
+
23
+ new_body << title.to_s if title
24
+ new_body << container.inner_html
22
25
  else
23
- new_body << r
26
+ new_body << b
24
27
  end
25
28
  end
26
29
 
data/rack-pjax.gemspec CHANGED
@@ -18,6 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- s.add_dependency('rack', '>1.0')
22
- s.add_dependency('nokogiri', '~>1.5.0')
21
+ s.add_dependency('rack', '~>1.3')
22
+ s.add_dependency('hpricot', '~>0.8.4')
23
23
  end
@@ -3,41 +3,75 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
3
  describe Rack::Pjax do
4
4
  include Rack::Test::Methods # can be moved to config
5
5
 
6
- def app
6
+ def generate_app(options={})
7
+ body = options[:body]
8
+
7
9
  Rack::Lint.new(
8
- Rack::Builder.app do
9
- use Rack::Pjax
10
- run lambda { |env|
11
- body = '<html><title>Hello</title><body><div data-pjax-container>World!</div></body></html>'
12
- headers = {
13
- "Content-Length" => Rack::Utils.bytesize(body).to_s,
14
- "Content-Type" => 'text/plain'
15
- }
16
- [200, headers, [body]]
17
- }
18
- end
10
+ Rack::Pjax.new(
11
+ lambda do |env|
12
+ [
13
+ 200,
14
+ {'Content-Type' => 'text/plain', 'Content-Length' => Rack::Utils.bytesize(body).to_s},
15
+ [body]
16
+ ]
17
+ end
18
+ )
19
19
  )
20
20
  end
21
21
 
22
- context "when receiving a pjax-request" do
23
- it "should return title-tag and inner-html of the pjax-container" do
22
+ context "a pjaxified app, upon receiving a pjax-request" do
23
+ before do
24
+ self.class.app = generate_app(:body => '<html><title>Hello</title><body><div data-pjax-container>World!</div></body></html>')
25
+ end
26
+
27
+ it "should return the title-tag in the body" do
24
28
  get "/", {}, {"HTTP_X_PJAX" => "true"}
25
29
  body.should == "<title>Hello</title>World!"
26
30
  end
27
31
 
28
- it "should have the correct Content Length" do
32
+ it "should return the inner-html of the pjax-container in the body" do
33
+ self.class.app = generate_app(:body => '<html><body><div data-pjax-container>World!</div></body></html>')
34
+
35
+ get "/", {}, {"HTTP_X_PJAX" => "true"}
36
+ body.should == "World!"
37
+ end
38
+
39
+ it "should return the correct Content Length" do
29
40
  get "/", {}, {"HTTP_X_PJAX" => "true"}
30
41
  headers['Content-Length'].should == Rack::Utils.bytesize(body).to_s
31
42
  end
43
+
44
+ it "should return the original body when there's no pjax-container" do
45
+ self.class.app = generate_app(:body => '<html><body>Has no pjax-container</body></html>')
46
+
47
+ get "/", {}, {"HTTP_X_PJAX" => "true"}
48
+ body.should == "<html><body>Has no pjax-container</body></html>"
49
+ end
50
+
51
+ it "should preserve whitespaces of the original body" do
52
+ container = "\n <p>\nfirst paragraph</p> <p>Second paragraph</p>\n"
53
+ self.class.app = generate_app(:body =><<-BODY)
54
+ <html>
55
+ <div data-pjax-container>#{container}</div>
56
+ </html>
57
+ BODY
58
+
59
+ get "/", {}, {"HTTP_X_PJAX" => "true"}
60
+ body.should == container
61
+ end
32
62
  end
33
63
 
34
- context "when receiving a non-pjax request" do
35
- it "should not alter the body" do
64
+ context "a pjaxified app, upon receiving a non-pjax request" do
65
+ before do
66
+ self.class.app = generate_app(:body => '<html><title>Hello</title><body><div data-pjax-container>World!</div></body></html>')
67
+ end
68
+
69
+ it "should return the original body" do
36
70
  get "/"
37
71
  body.should == '<html><title>Hello</title><body><div data-pjax-container>World!</div></body></html>'
38
72
  end
39
73
 
40
- it "should have the correct Content Length" do
74
+ it "should return the correct Content Length" do
41
75
  get "/"
42
76
  headers['Content-Length'].should == Rack::Utils.bytesize(body).to_s
43
77
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-pjax
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 3
10
- version: 0.5.3
9
+ - 4
10
+ version: 0.5.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gert Goet
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-02 00:00:00 Z
18
+ date: 2011-11-06 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rack
@@ -23,29 +23,29 @@ dependencies:
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - ">"
26
+ - - ~>
27
27
  - !ruby/object:Gem::Version
28
- hash: 15
28
+ hash: 9
29
29
  segments:
30
30
  - 1
31
- - 0
32
- version: "1.0"
31
+ - 3
32
+ version: "1.3"
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
36
- name: nokogiri
36
+ name: hpricot
37
37
  prerelease: false
38
38
  requirement: &id002 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
42
42
  - !ruby/object:Gem::Version
43
- hash: 3
43
+ hash: 55
44
44
  segments:
45
- - 1
46
- - 5
47
45
  - 0
48
- version: 1.5.0
46
+ - 8
47
+ - 4
48
+ version: 0.8.4
49
49
  type: :runtime
50
50
  version_requirements: *id002
51
51
  description: Serve pjax responses through rack middleware