Module: BotVerification::ControllerConcern

Extended by:
ActiveSupport::Concern
Defined in:
lib/bot_verification/controller_concern.rb

Overview

Controller concern for bot verification with session-based caching

Examples:

Usage in a controller

class MyController < ApplicationController
  include BotVerification::ControllerConcern

  def show
    if verified_good_bot?
      # Serve full content to verified bots
    else
      # Rate limit or require authentication
    end
  end
end

Instance Method Summary collapse

Instance Method Details

#ai_bot_user_agent?Boolean

Check if user agent looks like an AI bot

Returns:



75
76
77
# File 'lib/bot_verification/controller_concern.rb', line 75

def ai_bot_user_agent?
  Service.ai_bot_user_agent?(request.user_agent)
end

#bot_user_agent?Boolean

Check if user agent looks like a bot (regardless of verification)

Returns:



68
69
70
# File 'lib/bot_verification/controller_concern.rb', line 68

def bot_user_agent?
  detected_bot_type.present?
end

#detected_bot_typeSymbol?

Get the detected bot type for the current request (if any)

Returns:



61
62
63
# File 'lib/bot_verification/controller_concern.rb', line 61

def detected_bot_type
  @_detected_bot_type ||= Service.detect_bot_type(request.user_agent)
end

#search_bot_user_agent?Boolean

Check if user agent looks like a search engine bot

Returns:



82
83
84
# File 'lib/bot_verification/controller_concern.rb', line 82

def search_bot_user_agent?
  Service.search_bot_user_agent?(request.user_agent)
end

#verified_ai_bot?(strict: true) ⇒ Boolean

Check specifically if request is from a verified AI bot

Parameters:

  • (defaults to: true)

    See Service#verified_ai_bot?

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bot_verification/controller_concern.rb', line 38

def verified_ai_bot?(strict: true)
  cache_key = bot_session_cache_key
  return false unless cache_key

  cached = read_bot_session_cache(cache_key)
  return true if cached && cached[:ai_verified] == true

  result = Service.verified_ai_bot?(
    request.remote_ip,
    request.user_agent,
    strict: strict
  )

  if result
    write_bot_session_cache(cache_key, ai_verified: true, bot_type: detected_bot_type)
  end

  result
end

#verified_good_bot?(mode: :search_engines) ⇒ Boolean

Check if current request is from a verified good bot Uses session caching to avoid repeated verification calls

Parameters:

  • (defaults to: :search_engines)

    Verification mode (see Service)

Returns:



27
28
29
30
31
32
# File 'lib/bot_verification/controller_concern.rb', line 27

def verified_good_bot?(mode: :search_engines)
  return @_verified_good_bot if defined?(@_verified_good_bot) && @_verification_mode == mode

  @_verification_mode = mode
  @_verified_good_bot = check_bot_with_session_cache(mode: mode)
end