Class: Crawlr::HTTPInterface
- Inherits:
-
Object
- Object
- Crawlr::HTTPInterface
- Defined in:
- lib/crawlr/http_interface.rb
Overview
Handles fetching documents via async HTTP with proxy and cookie support.
The HTTPInterface class provides a high-level async HTTP client specifically designed for web scraping. It supports proxy rotation, cookie management, configurable timeouts, and transforms raw HTTP responses into a simplified response structure suitable for content processing.
Defined Under Namespace
Classes: Response
Instance Attribute Summary collapse
-
#config ⇒ Crawlr::Config
readonly
Configuration object containing HTTP settings.
Instance Method Summary collapse
-
#get(url) {|url, headers| ... } ⇒ HTTPInterface::Response
Performs an HTTP GET request with full async support and cookie management.
-
#initialize(config) ⇒ HTTPInterface
constructor
Initializes a new HTTPInterface with the given configuration.
Constructor Details
#initialize(config) ⇒ HTTPInterface
Initializes a new HTTPInterface with the given configuration
Sets up cookie management (if enabled) and proxy rotation state. The cookie jar persists across all requests made by this interface instance.
86 87 88 89 90 |
# File 'lib/crawlr/http_interface.rb', line 86 def initialize(config) @config = config @cookie_jars = Concurrent::Map.new if @config. @proxy_index = 0 end |
Instance Attribute Details
#config ⇒ Crawlr::Config (readonly)
Returns Configuration object containing HTTP settings.
65 66 67 |
# File 'lib/crawlr/http_interface.rb', line 65 def config @config end |
Instance Method Details
#get(url) {|url, headers| ... } ⇒ HTTPInterface::Response
Performs an HTTP GET request with full async support and cookie management
This method handles the complete HTTP request lifecycle including:
- Proxy selection and connection setup
- Cookie retrieval and attachment
- Request header customization via block
- Async execution with timeout handling
- Response cookie parsing and storage
- Resource cleanup and connection closing
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/crawlr/http_interface.rb', line 138 def get(url) # rubocop:disable Metrics/MethodLength Crawlr.logger.debug "Fetching #{url}" uri = URI.parse(url) proxy_url = next_proxy internet = build_internet_connection(proxy_url) request_headers = @config.headers.dup (uri, request_headers) yield(url, request_headers) if block_given? # Used for request customization hook raw_response = nil begin Sync do |task| raw_response = task.with_timeout(@config.timeout) do internet.get(url, request_headers) end end (uri, raw_response) if @config. && raw_response make_response_struct(url, raw_response) rescue Async::TimeoutError Crawlr.logger.warn "Timeout fetching #{url} after #{@config.timeout}sec" raise ensure raw_response&.close internet&.close Crawlr.logger.debug "Done fetching #{url}" end end |