From 84503cc0710eea00c351c26dd3b382bd4d88c7dc Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Wed, 26 Mar 2014 19:00:38 +0000 Subject: [PATCH] view-page: do not count hits on bot access --- app/controllers/public/content_viewer_controller.rb | 9 ++++++++- test/functional/content_viewer_controller_test.rb | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/controllers/public/content_viewer_controller.rb b/app/controllers/public/content_viewer_controller.rb index d845e77..d8eb96e 100644 --- a/app/controllers/public/content_viewer_controller.rb +++ b/app/controllers/public/content_viewer_controller.rb @@ -61,7 +61,7 @@ class ContentViewerController < ApplicationController end # At this point the page will be showed - @page.hit + @page.hit unless user_is_a_bot? @page = FilePresenter.for @page @@ -158,4 +158,11 @@ class ContentViewerController < ApplicationController end helper_method :pass_without_comment_captcha? + def user_is_a_bot? + request.env["HTTP_USER_AGENT"].match(/bot/) || + request.env["HTTP_USER_AGENT"].match(/spider/) || + request.env["HTTP_USER_AGENT"].match(/crawler/) || + request.env["HTTP_USER_AGENT"].match(/\(.*https?:\/\/.*\)/) + end + end diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index 5306fa3..9c767b5 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -1303,4 +1303,19 @@ class ContentViewerControllerTest < ActionController::TestCase assert_match /this is a sample text file/, @response.body end + should 'not count hit from bots' do + article = fast_create(Article, :profile_id => profile.id) + assert_no_difference article, :hits do + @request.env['HTTP_USER_AGENT'] = 'bot' + get 'view_page', :profile => profile.identifier, :page => article.path.split('/') + @request.env['HTTP_USER_AGENT'] = 'spider' + get 'view_page', :profile => profile.identifier, :page => article.path.split('/') + @request.env['HTTP_USER_AGENT'] = 'crawler' + get 'view_page', :profile => profile.identifier, :page => article.path.split('/') + @request.env['HTTP_USER_AGENT'] = '(http://some-crawler.com)' + get 'view_page', :profile => profile.identifier, :page => article.path.split('/') + article.reload + end + end + end -- libgit2 0.21.2