diff --git a/lib/legitbot/apple.rb b/lib/legitbot/apple.rb index c29809f..6c55723 100644 --- a/lib/legitbot/apple.rb +++ b/lib/legitbot/apple.rb @@ -12,5 +12,9 @@ def valid? end end + class Apple_as_Google < Apple + end + rule Legitbot::Apple, %w(Applebot) + rule Legitbot::Apple_as_Google, %w(Googlebot) end diff --git a/lib/legitbot/legitbot.rb b/lib/legitbot/legitbot.rb index a9664b7..90d2a19 100644 --- a/lib/legitbot/legitbot.rb +++ b/lib/legitbot/legitbot.rb @@ -11,12 +11,15 @@ module Legitbot # :yields: a found bot # def self.bot(userAgent, ip, resolver_config = nil) - bot = + bots = @rules.select { |rule| rule[:fragments].any? {|f| userAgent.index f} }.map { |rule| rule[:class].new(ip, resolver_config) - }.first + } + + bot = bots.select { |bot| bot.valid? }.first if bots.size > 1 + bot = bots.first if bot.nil? if bot && block_given? yield bot diff --git a/test/apple_as_google_test.rb b/test/apple_as_google_test.rb new file mode 100644 index 0000000..30f7f1b --- /dev/null +++ b/test/apple_as_google_test.rb @@ -0,0 +1,22 @@ +require 'minitest/autorun' +require 'legitbot' + +class AppleAsGoogleTest < Minitest::Test + def test_valid_ip + ip = "17.58.98.60" + match = Legitbot::Apple_as_Google.new(ip) + assert match.valid?, msg: "#{ip} is a valid Applebot IP" + end + + def test_invalid_ip + ip = "127.0.0.1" + match = Legitbot::Apple_as_Google.new(ip) + assert match.fake?, msg: "#{ip} is a fake Applebot IP" + end + + def test_user_agent + bot = Legitbot.bot("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)", "17.58.98.60") + assert_equal :apple_as_google, bot.detected_as + assert bot.valid?, msg: "A valid Applebot User-agent and IP" + end +end