Class: Crawlr::Hooks
- Inherits:
-
Object
- Object
- Crawlr::Hooks
- Defined in:
- lib/crawlr/hooks.rb
Overview
Event hook management system for scraping lifecycle customization.
The Hooks class provides a flexible event-driven system that allows users to register custom behavior at specific points during the scraping process. It supports multiple hooks per event and validates event names to ensure consistency across the framework.
Constant Summary collapse
- ALLOWED_EVENTS =
Supported lifecycle events for hook registration
:before_visit- Triggered before making HTTP request:after_visit- Triggered after receiving HTTP response:on_error- Triggered when an error occurs during scraping
%i[before_visit after_visit on_error].freeze
Instance Method Summary collapse
-
#clear(event = nil) ⇒ void
Clears registered hooks for all events or a specific event.
-
#initialize ⇒ Hooks
constructor
Initializes a new Hooks instance.
-
#register(event, &block) {|args| ... } ⇒ void
Registers a hook for a specific scraping lifecycle event.
-
#stats ⇒ Hash<Symbol, Object>
Returns statistics about registered hooks.
-
#trigger(event, *args) ⇒ void
Triggers all registered hooks for a specific event.
Constructor Details
#initialize ⇒ Hooks
Initializes a new Hooks instance
Creates an empty hook registry with auto-vivifying arrays for each event type.
51 52 53 |
# File 'lib/crawlr/hooks.rb', line 51 def initialize @hooks = Hash.new { |h, k| h[k] = [] } end |
Instance Method Details
#clear(event = nil) ⇒ void
This method returns an undefined value.
Clears registered hooks for all events or a specific event
Useful for testing, resetting hook configuration, or dynamically changing hook behavior during scraping sessions.
153 154 155 156 157 158 159 |
# File 'lib/crawlr/hooks.rb', line 153 def clear(event = nil) if event @hooks[event].clear else @hooks.clear end end |
#register(event, &block) {|args| ... } ⇒ void
This method returns an undefined value.
Registers a hook for a specific scraping lifecycle event
Hooks are executed in the order they were registered. Multiple hooks can be registered for the same event, and all will be executed when the event is triggered.
86 87 88 89 90 91 |
# File 'lib/crawlr/hooks.rb', line 86 def register(event, &block) raise ArgumentError, "Invalid event #{event}" unless ALLOWED_EVENTS.include?(event) raise ArgumentError, "Block required" unless block @hooks[event] << block end |
#stats ⇒ Hash<Symbol, Object>
Returns statistics about registered hooks
Provides metrics about hook registration for monitoring, debugging, and ensuring expected hooks are properly configured.
132 133 134 135 |
# File 'lib/crawlr/hooks.rb', line 132 def stats grouped = @hooks.transform_values(&:size) { total_hooks: @hooks.values.flatten.size, per_event: grouped } end |
#trigger(event, *args) ⇒ void
This method returns an undefined value.
Triggers all registered hooks for a specific event
Executes hooks in the order they were registered. If any hook raises an exception, it will be propagated and may prevent subsequent hooks from executing.
112 113 114 115 116 |
# File 'lib/crawlr/hooks.rb', line 112 def trigger(event, *args) raise ArgumentError, "Invalid event #{event}" unless ALLOWED_EVENTS.include?(event) @hooks[event].each { |blk| blk.call(*args) } end |