Sha256: bff6539be514660a8a7e3c1c5c022f7fcb989817e63051e39b8e47367445054c

Contents?: true

Size: 676 Bytes

Versions: 3

Compression:

Stored size: 676 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)
    bot =
      @rules.select { |rule|
        rule[:fragments].any? {|f| userAgent.index f}
      }.map { |rule|
        rule[:class].new(ip, resolver_config)
      }.first

    if bot && block_given?
      yield bot
    else
      bot
    end
  end

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
legitbot-0.1.2 lib/legitbot/legitbot.rb
legitbot-0.1.0 lib/legitbot/legitbot.rb
legitbot-0.0.1 lib/legitbot/legitbot.rb