class AdvancedBotDetection::Test

Attributes

agents[RW]
types[RW]
user_agent_string[RW]

Public Class Methods

new(user_agent_string) click to toggle source
# File lib/advanced_bot_detection/test.rb, line 8
def initialize(user_agent_string)
  load_agents
  @user_agent_string = user_agent_string

  @types = agent.map do |a|
    Array(a['types']).map(&:to_sym)
  end.flatten
end

Public Instance Methods

agent() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 17
def agent
  @agents ||= Test.agents.select do |agent|
    if agent['string_match'] == 'regex'
      @user_agent_string =~ Regexp.new(agent['string'], Regexp::IGNORECASE)
    else
      @user_agent_string.to_s.casecmp(agent['string'].to_s) == 0
    end
  end || []
end
bot?() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 43
def bot?
  type?(:checker, :crawler, :spam)
end
browser?() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 35
def browser?
  type? :browser
end
checker?() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 47
def checker?
  type? :checker
end
crawler?() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 59
def crawler?
  type? :crawler
end
downloader?() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 51
def downloader?
  type? :downloader
end
human?() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 39
def human?
  type?(:browser, :downloader, :proxy, :tablet, :phone)
end
mobile?() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 67
def mobile?
  type?(:tablet, :phone, :mobile_browser)
end
mobile_browser?() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 79
def mobile_browser?
  type? :mobile_browser
end
phone?() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 75
def phone?
  type? :phone
end
proxy?() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 55
def proxy?
  type? :proxy
end
spam?() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 63
def spam?
  type? :spam
end
tablet?() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 71
def tablet?
  type? :tablet
end
type?(*types) click to toggle source
# File lib/advanced_bot_detection/test.rb, line 27
def type?(*types)
  return nil if agent.empty?

  types.any? do |type|
    @types.include? type
  end
end

Private Instance Methods

load_agents() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 85
def load_agents
  Test.agents ||= []
  return unless Test.agents.empty?

  rel_path = ['config', 'user_agents.yml']
  paths.any? do |base_path|
    if File.exist? base_path.join(*rel_path)
      Test.agents = YAML.load(File.open(base_path.join(*rel_path), 'r'))
    end
  end
end
paths() click to toggle source
# File lib/advanced_bot_detection/test.rb, line 97
def paths
  base_paths = []
  base_paths << Rails.root if defined? Rails
  base_paths << AdvancedBotDetection.root
  base_paths
end