-
Notifications
You must be signed in to change notification settings - Fork 0
/
bots.rb
31 lines (27 loc) · 920 Bytes
/
bots.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
require 'nokogiri'
# sourced from https://github.com/charlotte-ruby/impressionist/blob/master/lib/impressionist/bots.rb
module MongoidTraffic
class Logger
class Bots
DATA_URL = 'http://www.user-agents.org/allagents.xml'.freeze
FILE_PATH = File.join(File.dirname(__dir__), '..', '..', 'vendor', 'mongoid_traffic', 'allagents.xml')
class << self
def list
@list ||= begin
response = File.open(FILE_PATH).read
doc = Nokogiri::XML(response)
doc.xpath('//user-agent').inject([]) do |res, agent|
type = agent.xpath('Type').text
res << agent.xpath('String').text.gsub('<', '<') if %w(R S).include?(type)
res
end
end
end
def is_a_bot?(referer)
return false unless referer.present?
list.include?(referer)
end
end
end
end
end