seo4ajax_rails 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/seo4ajax_rails.rb +154 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 182eec52dc5bf9573357fbc29404a50dadd0094a
|
4
|
+
data.tar.gz: 7890a986393cef772426937ae9f914142f93360a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5bf2948ce61acf3fb41f769cc4ccb0fde3aefe07bc0ac4e8bb7d40558a41e07a85cc5990391d5317ba2ddf0fa9e06decd17785bb1e01f310e770866d0cdb537a
|
7
|
+
data.tar.gz: 0338dd458eda203b20d2266189d79e6813f499fdacbd1c6a38c35b7acfb0120b485262822c389ec71c27f7dbeb09fc1ae7e6a834872bad1cbd338104fcb0ff1f
|
@@ -0,0 +1,154 @@
|
|
1
|
+
module Rack
|
2
|
+
class SEO4Ajax
|
3
|
+
require 'net/http'
|
4
|
+
require 'active_support'
|
5
|
+
|
6
|
+
def initialize(app, options={})
|
7
|
+
# googlebot, yahoo, and bingbot are not in this list because
|
8
|
+
# we support _escaped_fragment_ and want to ensure people aren't
|
9
|
+
# penalized for cloaking.
|
10
|
+
@crawler_user_agents = [
|
11
|
+
# 'googlebot',
|
12
|
+
# 'yahoo',
|
13
|
+
# 'bingbot',
|
14
|
+
'baiduspider',
|
15
|
+
'facebookexternalhit',
|
16
|
+
'twitterbot',
|
17
|
+
'rogerbot',
|
18
|
+
'linkedinbot',
|
19
|
+
'embedly',
|
20
|
+
'bufferbot',
|
21
|
+
'quora link preview',
|
22
|
+
'showyoubot',
|
23
|
+
'outbrain',
|
24
|
+
'pinterest/0.',
|
25
|
+
'developers.google.com/+/web/snippet',
|
26
|
+
'www.google.com/webmasters/tools/richsnippets',
|
27
|
+
'slackbot',
|
28
|
+
'vkShare',
|
29
|
+
'W3C_Validator',
|
30
|
+
'redditbot',
|
31
|
+
'Applebot',
|
32
|
+
'WhatsApp',
|
33
|
+
'flipboard',
|
34
|
+
'tumblr',
|
35
|
+
'bitlybot',
|
36
|
+
'SkypeUriPreview',
|
37
|
+
'nuzzel',
|
38
|
+
'Discordbot',
|
39
|
+
'Google Page Speed',
|
40
|
+
'Qwantify'
|
41
|
+
]
|
42
|
+
|
43
|
+
@extensions_to_ignore = [
|
44
|
+
'.js',
|
45
|
+
'.css',
|
46
|
+
'.xml',
|
47
|
+
'.less',
|
48
|
+
'.png',
|
49
|
+
'.jpg',
|
50
|
+
'.jpeg',
|
51
|
+
'.gif',
|
52
|
+
'.pdf',
|
53
|
+
'.doc',
|
54
|
+
'.txt',
|
55
|
+
'.ico',
|
56
|
+
'.rss',
|
57
|
+
'.zip',
|
58
|
+
'.mp3',
|
59
|
+
'.rar',
|
60
|
+
'.exe',
|
61
|
+
'.wmv',
|
62
|
+
'.doc',
|
63
|
+
'.avi',
|
64
|
+
'.ppt',
|
65
|
+
'.mpg',
|
66
|
+
'.mpeg',
|
67
|
+
'.tif',
|
68
|
+
'.wav',
|
69
|
+
'.mov',
|
70
|
+
'.psd',
|
71
|
+
'.ai',
|
72
|
+
'.xls',
|
73
|
+
'.mp4',
|
74
|
+
'.m4a',
|
75
|
+
'.swf',
|
76
|
+
'.dat',
|
77
|
+
'.dmg',
|
78
|
+
'.iso',
|
79
|
+
'.flv',
|
80
|
+
'.m4v',
|
81
|
+
'.torrent'
|
82
|
+
]
|
83
|
+
|
84
|
+
@options = options
|
85
|
+
@extensions_to_ignore = @options[:extensions_to_ignore] if @options[:extensions_to_ignore]
|
86
|
+
@crawler_user_agents = @options[:crawler_user_agents] if @options[:crawler_user_agents]
|
87
|
+
@app = app
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
def call(env)
|
92
|
+
if should_show_snapshot(env)
|
93
|
+
snapshot = get_snapshot_response(env)
|
94
|
+
if snapshot
|
95
|
+
response = build_rack_response_from_seo4ajax(snapshot)
|
96
|
+
return response.finish
|
97
|
+
end
|
98
|
+
end
|
99
|
+
@app.call(env)
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
def should_show_snapshot(env)
|
104
|
+
user_agent = env['HTTP_USER_AGENT']
|
105
|
+
is_requesting_snapshot = false
|
106
|
+
return false if !user_agent
|
107
|
+
return false if env['REQUEST_METHOD'] != 'GET'
|
108
|
+
request = Rack::Request.new(env)
|
109
|
+
is_requesting_snapshot = true if Rack::Utils.parse_query(request.query_string).has_key?('_escaped_fragment_')
|
110
|
+
is_requesting_snapshot = true if @crawler_user_agents.any? { |crawler_user_agent| user_agent.downcase.include?(crawler_user_agent.downcase) }
|
111
|
+
return false if @extensions_to_ignore.any? { |extension| request.fullpath.include? extension }
|
112
|
+
return is_requesting_snapshot
|
113
|
+
end
|
114
|
+
|
115
|
+
|
116
|
+
def get_snapshot_response(env)
|
117
|
+
begin
|
118
|
+
url = URI.parse(build_api_url(env))
|
119
|
+
headers = {
|
120
|
+
'User-Agent' => env['HTTP_USER_AGENT'],
|
121
|
+
'Accept-Encoding' => 'gzip'
|
122
|
+
}
|
123
|
+
req = Net::HTTP::Get.new(url.request_uri, headers)
|
124
|
+
http = Net::HTTP.new(url.host, url.port)
|
125
|
+
http.use_ssl = true if url.scheme == 'https'
|
126
|
+
response = http.request(req)
|
127
|
+
if response['Content-Encoding'] == 'gzip'
|
128
|
+
response.body = ActiveSupport::Gzip.decompress(response.body)
|
129
|
+
response['Content-Length'] = response.body.length
|
130
|
+
response.delete('Content-Encoding')
|
131
|
+
response.delete('Transfer-Encoding')
|
132
|
+
end
|
133
|
+
response
|
134
|
+
rescue
|
135
|
+
nil
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
|
140
|
+
def build_api_url(env)
|
141
|
+
fullpath = Rack::Request.new(env).fullpath
|
142
|
+
seo4ajax_url = @options[:seo4ajax_service_url] || ENV['SEO4AJAX_SERVICE_URL'] || 'http://api.seo4ajax.com/'
|
143
|
+
forward_slash = seo4ajax_url[-1, 1] == '/' ? '' : '/'
|
144
|
+
token = ENV['SEO4AJAX_TOKEN'] if ENV['SEO4AJAX_TOKEN']
|
145
|
+
token = @options[:seo4ajax_token] if @options[:seo4ajax_token]
|
146
|
+
"#{seo4ajax_url}#{forward_slash}#{token}#{fullpath}"
|
147
|
+
end
|
148
|
+
|
149
|
+
|
150
|
+
def build_rack_response_from_seo4ajax(snapshot)
|
151
|
+
Rack::Response.new(snapshot.body, snapshot.code, snapshot.header)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: seo4ajax_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- SEO4Ajax
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-16 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: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Rails middleware to integrate SEO4Ajax in your web application
|
42
|
+
email:
|
43
|
+
- support@seo4ajax.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- lib/seo4ajax_rails.rb
|
49
|
+
homepage: https://github.com/seo4ajax/seo4ajax_rails
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata: {}
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 2.4.5.2
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: Use SEO4Ajax to serve prerendered pages to bots
|
73
|
+
test_files: []
|