Class: BotVerification::Configuration
- Inherits:
-
Object
- Object
- BotVerification::Configuration
- Defined in:
- lib/bot_verification/configuration.rb
Instance Attribute Summary collapse
-
#cache_key_prefix ⇒ Object
Cache key prefix for Rails cache Default: "bot_verification".
-
#cache_ttl ⇒ Object
How long to cache verification results in Rails cache Default: 24 hours.
-
#dns_timeout ⇒ Object
Timeout for each DNS lookup (reverse and forward) in seconds Default: 1.0.
-
#dns_total_timeout ⇒ Object
Total timeout for all DNS operations in seconds Default: 2.0.
-
#ip_range_model_name ⇒ Object
Custom IP range model class name (optional) If set, uses your own model instead of the built-in one The model must include BotVerification::IpRangeModel.
-
#logger ⇒ Object
Logger instance (defaults to Rails.logger).
-
#on_error ⇒ Object
Error callback - called when errors occur during IP range refresh Useful for integrating with error tracking services (Airbrake, Sentry, etc.).
-
#on_refresh_complete ⇒ Object
Refresh complete callback - called after IP range refresh completes Useful for notifications or monitoring.
-
#on_verification ⇒ Object
Verification callback - called after each bot verification attempt Useful for collecting statistics about bot traffic.
-
#session_cache_ttl ⇒ Object
How long to cache results in session Default: 1 hour.
-
#skip_dns_verification ⇒ Object
Skip DNS verification entirely (only use IP range matching) Set to true if DNS lookups are unacceptable for your project Default: false.
-
#table_name ⇒ Object
Table name for storing bot IP ranges Default: "bot_ip_ranges".
-
#verify_ssl ⇒ Object
Verify SSL certificates when fetching IP ranges Set to false in development if you have certificate issues Default: true (always verify in production!).
Instance Method Summary collapse
- #cache ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#ip_range_model_class ⇒ Object
Get the IP range model class.
-
#report_error(error, context = {}) ⇒ Object
Report an error through the configured callback.
-
#report_refresh_complete(results) ⇒ Object
Report refresh completion through the configured callback.
-
#report_verification(result) ⇒ Object
Report verification attempt through the configured callback.
-
#validate! ⇒ Object
Validate configuration.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/bot_verification/configuration.rb', line 68 def initialize @table_name = "bot_ip_ranges" @skip_dns_verification = false @verify_ssl = true @dns_timeout = 1.0 @dns_total_timeout = 2.0 @cache_ttl = 24.hours @session_cache_ttl = 1.hour @cache_key_prefix = "bot_verification" @logger = nil @ip_range_model_name = nil @on_error = nil @on_refresh_complete = nil @on_verification = nil end |
Instance Attribute Details
#cache_key_prefix ⇒ Object
Cache key prefix for Rails cache Default: "bot_verification"
37 38 39 |
# File 'lib/bot_verification/configuration.rb', line 37 def cache_key_prefix @cache_key_prefix end |
#cache_ttl ⇒ Object
How long to cache verification results in Rails cache Default: 24 hours
29 30 31 |
# File 'lib/bot_verification/configuration.rb', line 29 def cache_ttl @cache_ttl end |
#dns_timeout ⇒ Object
Timeout for each DNS lookup (reverse and forward) in seconds Default: 1.0
21 22 23 |
# File 'lib/bot_verification/configuration.rb', line 21 def dns_timeout @dns_timeout end |
#dns_total_timeout ⇒ Object
Total timeout for all DNS operations in seconds Default: 2.0
25 26 27 |
# File 'lib/bot_verification/configuration.rb', line 25 def dns_total_timeout @dns_total_timeout end |
#ip_range_model_name ⇒ Object
Custom IP range model class name (optional) If set, uses your own model instead of the built-in one The model must include BotVerification::IpRangeModel
45 46 47 |
# File 'lib/bot_verification/configuration.rb', line 45 def ip_range_model_name @ip_range_model_name end |
#logger ⇒ Object
Logger instance (defaults to Rails.logger)
40 41 42 |
# File 'lib/bot_verification/configuration.rb', line 40 def logger @logger end |
#on_error ⇒ Object
Error callback - called when errors occur during IP range refresh Useful for integrating with error tracking services (Airbrake, Sentry, etc.)
51 52 53 |
# File 'lib/bot_verification/configuration.rb', line 51 def on_error @on_error end |
#on_refresh_complete ⇒ Object
Refresh complete callback - called after IP range refresh completes Useful for notifications or monitoring
57 58 59 |
# File 'lib/bot_verification/configuration.rb', line 57 def on_refresh_complete @on_refresh_complete end |
#on_verification ⇒ Object
Verification callback - called after each bot verification attempt Useful for collecting statistics about bot traffic
66 67 68 |
# File 'lib/bot_verification/configuration.rb', line 66 def on_verification @on_verification end |
#session_cache_ttl ⇒ Object
How long to cache results in session Default: 1 hour
33 34 35 |
# File 'lib/bot_verification/configuration.rb', line 33 def session_cache_ttl @session_cache_ttl end |
#skip_dns_verification ⇒ Object
Skip DNS verification entirely (only use IP range matching) Set to true if DNS lookups are unacceptable for your project Default: false
12 13 14 |
# File 'lib/bot_verification/configuration.rb', line 12 def skip_dns_verification @skip_dns_verification end |
#table_name ⇒ Object
Table name for storing bot IP ranges Default: "bot_ip_ranges"
7 8 9 |
# File 'lib/bot_verification/configuration.rb', line 7 def table_name @table_name end |
#verify_ssl ⇒ Object
Verify SSL certificates when fetching IP ranges Set to false in development if you have certificate issues Default: true (always verify in production!)
17 18 19 |
# File 'lib/bot_verification/configuration.rb', line 17 def verify_ssl @verify_ssl end |
Instance Method Details
#cache ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/bot_verification/configuration.rb', line 115 def cache @cache ||= if defined?(Rails) && Rails.respond_to?(:cache) && Rails.cache Rails.cache else ActiveSupport::Cache::MemoryStore.new end end |
#ip_range_model_class ⇒ Object
Get the IP range model class
124 125 126 127 128 129 130 |
# File 'lib/bot_verification/configuration.rb', line 124 def ip_range_model_class if @ip_range_model_name @ip_range_model_name.constantize else BotVerification::IpRange end end |
#report_error(error, context = {}) ⇒ Object
Report an error through the configured callback
85 86 87 88 89 90 91 |
# File 'lib/bot_verification/configuration.rb', line 85 def report_error(error, context = {}) return unless on_error.respond_to?(:call) on_error.call(error, context) rescue StandardError => e logger.error("[BotVerification] Error in on_error callback: #{e.message}") end |
#report_refresh_complete(results) ⇒ Object
Report refresh completion through the configured callback
94 95 96 97 98 99 100 |
# File 'lib/bot_verification/configuration.rb', line 94 def report_refresh_complete(results) return unless on_refresh_complete.respond_to?(:call) on_refresh_complete.call(results) rescue StandardError => e logger.error("[BotVerification] Error in on_refresh_complete callback: #{e.message}") end |
#report_verification(result) ⇒ Object
Report verification attempt through the configured callback
103 104 105 106 107 108 109 |
# File 'lib/bot_verification/configuration.rb', line 103 def report_verification(result) return unless on_verification.respond_to?(:call) on_verification.call(result) rescue StandardError => e logger.error("[BotVerification] Error in on_verification callback: #{e.message}") end |
#validate! ⇒ Object
Validate configuration
133 134 135 136 137 |
# File 'lib/bot_verification/configuration.rb', line 133 def validate! raise ConfigurationError, "table_name cannot be blank" if table_name.blank? raise ConfigurationError, "dns_timeout must be positive" unless dns_timeout.positive? raise ConfigurationError, "dns_total_timeout must be positive" unless dns_total_timeout.positive? end |