Sha256: f26ee244aefede1e11e695769615d4b53dc04ddaf87b117a22d55516dab87569

Contents?: true

Size: 1.03 KB

Versions: 34

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

##
# Bot lookup based on user agent
module Legitbot
  @rules = []

  class << self
    attr_accessor :resolver_config
  end

  ##
  # 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
  #
  # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  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
  # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

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

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
legitbot-1.11.2 lib/legitbot/legitbot.rb
legitbot-1.11.1 lib/legitbot/legitbot.rb
legitbot-1.11.0 lib/legitbot/legitbot.rb
legitbot-1.10.6 lib/legitbot/legitbot.rb
legitbot-1.10.5 lib/legitbot/legitbot.rb
legitbot-1.10.4 lib/legitbot/legitbot.rb
legitbot-1.10.3 lib/legitbot/legitbot.rb
legitbot-1.10.1 lib/legitbot/legitbot.rb
legitbot-1.10.0 lib/legitbot/legitbot.rb
legitbot-1.9.4 lib/legitbot/legitbot.rb
legitbot-1.9.3 lib/legitbot/legitbot.rb
legitbot-1.9.2 lib/legitbot/legitbot.rb
legitbot-1.9.1 lib/legitbot/legitbot.rb
legitbot-1.9.0 lib/legitbot/legitbot.rb
legitbot-1.8.0 lib/legitbot/legitbot.rb
legitbot-1.7.3 lib/legitbot/legitbot.rb
legitbot-1.7.2 lib/legitbot/legitbot.rb
legitbot-1.7.1 lib/legitbot/legitbot.rb
legitbot-1.7.0 lib/legitbot/legitbot.rb
legitbot-1.6.1 lib/legitbot/legitbot.rb