rack_phantom 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7b24aff91966a742772634e4476f18f033a7d90e
4
+ data.tar.gz: 3eeb030ab9e14d4905975328c09c911fbc23fcb1
5
+ SHA512:
6
+ metadata.gz: 0e8ed289c296b7eb6ff2cf190f1029192de33efcb42b9e39578b2f1ca1ed312844d1abdb3eafc55c8cec83dcbb4a2fb75a9f780a9d611c5b5a46662c476e1988
7
+ data.tar.gz: 6ae87b22d1b4c2ace8bef14f9fd9020a2167dc264fc60c30abfa48e4ec046d42937cd08a335106ae4100cdef9486dcf691ce68f15b8db1caab5631e3b338992d
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1 @@
1
+ 2.0.0
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - jruby-19mode # JRuby in 1.9 mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack_phantom.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Selva
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ # RackPhantom
2
+
3
+ Rack middlware to render single page apps (or any html page with javascript) with phantomjs and return plain html for bots.
4
+
5
+ ## Travis
6
+
7
+ [![Build Status](https://travis-ci.org/selvakn/rack_phantom.png)](https://travis-ci.org/selvakn/rack_phantom)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'rack_phantom'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rack_phantom
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,12 @@
1
+ var system = require('system');
2
+ var page = require('webpage').create();
3
+
4
+ page.content = system.args[1];
5
+
6
+ var result = page.evaluate(function(){
7
+ return document.documentElement.outerHTML;
8
+ });
9
+
10
+ console.log(result);
11
+
12
+ phantom.exit();
@@ -0,0 +1,48 @@
1
+ require 'rack_phantom/version'
2
+ require 'phantomjs'
3
+
4
+ module RackPhantom
5
+ BOTS = [
6
+ 'AdsBot-Google',
7
+ 'Baiduspider',
8
+ 'Googlebot',
9
+ 'Googlebot-Mobile',
10
+ 'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)',
11
+ 'Mozilla/5.0 (compatible; Ask Jeeves/Teoma; +http://about.ask.com/en/docs/about/webmasters.shtml)'
12
+ ]
13
+
14
+ class Middleware
15
+ def initialize(app)
16
+ @app = app
17
+ end
18
+
19
+ def call(env)
20
+ status, headers, response = @app.call(env)
21
+
22
+ return [status, headers, response] unless render?(headers, env)
23
+
24
+ html = Phantomjs.run('js/render.js', %['#{response}'])
25
+ [status, headers, html]
26
+ end
27
+
28
+ private
29
+ def render?(headers, env)
30
+ html_response?(headers) && bot?(env) && get?(env)
31
+ end
32
+
33
+ def html_response?(headers)
34
+ headers['Content-Type'].include? 'text/html'
35
+ end
36
+
37
+ def get?(env)
38
+ env['REQUEST_METHOD'] == 'GET'
39
+ end
40
+
41
+ def bot?(env)
42
+ BOTS.include? env['HTTP_USER_AGENT']
43
+ end
44
+ end
45
+ end
46
+
47
+
48
+ require 'rack_phantom/railtie' if defined?(Rails)
@@ -0,0 +1,7 @@
1
+ module RackPhantom
2
+ class Railtie < Rails::Railtie
3
+ initializer "rack_phantom.configure_rails_initialization" do |app|
4
+ app.middleware.use RackPhantom::Middleware
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module RackPhantom
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack_phantom/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rack_phantom'
8
+ spec.version = RackPhantom::VERSION
9
+ spec.authors = ['Selva']
10
+ spec.email = ['k.n.selvakumar@gmail.com']
11
+ spec.description = 'Rack middlware to render single page apps (or any html page with javascript) with phantomjs and return plain html for bots.'
12
+ spec.summary = ''
13
+ spec.homepage = 'https://github.com/selvakn/rack_phantom'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = %w(lib)
20
+
21
+ spec.add_dependency 'rack', '~> 1.5.2'
22
+ spec.add_dependency 'phantomjs.rb', '~> 1.0.1'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rack-test'
27
+ spec.add_development_dependency 'rspec'
28
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe RackPhantom do
4
+
5
+ let(:html) { %q{<html><head><script>document.write("Hello World");</script></head><body></body></html>} }
6
+ let(:real_app) { ->(env) { [200, {'Content-Type' => 'text/html'}, html] } }
7
+ let(:app) { RackPhantom::Middleware.new(real_app) }
8
+
9
+
10
+ context 'for bots' do
11
+ it 'should execute javascript' do
12
+ get '/', {}, as_bot
13
+ last_response.should be_ok
14
+ last_response.body.strip.should == evaluated_html
15
+ end
16
+
17
+ it 'escapes the single quotes'
18
+
19
+ def evaluated_html
20
+ %q{<html><head><script>document.write("Hello World");</script></head><body>Hello World</body></html>}
21
+ end
22
+ end
23
+
24
+
25
+ context 'for non bots' do
26
+ it 'should not render' do
27
+ get '/'
28
+ last_response.body.strip.should == html
29
+ end
30
+ end
31
+
32
+ context 'for POST requests' do
33
+ it 'should not render' do
34
+ post '/', {}, as_bot
35
+ last_response.body.strip.should == html
36
+ end
37
+ end
38
+
39
+ context 'for non html responses' do
40
+ let(:json) { '{}' }
41
+ let(:real_app) { ->(env) { [200, {'Content-Type' => 'application/json'}, json] } }
42
+
43
+ it 'should not render' do
44
+ get '/'
45
+ last_response.body.strip.should == json
46
+ end
47
+ end
48
+
49
+ def as_bot
50
+ {'HTTP_USER_AGENT' => 'Googlebot'}
51
+ end
52
+ end
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require
4
+ require 'rack/test'
5
+
6
+ require 'rack_phantom'
7
+
8
+ RSpec.configure do |config|
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.run_all_when_everything_filtered = true
11
+ config.filter_run :focus
12
+
13
+ config.order = 'random'
14
+
15
+ config.include Rack::Test::Methods
16
+
17
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_phantom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Selva
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.5.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.5.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: phantomjs.rb
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Rack middlware to render single page apps (or any html page with javascript)
98
+ with phantomjs and return plain html for bots.
99
+ email:
100
+ - k.n.selvakumar@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - .gitignore
106
+ - .rspec
107
+ - .ruby-version
108
+ - .travis.yml
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - js/render.js
114
+ - lib/rack_phantom.rb
115
+ - lib/rack_phantom/railtie.rb
116
+ - lib/rack_phantom/version.rb
117
+ - rack_phantom.gemspec
118
+ - spec/rack_phantom_spec.rb
119
+ - spec/spec_helper.rb
120
+ homepage: https://github.com/selvakn/rack_phantom
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.0.3
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: ''
144
+ test_files:
145
+ - spec/rack_phantom_spec.rb
146
+ - spec/spec_helper.rb