module Crawlable class Rack def initialize(app) @app = app end def call(env) if using_heroku?(env) || true if file = Crawlable::Sitemap.find(env['REQUEST_PATH'], heroku_writable_directory) return sitemap(file) elsif file = Crawlable::Feed.find(env['REQUEST_PATH'], heroku_writable_directory) return feed(file) end end @app.call(env) end def sitemap(file) [200, { 'Cache-Control' => 'public, max-age=86400', 'Content-Length' => File.size(file).to_s, 'Content-Type' => 'text/xml' }, IO.read(file)] end def feed(file) [200, { 'Cache-Control' => 'public, max-age=86400', 'Content-Length' => File.size(file).to_s, 'Content-Type' => 'text/xml' }, IO.read(file)] end def heroku_writable_directory "#{Rails.root}/tmp" end def using_heroku?(env) if env["HEROKU"].nil? if env["HEROKU_PORT"].nil? && ENV["HEROKU_TYPE"].nil? env["HEROKU"] = false else env["HEROKU"] = true end end env["HEROKU"] == true end end end