Class: Crawlr::Context
- Inherits:
-
Object
- Object
- Crawlr::Context
- Defined in:
- lib/crawlr/context.rb
Overview
The Context class holds metadata and shared data during a scraping session, such as URLs and crawl depth.
It acts like a small key-value store (@data) and provides
helper methods to manage depth and resolve relative URLs.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Retrieve a stored value by key.
-
#[]=(key, value) ⇒ Object
Assign a value to a key.
-
#increment_depth ⇒ Integer
Increment the crawl depth by 1.
-
#initialize(base_url: nil, page_url: nil, current_depth: 0) ⇒ Context
constructor
Create a new scraping context.
-
#resolve_url(url) ⇒ String
Resolve a relative URL using the base_url.
-
#to_h ⇒ Hash
Convert the context to a Hash.
Constructor Details
#initialize(base_url: nil, page_url: nil, current_depth: 0) ⇒ Context
Create a new scraping context.
28 29 30 31 32 33 |
# File 'lib/crawlr/context.rb', line 28 def initialize(base_url: nil, page_url: nil, current_depth: 0) @base_url = base_url @page_url = page_url @current_depth = current_depth @data = {} end |
Instance Attribute Details
#base_url ⇒ String, ...
21 22 23 |
# File 'lib/crawlr/context.rb', line 21 def base_url @base_url end |
#current_depth ⇒ String, ...
21 22 23 |
# File 'lib/crawlr/context.rb', line 21 def current_depth @current_depth end |
#page_url ⇒ String, ...
21 22 23 |
# File 'lib/crawlr/context.rb', line 21 def page_url @page_url end |
Instance Method Details
#[](key) ⇒ Object?
Retrieve a stored value by key.
39 40 41 |
# File 'lib/crawlr/context.rb', line 39 def [](key) @data[key] end |
#[]=(key, value) ⇒ Object
Assign a value to a key.
48 49 50 |
# File 'lib/crawlr/context.rb', line 48 def []=(key, value) @data[key] = value end |
#increment_depth ⇒ Integer
Increment the crawl depth by 1.
68 69 70 |
# File 'lib/crawlr/context.rb', line 68 def increment_depth @current_depth += 1 end |
#resolve_url(url) ⇒ String
Resolve a relative URL using the base_url.
76 77 78 |
# File 'lib/crawlr/context.rb', line 76 def resolve_url(url) URI.join(@base_url, url).to_s end |
#to_h ⇒ Hash
Convert the context to a Hash.
Includes base_url, page_url, current_depth, and all stored data.
57 58 59 60 61 62 63 |
# File 'lib/crawlr/context.rb', line 57 def to_h { base_url: @base_url, page_url: @page_url, current_depth: @current_depth }.merge(@data) end |