From 3a64d4b9ab005cd0a221aa8e1fa442c602aff403 Mon Sep 17 00:00:00 2001 From: Ross Kaffenberger Date: Tue, 20 Mar 2012 16:57:20 -0400 Subject: [PATCH] Add extract controller logic for bypassing impression to bot? method --- app/controllers/impressionist_controller.rb | 5 +--- app/models/impressionist/bots.rb | 6 +++++ test_app/spec/models/bots_spec.rb | 27 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 test_app/spec/models/bots_spec.rb diff --git a/app/controllers/impressionist_controller.rb b/app/controllers/impressionist_controller.rb index 6f6e774..05a3976 100644 --- a/app/controllers/impressionist_controller.rb +++ b/app/controllers/impressionist_controller.rb @@ -41,10 +41,7 @@ module ImpressionistController private def bypass - Impressionist::Bots::WILD_CARDS.each do |wild_card| - return true if request.user_agent and request.user_agent.downcase.include? wild_card - end - Impressionist::Bots::LIST.include? request.user_agent + Impressionist::Bots.bot?(request.user_agent) end def unique_instance?(impressionable, unique_opts) diff --git a/app/models/impressionist/bots.rb b/app/models/impressionist/bots.rb index d0343e9..aee7cf9 100644 --- a/app/models/impressionist/bots.rb +++ b/app/models/impressionist/bots.rb @@ -1,5 +1,11 @@ module Impressionist module Bots + + def self.bot?(user_agent = nil) + return false if user_agent.nil? + WILD_CARDS.any? { |wc| user_agent.downcase.include?(wc) } || LIST.include?(user_agent) + end + WILD_CARDS = ["bot","yahoo","slurp","google","msn","crawler"] LIST = [" UnChaos From Chaos To Order Hybrid Web Search Engine.(vadim_gonchar@unchaos.com)", diff --git a/test_app/spec/models/bots_spec.rb b/test_app/spec/models/bots_spec.rb new file mode 100644 index 0000000..a298358 --- /dev/null +++ b/test_app/spec/models/bots_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe Impressionist::Bots do + + describe "self.bot?" do + it "is true if user_agent is matches wild card" do + Impressionist::Bots.bot?("google.com bot").should be_true + end + + it "is true if user_agent is on bot list" do + Impressionist::Bots.bot?("A-Online Search").should be_true + end + + it "is false if user_agent is blank" do + Impressionist::Bots.bot?("").should be_false + Impressionist::Bots.bot?(nil).should be_false + end + + it "is false if user_agent is safe" do + Impressionist::Bots.bot?('127.0.0.1').should be_false + end + + it "is false if user_agent not given" do + Impressionist::Bots.bot?.should be_false + end + end +end \ No newline at end of file