Class: Rack::Prerender::Constraint
- Inherits:
-
Object
- Object
- Rack::Prerender::Constraint
- Defined in:
- lib/rack/prerender/constraint.rb
Instance Attribute Summary collapse
-
#blacklist ⇒ Object
readonly
Returns the value of attribute blacklist.
-
#crawler_user_agents ⇒ Object
readonly
Returns the value of attribute crawler_user_agents.
-
#extensions_to_ignore ⇒ Object
readonly
Returns the value of attribute extensions_to_ignore.
-
#whitelist ⇒ Object
readonly
Returns the value of attribute whitelist.
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Constraint
constructor
A new instance of Constraint.
-
#matches?(env) ⇒ Boolean
This is run on every request, so performance matters here.
Constructor Details
#initialize(opts = {}) ⇒ Constraint
Returns a new instance of Constraint.
6 7 8 9 10 11 |
# File 'lib/rack/prerender/constraint.rb', line 6 def initialize(opts = {}) @blacklist = cast_to_regexp(opts[:blacklist], escape: false) @whitelist = cast_to_regexp(opts[:whitelist], escape: false) @crawler_user_agents = cast_to_regexp(opts[:crawler_user_agents] || CRAWLER_USER_AGENTS) @extensions_to_ignore = cast_to_regexp(opts[:extensions_to_ignore] || EXTENSIONS_TO_IGNORE) end |
Instance Attribute Details
#blacklist ⇒ Object (readonly)
Returns the value of attribute blacklist.
4 5 6 |
# File 'lib/rack/prerender/constraint.rb', line 4 def blacklist @blacklist end |
#crawler_user_agents ⇒ Object (readonly)
Returns the value of attribute crawler_user_agents.
4 5 6 |
# File 'lib/rack/prerender/constraint.rb', line 4 def crawler_user_agents @crawler_user_agents end |
#extensions_to_ignore ⇒ Object (readonly)
Returns the value of attribute extensions_to_ignore.
4 5 6 |
# File 'lib/rack/prerender/constraint.rb', line 4 def extensions_to_ignore @extensions_to_ignore end |
#whitelist ⇒ Object (readonly)
Returns the value of attribute whitelist.
4 5 6 |
# File 'lib/rack/prerender/constraint.rb', line 4 def whitelist @whitelist end |
Instance Method Details
#matches?(env) ⇒ Boolean
This is run on every request, so performance matters here.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rack/prerender/constraint.rb', line 14 def matches?(env) return false if env['REQUEST_METHOD'] != 'GET' || env['HTTP_X_PRERENDER'] || (user_agent = env['HTTP_USER_AGENT']).nil? query = env['QUERY_STRING'].to_s return false unless crawler_user_agents.match?(user_agent) || env['HTTP_X_BUFFERBOT'] || query.include?('_escaped_fragment_') path = env['SCRIPT_NAME'].to_s + env['PATH_INFO'].to_s fullpath = query.empty? ? path : "#{path}?#{query}" return false if extensions_to_ignore.match?(fullpath) return false if whitelist && !whitelist.match?(fullpath) return false if blacklist && (blacklist.match?(fullpath) || blacklist.match?(env['HTTP_REFERER'].to_s)) true end |