lib/crawlable/rack.rb in crawlable-0.0.1.7 vs lib/crawlable/rack.rb in crawlable-0.0.1.8

- old
+ new

@@ -4,22 +4,41 @@ def initialize(app) @app = app end def call(env) - if ENV["HEROKU"] - return sitmap if env['REQUEST_PATH'] =~ /\/sitemap\.xml/i + 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 = File.join(heroku_writable_directory, "sitemap.xml.gz") + 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