Sha256: 43c9278f36806bceca8f217c324608491f70ea0ca53c240b8c3bb54ffda18a5a

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

require 'erb'

module Roboto
  #provides the content of effective robots.txt file
  class ContentProvider
    # Reads the contents of the effective robots.txt file
    # @return [String] the contents of the effective robots.txt file
    def contents(custom_binding = nil)
      return @contents unless @contents.nil?

      @contents = File.read(path)
      if path.extname == '.erb'
        @contents = ERB.new(@contents).result(custom_binding ? custom_binding : binding)
      end
      @contents
    end

    # Determines the most relevant robots.txt file.
    #
    # It checks for the paths in the following order
    #
    # 1. Rails.root/config/robots/<environment>.txt (ie production.txt)
    # 2. Rails.root/config/robots/default.txt
    # 3. The default, blank robots.txt provided by the gem
    # @return [Path] the path of the effective robots.txt file
    def path
      lookup_paths.each do |f|
        if FileTest.exist?(f)
          return f
        end
      end

      #this should never occur because we define a default in the gem
      raise "Robots file not found"
    end

    protected
    def lookup_paths
      [
        Rails.root.join("config/robots/#{Rails.env}.txt"),
        Rails.root.join("config/robots/#{Rails.env}.txt.erb"),
        Rails.root.join(relative_path_to_default),
        Rails.root.join("#{relative_path_to_default}.erb"),
        Roboto::Engine.root.join(relative_path_to_default)
      ]
    end

    def relative_path_to_default
      "config/robots/default.txt"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
roboto-0.2.0 lib/roboto/content_provider.rb
roboto-0.1.0 lib/roboto/content_provider.rb