Sha256: c93491b64668f952931a81872fe94c3e76897f976d7086bd0ee664dddfa87942

Contents?: true

Size: 796 Bytes

Versions: 7

Compression:

Stored size: 796 Bytes

Contents

module Legitbot
  @rules = []

  ##
  # Lookup a bot based on its signature from +User-Agent+ header.
  #
  # If a block given, passes the found bot to the block.
  #
  # Returns +nil+ if no bot found and a bot match instance
  # otherwise.
  # :yields: a found bot
  #
  def self.bot(userAgent, ip, resolver_config = nil)
    bots =
      @rules.select { |rule|
        rule[:fragments].any? {|f| userAgent.index f}
      }.map { |rule|
        rule[:class].new(ip, resolver_config)
      }

    selected = bots.select { |b| b.valid? }.first if bots.size > 1
    selected = bots.last if selected.nil?

    if selected && block_given?
      yield selected
    else
      selected
    end
  end

  def self.rule(clazz, fragments)
    @rules << {:class => clazz, :fragments => fragments}
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
legitbot-0.3.2 lib/legitbot/legitbot.rb
legitbot-0.3.1 lib/legitbot/legitbot.rb
legitbot-0.3.0 lib/legitbot/legitbot.rb
legitbot-0.2.7 lib/legitbot/legitbot.rb
legitbot-0.2.6 lib/legitbot/legitbot.rb
legitbot-0.2.4 lib/legitbot/legitbot.rb
legitbot-0.2.3 lib/legitbot/legitbot.rb