analytical 0.6.0 → 0.7.0

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.7.0
data/analytical.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{analytical}
8
- s.version = "0.6.0"
8
+ s.version = "0.7.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Joshua Krall"]
12
- s.date = %q{2010-05-02}
12
+ s.date = %q{2010-05-04}
13
13
  s.description = %q{Gem for managing multiple analytics services in your rails app.}
14
14
  s.email = %q{josh@transfs.com}
15
15
  s.extra_rdoc_files = [
@@ -77,12 +77,14 @@ Gem::Specification.new do |s|
77
77
  "lib/analytical.rb",
78
78
  "lib/analytical/api.rb",
79
79
  "lib/analytical/base.rb",
80
+ "lib/analytical/bot_detector.rb",
80
81
  "lib/analytical/clicky.rb",
81
82
  "lib/analytical/console.rb",
82
83
  "lib/analytical/google.rb",
83
84
  "lib/analytical/kiss_metrics.rb",
84
85
  "rails/init.rb",
85
86
  "spec/analytical/api_spec.rb",
87
+ "spec/analytical/bot_detector_spec.rb",
86
88
  "spec/analytical/clicky_spec.rb",
87
89
  "spec/analytical/google_spec.rb",
88
90
  "spec/analytical/kiss_metrics_spec.rb",
@@ -98,6 +100,7 @@ Gem::Specification.new do |s|
98
100
  s.summary = %q{Gem for managing multiple analytics services in your rails app.}
99
101
  s.test_files = [
100
102
  "spec/analytical/api_spec.rb",
103
+ "spec/analytical/bot_detector_spec.rb",
101
104
  "spec/analytical/clicky_spec.rb",
102
105
  "spec/analytical/google_spec.rb",
103
106
  "spec/analytical/kiss_metrics_spec.rb",
data/lib/analytical.rb CHANGED
@@ -7,6 +7,7 @@ module Analytical
7
7
  # any method placed here will apply to ActionController::Base
8
8
  def analytical(options={})
9
9
  send :include, InstanceMethods
10
+ send :include, Analytical::BotDetector
10
11
  send :helper_method, :analytical
11
12
  send :cattr_accessor, :analytical_options
12
13
 
@@ -38,6 +39,9 @@ module Analytical
38
39
  if options[:disable_if].call(self)
39
40
  options[:modules] = options[:development_modules]
40
41
  end
42
+ if analytical_is_robot?(request.user_agent)
43
+ options[:modules] = []
44
+ end
41
45
  Analytical::Api.new options
42
46
  end
43
47
  end
@@ -0,0 +1,46 @@
1
+ module Analytical
2
+ module BotDetector
3
+
4
+ def analytical_is_robot?(user_agent)
5
+ unless user_agent.blank?
6
+ user_agent = user_agent.to_s.downcase
7
+
8
+ # We mark something as a bot if it contains any of the $bot_indicators
9
+ # or if it does not contain one of the $browser_indicators. In addition,
10
+ # if the user-agent string contains "mozilla" we make sure it has version
11
+ # information. Finally anything that starts with a word in the $whitelist
12
+ # is never considered a bot.
13
+
14
+ whitelist = %w(w3m dillo links elinks lynx)
15
+ whitelist.each do |word|
16
+ return false if user_agent.index(word) == 0
17
+ end
18
+
19
+ bot_indicators = %w(bot spider search jeeves crawl seek heritrix slurp thumbnails capture ferret webinator scan retriever accelerator upload digg extractor grub scrub)
20
+ bot_indicators.each do |word|
21
+ return true if user_agent.index word
22
+ end
23
+
24
+ browser_indicators = %w(mozilla browser iphone lynx mobile opera icab)
25
+ has_browser_indicator = false
26
+
27
+ browser_indicators.each do |word|
28
+ if user_agent.index word
29
+ has_browser_indicator = true
30
+ break
31
+ end
32
+ end
33
+
34
+ return true if not has_browser_indicator
35
+
36
+ # Check for mozilla version information
37
+ if user_agent.include? "mozilla"
38
+ return true if not user_agent.include? "("
39
+ return true if user_agent !~ /mozilla\/\d+/i
40
+ end
41
+ end
42
+ return false
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "Analytical::BotDetector" do
4
+ class DummyForBotDetector
5
+ include Analytical::BotDetector
6
+ end
7
+ before(:each) do
8
+ @d = DummyForBotDetector.new
9
+ end
10
+
11
+ describe 'with nil user_agent' do
12
+ it 'should return false' do
13
+ @d.analytical_is_robot?(nil).should be_false
14
+ end
15
+ end
16
+
17
+ describe 'with empty user_agent' do
18
+ it 'should return false' do
19
+ @d.analytical_is_robot?('').should be_false
20
+ end
21
+ end
22
+
23
+
24
+ end
@@ -11,8 +11,12 @@ describe "Analytical" do
11
11
  describe 'on initialization' do
12
12
  class DummyForInit
13
13
  extend Analytical
14
- def request; OpenStruct.new(:'ssl?'=>true); end
15
14
  def self.helper_method(*a); end
15
+ def request
16
+ Spec::Mocks::Mock.new 'request',
17
+ :'ssl?'=>true,
18
+ :user_agent=>'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 GTB7.0'
19
+ end
16
20
  end
17
21
 
18
22
  it 'should have the default options' do
@@ -23,6 +27,23 @@ describe "Analytical" do
23
27
  d.options[:disable_if].call.should be_false
24
28
  end
25
29
 
30
+ it 'should use the supplied options' do
31
+ DummyForInit.analytical :modules=>[:google]
32
+ d = DummyForInit.new.analytical
33
+ d.options[:modules].should == [:google]
34
+ d.options[:development_modules].should == [:console]
35
+ d.options[:disable_if].call.should be_false
36
+ end
37
+
38
+ describe 'with a robot request' do
39
+ it 'should set the modules to []' do
40
+ DummyForInit.analytical
41
+ d = DummyForInit.new
42
+ d.stub!(:'analytical_is_robot?').and_return(true)
43
+ d.analytical.options[:modules].should == []
44
+ end
45
+ end
46
+
26
47
  it 'should open the initialization file' do
27
48
  File.should_receive(:'exists?').with("#{RAILS_ROOT}/config/analytical.yml").and_return(true)
28
49
  DummyForInit.analytical
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 6
7
+ - 7
8
8
  - 0
9
- version: 0.6.0
9
+ version: 0.7.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Joshua Krall
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-02 00:00:00 -05:00
17
+ date: 2010-05-04 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -113,12 +113,14 @@ files:
113
113
  - lib/analytical.rb
114
114
  - lib/analytical/api.rb
115
115
  - lib/analytical/base.rb
116
+ - lib/analytical/bot_detector.rb
116
117
  - lib/analytical/clicky.rb
117
118
  - lib/analytical/console.rb
118
119
  - lib/analytical/google.rb
119
120
  - lib/analytical/kiss_metrics.rb
120
121
  - rails/init.rb
121
122
  - spec/analytical/api_spec.rb
123
+ - spec/analytical/bot_detector_spec.rb
122
124
  - spec/analytical/clicky_spec.rb
123
125
  - spec/analytical/google_spec.rb
124
126
  - spec/analytical/kiss_metrics_spec.rb
@@ -158,6 +160,7 @@ specification_version: 3
158
160
  summary: Gem for managing multiple analytics services in your rails app.
159
161
  test_files:
160
162
  - spec/analytical/api_spec.rb
163
+ - spec/analytical/bot_detector_spec.rb
161
164
  - spec/analytical/clicky_spec.rb
162
165
  - spec/analytical/google_spec.rb
163
166
  - spec/analytical/kiss_metrics_spec.rb