Class: Crawlr::Robots
- Inherits:
-
Object
- Object
- Crawlr::Robots
- Defined in:
- lib/crawlr/robots.rb
Overview
Robots.txt parser and compliance checker for respectful web scraping.
The Robots class implements full robots.txt specification compliance, including user-agent matching, path pattern matching with wildcards, allow/disallow precedence rules, and crawl-delay directives. It helps ensure that scrapers respect website crawling policies and avoid making unwanted requests.
Defined Under Namespace
Classes: Rule
Instance Attribute Summary collapse
-
#store ⇒ Hash<String, Array<Rule>>
readonly
Internal store of parsed robots.txt rules by domain.
Instance Method Summary collapse
-
#allowed?(url, user_agent) ⇒ Boolean
Determines if a URL is allowed to be crawled according to robots.txt rules.
-
#exists?(origin) ⇒ Boolean
Checks if robots.txt has been parsed and cached for a given origin.
-
#initialize ⇒ Robots
constructor
Initializes a new Robots instance.
-
#parse(url, content) ⇒ void
Parses robots.txt content and stores rules for the given URL's domain.
Constructor Details
#initialize ⇒ Robots
Initializes a new Robots instance
Creates an empty store for caching parsed robots.txt files by domain. Each domain's robots.txt is parsed once and cached for subsequent permission checks.
86 87 88 |
# File 'lib/crawlr/robots.rb', line 86 def initialize @store = {} end |
Instance Attribute Details
#store ⇒ Hash<String, Array<Rule>> (readonly)
Returns Internal store of parsed robots.txt rules by domain.
76 77 78 |
# File 'lib/crawlr/robots.rb', line 76 def store @store end |
Instance Method Details
#allowed?(url, user_agent) ⇒ Boolean
Determines if a URL is allowed to be crawled according to robots.txt rules
This method implements the full robots.txt specification including:
- User-agent matching with prefix matching and wildcards
- Path pattern matching with wildcards and end anchors
- Allow/disallow precedence with longest match wins
- Graceful fallback when no robots.txt exists
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/crawlr/robots.rb', line 131 def allowed?(url, user_agent) rule = get_rule(url, user_agent) return true unless rule path = URI.parse(url).path matched = matched_rules(rule, path) return true if matched.empty? longest_match_allows?(matched) end |
#exists?(origin) ⇒ Boolean
Checks if robots.txt has been parsed and cached for a given origin
99 100 101 |
# File 'lib/crawlr/robots.rb', line 99 def exists?(origin) @store.key?(origin) end |
#parse(url, content) ⇒ void
This method returns an undefined value.
Parses robots.txt content and stores rules for the given URL's domain
Extracts and processes all robots.txt directives including:
- User-agent declarations
- Allow and Disallow rules
- Crawl-delay directives
- Sitemap declarations
- Comment and empty line handling
180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/crawlr/robots.rb', line 180 def parse(url, content) uri = URI.parse(url) domain = uri.host.downcase hash = parse_to_hash(content) rules = [] hash[:rules].each do |user_agent, rule| rules << Rule.new(user_agent, rule[:allow], rule[:disallow], rule[:crawl_delay]) end @store[domain] ||= rules end |