Class: Browser::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/browser/bot.rb,
lib/browser/bot/keyword_matcher.rb,
lib/browser/bot/known_bots_matcher.rb,
lib/browser/bot/empty_user_agent_matcher.rb

Defined Under Namespace

Classes: EmptyUserAgentMatcher, KeywordMatcher, KnownBotsMatcher

Constant Summary collapse

GENERIC_NAME =
"Generic Bot"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ua) ⇒ Bot

Returns a new instance of Bot.



43
44
45
46
# File 'lib/browser/bot.rb', line 43

def initialize(ua)
  @ua = ua.downcase.strip
  @browser = Browser.new(@ua)
end

Class Method Details

.bot_exceptionsObject



27
28
29
# File 'lib/browser/bot.rb', line 27

def self.bot_exceptions
  @bot_exceptions ||= load_yaml("bot_exceptions.yml")
end

.botsObject



23
24
25
# File 'lib/browser/bot.rb', line 23

def self.bots
  @bots ||= load_yaml("bots.yml")
end

.default_matchersObject



11
12
13
14
15
16
17
# File 'lib/browser/bot.rb', line 11

def self.default_matchers
  [
    EmptyUserAgentMatcher,
    KnownBotsMatcher,
    KeywordMatcher
  ]
end

.load_yaml(path) ⇒ Object



19
20
21
# File 'lib/browser/bot.rb', line 19

def self.load_yaml(path)
  YAML.load_file(Browser.root.join(path))
end

.matchersObject



7
8
9
# File 'lib/browser/bot.rb', line 7

def self.matchers
  @matchers ||= default_matchers
end

.search_enginesObject



31
32
33
# File 'lib/browser/bot.rb', line 31

def self.search_engines
  @search_engines ||= load_yaml("search_engines.yml")
end

.why?(ua) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/browser/bot.rb', line 35

def self.why?(ua)
  ua = ua.downcase.strip
  browser = Browser.new(ua)
  matchers.find {|matcher| matcher.call(ua, browser) }
end

Instance Method Details

#bot?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/browser/bot.rb', line 48

def bot?
  !bot_exception? && detect_bot?
end

#nameObject



60
61
62
63
64
# File 'lib/browser/bot.rb', line 60

def name
  return unless bot?

  self.class.bots.find {|key, _| ua.include?(key) }&.last || GENERIC_NAME
end

#search_engine?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/browser/bot.rb', line 56

def search_engine?
  self.class.search_engines.any? {|key, _| ua.include?(key) }
end

#why?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/browser/bot.rb', line 52

def why?
  self.class.matchers.find {|matcher| matcher.call(ua, self) }
end