module Robotron
  class << self
    def call(env)
      body = if Rails.env.production?
        File.read(Rails.root.join('config', 'robots.txt'))
      else
        default_body
      end

      [200, default_headers, [body]]
    rescue Errno::ENOENT
      [404, default_headers, [default_body]]
    end

    def default_body
      "User-agent: *\nDisallow: /"
    end

    def default_headers
      {
        'Content-Type' => 'text/plain',
        'Cache-Control' => 'public, max-age=31557600'
      }
    end
  end
end

module Robotron
  class << self
    def call(env)
      body = File.read(robots_path)

      [200, default_headers, [body]]
    rescue Errno::ENOENT
      [200, default_headers, [default_body]]
    end

    def environment
      ENV['RACK_ENV'] || 'development'
    end

    def robots_directory
      defined?(Rails) ? Rails.root.join('config') : File.join('spec')
    end

    def robots_path
      File.join(robots_directory, "robots.txt.#{ environment }")
    end

    def default_body
      "User-Agent: *\nDisallow: /"
    end

    def default_headers
      {
        'Content-Type' => 'text/plain',
        'Cache-Control' => 'public, max-age=31557600'
      }
    end
  end
end

Allow robots.txt for multiple environments
Allow robots.txt for multiple environments
