Commit 84503cc0710eea00c351c26dd3b382bd4d88c7dc
1 parent
ff59cf7f
Exists in
master
and in
23 other branches
view-page: do not count hits on bot access
(ActionItem3063)
Showing
2 changed files
with
23 additions
and
1 deletions
Show diff stats
app/controllers/public/content_viewer_controller.rb
| @@ -61,7 +61,7 @@ class ContentViewerController < ApplicationController | @@ -61,7 +61,7 @@ class ContentViewerController < ApplicationController | ||
| 61 | end | 61 | end |
| 62 | 62 | ||
| 63 | # At this point the page will be showed | 63 | # At this point the page will be showed |
| 64 | - @page.hit | 64 | + @page.hit unless user_is_a_bot? |
| 65 | 65 | ||
| 66 | @page = FilePresenter.for @page | 66 | @page = FilePresenter.for @page |
| 67 | 67 | ||
| @@ -158,4 +158,11 @@ class ContentViewerController < ApplicationController | @@ -158,4 +158,11 @@ class ContentViewerController < ApplicationController | ||
| 158 | end | 158 | end |
| 159 | helper_method :pass_without_comment_captcha? | 159 | helper_method :pass_without_comment_captcha? |
| 160 | 160 | ||
| 161 | + def user_is_a_bot? | ||
| 162 | + request.env["HTTP_USER_AGENT"].match(/bot/) || | ||
| 163 | + request.env["HTTP_USER_AGENT"].match(/spider/) || | ||
| 164 | + request.env["HTTP_USER_AGENT"].match(/crawler/) || | ||
| 165 | + request.env["HTTP_USER_AGENT"].match(/\(.*https?:\/\/.*\)/) | ||
| 166 | + end | ||
| 167 | + | ||
| 161 | end | 168 | end |
test/functional/content_viewer_controller_test.rb
| @@ -1303,4 +1303,19 @@ class ContentViewerControllerTest < ActionController::TestCase | @@ -1303,4 +1303,19 @@ class ContentViewerControllerTest < ActionController::TestCase | ||
| 1303 | assert_match /this is a sample text file/, @response.body | 1303 | assert_match /this is a sample text file/, @response.body |
| 1304 | end | 1304 | end |
| 1305 | 1305 | ||
| 1306 | + should 'not count hit from bots' do | ||
| 1307 | + article = fast_create(Article, :profile_id => profile.id) | ||
| 1308 | + assert_no_difference article, :hits do | ||
| 1309 | + @request.env['HTTP_USER_AGENT'] = 'bot' | ||
| 1310 | + get 'view_page', :profile => profile.identifier, :page => article.path.split('/') | ||
| 1311 | + @request.env['HTTP_USER_AGENT'] = 'spider' | ||
| 1312 | + get 'view_page', :profile => profile.identifier, :page => article.path.split('/') | ||
| 1313 | + @request.env['HTTP_USER_AGENT'] = 'crawler' | ||
| 1314 | + get 'view_page', :profile => profile.identifier, :page => article.path.split('/') | ||
| 1315 | + @request.env['HTTP_USER_AGENT'] = '(http://some-crawler.com)' | ||
| 1316 | + get 'view_page', :profile => profile.identifier, :page => article.path.split('/') | ||
| 1317 | + article.reload | ||
| 1318 | + end | ||
| 1319 | + end | ||
| 1320 | + | ||
| 1306 | end | 1321 | end |