Class: Crawlr::Domains
- Inherits:
-
Object
- Object
- Crawlr::Domains
- Defined in:
- lib/crawlr/domains.rb
Overview
Domain filtering and validation class for controlling scraping scope.
The Domains class manages which domains are allowed to be scraped by implementing both explicit domain allowlists and glob pattern matching. It provides flexible domain filtering to restrict scraping to specific sites or domain patterns while normalizing domain names for consistent comparison.
Instance Method Summary collapse
-
#allowed?(url) ⇒ Boolean
Checks if a URL is allowed based on configured domain restrictions.
-
#domain_stats ⇒ Hash<Symbol, Integer>
Returns statistics about the configured domain restrictions.
-
#initialize(config) ⇒ Domains
constructor
Initializes a new Domains instance with the given configuration.
Constructor Details
#initialize(config) ⇒ Domains
Initializes a new Domains instance with the given configuration
47 48 49 50 51 |
# File 'lib/crawlr/domains.rb', line 47 def initialize(config) @config = config @allowed_domains = extract_allowed_domains(@config.allowed_domains) @domain_glob = @config.domain_glob end |
Instance Method Details
#allowed?(url) ⇒ Boolean
Checks if a URL is allowed based on configured domain restrictions
The method performs the following checks in order:
- If no restrictions are configured, allows all URLs
- If glob patterns are configured, tests URL against each pattern
- If explicit domains are configured, checks normalized domain name
- Logs rejection for debugging purposes
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/crawlr/domains.rb', line 78 def allowed?(url) return true if @allowed_domains.empty? && @domain_glob.empty? return true if !@domain_glob.empty? && matches_domain_glob?(url) uri = URI(url) base_name = uri.host.sub("www.", "") allowed = @allowed_domains.include?(base_name) Crawlr.logger.info("URL not allowed: #{url}") unless allowed allowed end |
#domain_stats ⇒ Hash<Symbol, Integer>
Returns statistics about the configured domain restrictions
Provides metrics about the number of explicitly allowed domains and glob patterns configured for monitoring and debugging purposes.
103 104 105 106 107 108 |
# File 'lib/crawlr/domains.rb', line 103 def domain_stats { allowed_domains: @allowed_domains.size, domain_glob: @domain_glob.size } end |