class BetterRobots::Generator

Constants

DEFAULT_DISALLOW_TEXT
ROBOTS_CACHE

Public Class Methods

call(env) click to toggle source
# File lib/better_robots.rb, line 17
def call(env)
  res = cached_robots_txt_for(env['SERVER_NAME'])
  [ 200, headers_for(res), [ res.txt ] ]
end
config() click to toggle source
# File lib/better_robots.rb, line 11
def config
  @config ||= {
    :robots_txt_path => ((Rails.root.join("public") if defined? Rails) || ".")
  }
end

Private Class Methods

cached_robots_txt_for(server_name) click to toggle source
# File lib/better_robots.rb, line 22
def cached_robots_txt_for(server_name)
  ROBOTS_CACHE[server_name] ||= robots_txt_for(server_name)
end
headers_for(res) click to toggle source
# File lib/better_robots.rb, line 39
def headers_for(res)
  {
    "Content-Type" => "text/plain",
    "Content-Length" => res.length.to_s
  }
end
read_robots_file(server_name) click to toggle source
# File lib/better_robots.rb, line 35
def read_robots_file(server_name)
  File.read(File.join(config[:robots_txt_path], "#{server_name}.robots.txt"))
end
robots_txt_for(server_name) click to toggle source
# File lib/better_robots.rb, line 26
def robots_txt_for(server_name)
  begin
    txt = read_robots_file(server_name)
    OpenStruct.new(:txt => txt, :length => txt.length)
  rescue Errno::ENOENT
    OpenStruct.new(:txt => DEFAULT_DISALLOW_TEXT, :length => DEFAULT_DISALLOW_TEXT.length)
  end
end