Class: Rack::Prerender

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/prerender.rb,
lib/rack/prerender/fetcher.rb,
lib/rack/prerender/version.rb,
lib/rack/prerender/recacher.rb,
lib/rack/prerender/constraint.rb,
lib/rack/prerender/recache_job.rb

Defined Under Namespace

Classes: Constraint, Fetcher, Recacher

Constant Summary collapse

VERSION =
'1.6.2.5'
RecacheJob =
if defined?(::ActiveJob::Base)
  Class.new(::ActiveJob::Base)
elsif defined?(::Sidekiq::Worker)
  Class.new do
    include ::Sidekiq::Worker
    instance_eval { alias perform_later perform_async }
  end
else
  raise NameError, 'requires ActiveJob or Sidekiq'
end.class_eval do
  def perform(url, options)
    ::Rack::Prerender::Recacher.new(options).call(url)
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Prerender

Returns a new instance of Prerender.



11
12
13
14
15
16
# File 'lib/rack/prerender.rb', line 11

def initialize(app, options = {})
  @app        = app
  @constraint = Constraint.new(options)
  @fetcher    = Fetcher.new(options)
  self.class.instance_variable_set(:@options, options)
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



9
10
11
# File 'lib/rack/prerender.rb', line 9

def app
  @app
end

#constraintObject

Returns the value of attribute constraint.



9
10
11
# File 'lib/rack/prerender.rb', line 9

def constraint
  @constraint
end

#fetcherObject

Returns the value of attribute fetcher.



9
10
11
# File 'lib/rack/prerender.rb', line 9

def fetcher
  @fetcher
end

Class Method Details

.fetch(arg, **options) ⇒ Object

utility methods



24
25
26
# File 'lib/rack/prerender.rb', line 24

def self.fetch(arg, **options)
  Fetcher.new(@options.to_h.merge(options)).fetch(arg)
end

.recache_later(url, **options) ⇒ Object



32
33
34
35
36
# File 'lib/rack/prerender.rb', line 32

def self.recache_later(url, **options)
  # require on demand, so ActiveJob/Sidekiq can come later in load order
  require_relative 'prerender/recache_job'
  RecacheJob.perform_later(url, @options.to_h.merge(options))
end

.recache_now(url, **options) ⇒ Object



28
29
30
# File 'lib/rack/prerender.rb', line 28

def self.recache_now(url, **options)
  Recacher.new(@options.to_h.merge(options)).call(url)
end

Instance Method Details

#call(env) ⇒ Object



18
19
20
# File 'lib/rack/prerender.rb', line 18

def call(env)
  constraint.matches?(env) && fetcher.call(env) || app.call(env)
end