Class: Rack::Prerender::Constraint

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/prerender/constraint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#blacklistObject (readonly)

Returns the value of attribute blacklist.



4
5
6
# File 'lib/rack/prerender/constraint.rb', line 4

def blacklist
  @blacklist
end

#crawler_user_agentsObject (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_ignoreObject (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

#whitelistObject (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

Returns:

  • (Boolean)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rack/prerender/constraint.rb', line 13

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.downcase) ||
                      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.downcase))

  true
end