Class: Rack::Prerender::Recacher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Recacher

Returns a new instance of Recacher.



6
7
8
# File 'lib/rack/prerender/recacher.rb', line 6

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#api_urlObject



19
20
21
22
# File 'lib/rack/prerender/recacher.rb', line 19

def api_url
  options[:prerender_recache_url] || ENV['PRERENDER_RECACHE_URL'] ||
    'http://api.prerender.io/recache'
end

#call(cached_url) ⇒ Object



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

def call(cached_url)
  uri = URI(api_url)
  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    request = Net::HTTP::Post.new(uri, 'Content-Type' => 'application/json')
    request.body = %({"prerenderToken":"#{token}","url":"#{cached_url}"})
    http.request(request) # => Net::HTTPResponse object
  end
end

#tokenObject



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

def token
  options[:prerender_token] || ENV['PRERENDER_TOKEN']
end