Class: Crawlr::Callbacks

Inherits:
Object
  • Object
show all
Defined in:
lib/crawlr/callbacks.rb

Overview

Manages callback registration and execution for document scraping operations.

The Callbacks class provides a centralized way to register and manage callbacks that process specific nodes in HTML or XML documents using CSS or XPath selectors.

Examples:

Basic usage

callbacks = Crawlr::Callbacks.new
callbacks.register(:html, :css, '.title') do |node, context|
  puts node.text
end

Using XPath selectors

callbacks.register(:xml, :xpath, '//item[@id]') do |node, context|
  process_item(node, context)
end

Since:

  • 0.1.0

Constant Summary collapse

ALLOWED_FORMATS =

Supported document formats for scraping

Returns:

  • (Array<Symbol>)

    Array of allowed format symbols

Since:

  • 0.1.0

%i[html xml].freeze
ALLOWED_SELECTOR_TYPES =

Supported selector types for element selection

Returns:

  • (Array<Symbol>)

    Array of allowed selector type symbols

Since:

  • 0.1.0

%i[css xpath].freeze

Instance Method Summary collapse

Constructor Details

#initializeCallbacks

Initializes a new Callbacks instance

Examples:

callbacks = Crawlr::Callbacks.new

Since:

  • 0.1.0



35
36
37
# File 'lib/crawlr/callbacks.rb', line 35

def initialize
  @callbacks = []
end

Instance Method Details

#allArray<Hash>

Returns a copy of all registered callbacks

Examples:

callbacks = instance.all
puts callbacks.length #=> 3

Returns:

  • (Array<Hash>)

    Array of callback hashes containing format, selector_type, selector, and block

Since:

  • 0.1.0



45
46
47
# File 'lib/crawlr/callbacks.rb', line 45

def all
  @callbacks.dup
end

#clearArray

Clears all registered callbacks

Examples:

instance.clear
puts instance.stats[:callbacks_count] #=> 0

Returns:

  • (Array)

    Empty callbacks array

Since:

  • 0.1.0



95
96
97
# File 'lib/crawlr/callbacks.rb', line 95

def clear
  @callbacks.clear
end

#register(format, selector_type, selector, &block) {|node, ctx| ... } ⇒ void

This method returns an undefined value.

Registers a new callback for processing matching nodes

Examples:

Register a CSS selector callback

register(:html, :css, '.product-title') do |node, ctx|
  ctx.titles << node.text.strip
end

Register an XPath selector callback

register(:xml, :xpath, '//item[@price > 100]') do |node, ctx|
  ctx.expensive_items << parse_item(node)
end

Parameters:

  • format (Symbol)

    The document format (:html or :xml)

  • selector_type (Symbol)

    The selector type (:css or :xpath)

  • selector (String)

    The selector string to match elements

  • block (Proc)

    The callback block to execute when elements match

Yield Parameters:

  • node (Object)

    The matched DOM node

  • ctx (Object)

    The scraping context object

Raises:

  • (ArgumentError)

    When format or selector_type is not supported

Since:

  • 0.1.0



69
70
71
72
73
74
75
76
77
# File 'lib/crawlr/callbacks.rb', line 69

def register(format, selector_type, selector, &block)
  validate_registration(format, selector_type)
  @callbacks << {
    format: format,
    selector_type: selector_type,
    selector: selector,
    block: ->(node, ctx) { block.call(node, ctx) }
  }
end

#statsHash<Symbol, Integer>

Returns basic statistics about registered callbacks

Examples:

stats = instance.stats
puts stats[:callbacks_count] #=> 5

Returns:

  • (Hash<Symbol, Integer>)

    Hash containing callback statistics

Since:

  • 0.1.0



85
86
87
# File 'lib/crawlr/callbacks.rb', line 85

def stats
  { callbacks_count: @callbacks.size }
end