module Legitbot

Bot lookup based on user agent

Constants

VERSION

Attributes

resolver_config[RW]

Public Class Methods

bot(user_agent, ip) { |selected| ... } click to toggle source

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.

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

# File lib/legitbot/legitbot.rb, line 22
def self.bot(user_agent, ip)
  bots = @rules
         .select { |rule| rule[:fragments].any? { |f| user_agent.index f } }
         .map { |rule| rule[:class].new(ip) }

  selected = bots.select(&:valid?).first if bots.size > 1
  selected = bots.last if selected.nil?

  if selected && block_given?
    yield selected
  else
    selected
  end
end
rule(clazz, fragments) click to toggle source

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

# File lib/legitbot/legitbot.rb, line 38
def self.rule(clazz, fragments)
  @rules << { class: clazz, fragments: fragments }
end