Class: Browser::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/browser/bot.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ua) ⇒ Bot

Returns a new instance of Bot.



34
35
36
# File 'lib/browser/bot.rb', line 34

def initialize(ua)
  @ua = ua
end

Instance Attribute Details

#uaObject (readonly)

Returns the value of attribute ua.



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

def ua
  @ua
end

Class Method Details

.bot_exceptionsObject



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

def self.bot_exceptions
  @bot_exceptions ||= YAML
                      .load_file(Browser.root.join("bot_exceptions.yml"))
end

.botsObject



13
14
15
# File 'lib/browser/bot.rb', line 13

def self.bots
  @bots ||= YAML.load_file(Browser.root.join("bots.yml"))
end

.detect_empty_ua!Object



5
6
7
# File 'lib/browser/bot.rb', line 5

def self.detect_empty_ua!
  @detect_empty_ua = true
end

.detect_empty_ua?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/browser/bot.rb', line 9

def self.detect_empty_ua?
  @detect_empty_ua
end

.search_enginesObject



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

def self.search_engines
  @search_engines ||= YAML
                      .load_file(Browser.root.join("search_engines.yml"))
end

.why?(ua) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.why?(ua)
  downcased_ua = ua.downcase
  bots.find {|key, _| downcased_ua.include?(key) }
end

Instance Method Details

#bot?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/browser/bot.rb', line 38

def bot?
  bot_with_empty_ua? || (!bot_exception? && detect_bot?)
end

#nameObject



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

def name
  return unless bot?
  return "Generic Bot" if bot_with_empty_ua?

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

#search_engine?Boolean

Returns:

  • (Boolean)


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

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