Commit 84503cc0710eea00c351c26dd3b382bd4d88c7dc

Authored by Rodrigo Souto
1 parent ff59cf7f

view-page: do not count hits on bot access

(ActionItem3063)
app/controllers/public/content_viewer_controller.rb
... ... @@ -61,7 +61,7 @@ class ContentViewerController < ApplicationController
61 61 end
62 62  
63 63 # At this point the page will be showed
64   - @page.hit
  64 + @page.hit unless user_is_a_bot?
65 65  
66 66 @page = FilePresenter.for @page
67 67  
... ... @@ -158,4 +158,11 @@ class ContentViewerController < ApplicationController
158 158 end
159 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 168 end
... ...
test/functional/content_viewer_controller_test.rb
... ... @@ -1303,4 +1303,19 @@ class ContentViewerControllerTest < ActionController::TestCase
1303 1303 assert_match /this is a sample text file/, @response.body
1304 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 1321 end
... ...