voight_kampff 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/voight_kampff/test.rb +19 -6
- data/lib/voight_kampff/version.rb +1 -1
- data/spec/lib/voight_kampff/test_spec.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1f725ef9bb3130917b515802108e7afa6c5f923
|
4
|
+
data.tar.gz: db35fc150b61de1541d88893316d716d1584dd9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfcccea8835dbb3857bae599f4b15dc93d49d846b2be5b0f27fa83b5c59cbe94b412bb71feab1b50e8ec284a94399fc4316ffdc3d6aa2852ad734b54750bc609
|
7
|
+
data.tar.gz: 9ac3a9a20d1c4e88faf9d4a7575c3ade972ff27efe936c4d6cfca851f2045680f0829d6353a02029b539f01bd2fcba32613dd03f3dd77f493cf9410e9aedeafa
|
data/lib/voight_kampff/test.rb
CHANGED
@@ -9,11 +9,7 @@ module VoightKampff
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def agent
|
12
|
-
|
13
|
-
|
14
|
-
@agent ||= @@crawlers.find do |crawler|
|
15
|
-
self.user_agent_string =~ Regexp.new(crawler['pattern'], Regexp::IGNORECASE)
|
16
|
-
end || {}
|
12
|
+
@agent ||= matching_crawler || {}
|
17
13
|
end
|
18
14
|
|
19
15
|
def human?
|
@@ -40,7 +36,24 @@ module VoightKampff
|
|
40
36
|
lookup_paths.find { |path| File.exists? path }
|
41
37
|
end
|
42
38
|
|
43
|
-
def
|
39
|
+
def matching_crawler
|
40
|
+
if match = crawler_regexp.match(@user_agent_string)
|
41
|
+
index = match.names.first.sub(/match/, '').to_i
|
42
|
+
crawlers[index]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def crawler_regexp
|
47
|
+
@@crawler_regexp ||= begin
|
48
|
+
# NOTE: This is admittedly a bit convoluted but the performance gains make it worthwhile
|
49
|
+
index = -1
|
50
|
+
crawler_patterns = crawlers.map{|c| index += 1; "(?<match#{index}>#{c["pattern"]})" }.join("|")
|
51
|
+
crawler_patterns = "(#{crawler_patterns})"
|
52
|
+
Regexp.new(crawler_patterns, Regexp::IGNORECASE)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def crawlers
|
44
57
|
@@crawlers ||= JSON.load(File.open(preferred_path, 'r'))
|
45
58
|
end
|
46
59
|
end
|
@@ -25,4 +25,12 @@ describe VoightKampff::Test do
|
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
28
|
+
|
29
|
+
context 'after the first run' do
|
30
|
+
before { VoightKampff::Test.new('anything').bot? }
|
31
|
+
|
32
|
+
it 'is fast' do
|
33
|
+
expect(Benchmark.realtime { 20.times { VoightKampff::Test.new('anything').bot? } }).to be < 0.002
|
34
|
+
end
|
35
|
+
end
|
28
36
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: voight_kampff
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Crownoble
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|