voight_kampff 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c6adf1c9f085e2e6635957d63cd8cab207f3a07
4
- data.tar.gz: 6e2949e6c89a8c2f3aafe2f07a726788a60711c2
3
+ metadata.gz: f1f725ef9bb3130917b515802108e7afa6c5f923
4
+ data.tar.gz: db35fc150b61de1541d88893316d716d1584dd9d
5
5
  SHA512:
6
- metadata.gz: e36f897ff189876f736676aca23c5fbc948562f28fd16887ca9daba33da98fc5b779d6e5a3823c9c11d0b1faa2731dc51257c6c227c0150ad9e6882e69607945
7
- data.tar.gz: cc62b19d3b66917f1d8afc91f74b0ba447d7dff859cb8e0622e30fa50632325363fb30d17dc256c9e77734a3a919d5a750b08ccbd5df040cf2c8ed2074535b0f
6
+ metadata.gz: cfcccea8835dbb3857bae599f4b15dc93d49d846b2be5b0f27fa83b5c59cbe94b412bb71feab1b50e8ec284a94399fc4316ffdc3d6aa2852ad734b54750bc609
7
+ data.tar.gz: 9ac3a9a20d1c4e88faf9d4a7575c3ade972ff27efe936c4d6cfca851f2045680f0829d6353a02029b539f01bd2fcba32613dd03f3dd77f493cf9410e9aedeafa
@@ -9,11 +9,7 @@ module VoightKampff
9
9
  end
10
10
 
11
11
  def agent
12
- load_crawlers
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 load_crawlers
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
@@ -1,3 +1,3 @@
1
1
  module VoightKampff
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
  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.1
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-02 00:00:00.000000000 Z
11
+ date: 2015-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack