Sha256: dcb657632617fad173e65c5479183b1f97078290e50af6146672bfc915b4b21b

Contents?: true

Size: 771 Bytes

Versions: 2

Compression:

Stored size: 771 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)
      }

    bot = bots.select { |bot| bot.valid? }.first if bots.size > 1
    bot = bots.first if bot.nil?

    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

2 entries across 2 versions & 1 rubygems

Version Path
legitbot-0.2.1 lib/legitbot/legitbot.rb
legitbot-0.2.0 lib/legitbot/legitbot.rb