Class: Crawlr::Collector
- Inherits:
-
Object
- Object
- Crawlr::Collector
- Defined in:
- lib/crawlr/collector.rb
Overview
Main orchestrator class that manages scraping sessions.
The Collector is the central component of the Crawlr framework, responsible for:
- Managing URL visits with configurable depth control
- Handling concurrent requests with parallelism limits
- Respecting robots.txt and implementing polite crawling delays
- Executing registered callbacks on scraped content
- Maintaining visit history and domain filtering
- Providing hooks for custom behavior during scraping lifecycle
Instance Attribute Summary collapse
-
#config ⇒ Crawlr::Config
readonly
The configuration object for this collector.
- #context ⇒ Crawlr::Context, ...
- #http ⇒ Crawlr::Context, ...
- #visits ⇒ Crawlr::Context, ...
Instance Method Summary collapse
-
#clone ⇒ Crawlr::Collector
Creates a clone of the current collector with shared HTTP and visit state.
-
#hook(event, &block) {|args| ... } ⇒ void
Registers a hook for specific scraping lifecycle events.
-
#initialize(options = {}) ⇒ Collector
constructor
Initializes a new Collector instance with the given configuration.
-
#on_html(selector_type, selector, &block) {|node, ctx| ... } ⇒ void
Registers a callback for HTML content using CSS or XPath selectors.
-
#on_xml(selector_type, selector, &block) {|node, ctx| ... } ⇒ void
Registers a callback for XML content using CSS or XPath selectors.
-
#paginated_visit(url, current_depth: 0, query: "page", batch_size: 5, start_page: 1) {|collector| ... } ⇒ void
Performs paginated scraping by automatically generating page URLs.
-
#stats ⇒ Hash<Symbol, Object>
Returns comprehensive statistics about the collector's state and activity.
-
#visit(input, current_depth = 0) {|collector| ... } ⇒ void
Visits one or more URLs and processes them according to registered callbacks.
Constructor Details
#initialize(options = {}) ⇒ Collector
Initializes a new Collector instance with the given configuration
82 83 84 85 86 87 88 89 90 |
# File 'lib/crawlr/collector.rb', line 82 def initialize( = {}) @config = Crawlr::Config.new() @http = Crawlr::HTTPInterface.new(@config) @visits = Crawlr::Visits.new(@config) @domains = Crawlr::Domains.new(@config) @hooks = Crawlr::Hooks.new @callbacks = Crawlr::Callbacks.new @robots = Crawlr::Robots.new end |
Instance Attribute Details
#config ⇒ Crawlr::Config (readonly)
Returns The configuration object for this collector.
59 60 61 |
# File 'lib/crawlr/collector.rb', line 59 def config @config end |
#context ⇒ Crawlr::Context, ...
64 65 66 |
# File 'lib/crawlr/collector.rb', line 64 def context @context end |
#visits ⇒ Crawlr::Context, ...
64 65 66 |
# File 'lib/crawlr/collector.rb', line 64 def visits @visits end |
Instance Method Details
#clone ⇒ Crawlr::Collector
Creates a clone of the current collector with shared HTTP and visit state
This is useful for creating multiple collectors that share the same HTTP connection pool and visit history while having independent callback and hook configurations.
267 268 269 270 271 272 273 |
# File 'lib/crawlr/collector.rb', line 267 def clone new_collector = self.class.new(@config.to_h) new_collector.http = @http new_collector.visits = @visits new_collector end |
#hook(event, &block) {|args| ... } ⇒ void
This method returns an undefined value.
Registers a hook for specific scraping lifecycle events
Hooks allow you to execute custom code at specific points during the scraping process, such as before/after visits or on errors.
248 249 250 |
# File 'lib/crawlr/collector.rb', line 248 def hook(event, &block) @hooks.register(event, &block) end |
#on_html(selector_type, selector, &block) {|node, ctx| ... } ⇒ void
This method returns an undefined value.
Registers a callback for HTML content using CSS or XPath selectors
111 112 113 |
# File 'lib/crawlr/collector.rb', line 111 def on_html(selector_type, selector, &block) @callbacks.register(:html, selector_type, selector, &block) end |
#on_xml(selector_type, selector, &block) {|node, ctx| ... } ⇒ void
This method returns an undefined value.
Registers a callback for XML content using CSS or XPath selectors
133 134 135 |
# File 'lib/crawlr/collector.rb', line 133 def on_xml(selector_type, selector, &block) @callbacks.register(:xml, selector_type, selector, &block) end |
#paginated_visit(url, current_depth: 0, query: "page", batch_size: 5, start_page: 1) {|collector| ... } ⇒ void
This method returns an undefined value.
Performs paginated scraping by automatically generating page URLs
This method is specifically designed for APIs or websites that use query parameter pagination (e.g., ?page=1, ?page=2, etc.). It automatically generates URLs and stops when pages return 404 or too many failures occur.
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/crawlr/collector.rb', line 212 def paginated_visit(url, current_depth: 0, query: "page", batch_size: 5, start_page: 1) return unless valid_url?(url) yield self if block_given? fetch_robots_txt(url) unless @config.ignore_robots_txt return unless can_visit?(url, @config.headers) pages_to_visit = build_initial_pages(url, query, batch_size, start_page) process_page_batches(pages_to_visit, current_depth, batch_size, query) end |
#stats ⇒ Hash<Symbol, Object>
Returns comprehensive statistics about the collector's state and activity
Provides metrics about configuration, registered hooks/callbacks, visit history, and retry behavior for monitoring and debugging.
295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/crawlr/collector.rb', line 295 def stats base = { max_depth: @config.max_depth, allow_url_revisit: @config.allow_url_revisit } base.merge!(@hooks.stats) base.merge!(@callbacks.stats) base.merge!(@visits.stats) base.merge!(retry_stats) if @config.max_retries base end |
#visit(input, current_depth = 0) {|collector| ... } ⇒ void
This method returns an undefined value.
Visits one or more URLs and processes them according to registered callbacks
This method handles the core scraping workflow including:
- robots.txt checking (unless disabled)
- URL validation and filtering
- Concurrent processing with parallelism limits
- Depth tracking and limits
- Error handling and retry logic
167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/crawlr/collector.rb', line 167 def visit(input, current_depth = 0) yield self if block_given? urls = normalize_urls(input) return if exceeded_max_depth?(urls, current_depth) process_robots(urls) unless @config.ignore_robots_txt urls = filter_urls(urls) return if urls.empty? perform_visits(urls, current_depth) end |