Sha256: 76232c2ff56ca8a8610692ab02d319af0729c8f6fb6ed3d3ebd402b4c3f4e06e

Contents?: true

Size: 1.34 KB

Versions: 3

Compression:

Stored size: 1.34 KB

Contents

module Middleman
  module Robots
    # Robots Group Class
    #
    # Group class generate block in robots.txt
    class Group
      attr_reader :user_agent, :disallow, :allow

      def initialize(rule)
        @user_agent = generate_user_agent(rule)
        @disallow   = generate_disallow(rule)
        @allow      = generate_allow(rule)
      end

      def text
        group = []
        group << "User-Agent: #{@user_agent}" unless @user_agent.empty?
        group << @disallow.collect{|item| "Disallow: #{item}" }.join("\n") if @disallow.length > 0
        group << @allow.collect{|item| "Allow: #{item}" }.join("\n") if @allow.length > 0
        group.join("\n") + "\n"
      end

      private
        def generate_user_agent(rule)
          return '*' unless rule.key?('user-agent') || rule.key?(:user_agent)
          rule[:user_agent] || rule['user-agent']
        end

        def generate_disallow(rule)
          paths = []
          return paths unless rule.has_key?(:disallow)

          rule[:disallow].each do |path|
            paths << File.join('/', path)
          end
          paths
        end

        def generate_allow(rule)
          paths = []
          return paths unless rule.has_key?(:allow)

          rule[:allow].each do |path|
            paths << File.join('/', path)
          end
          paths
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
middleman-robots-1.3.2 lib/middleman-robots/group.rb
middleman-robots-1.3.1 lib/middleman-robots/group.rb
middleman-robots-1.3.0 lib/middleman-robots/group.rb