Class: Html2rss::RequestSession
- Inherits:
-
Object
- Object
- Html2rss::RequestSession
- Defined in:
- lib/html2rss/request_session.rb,
lib/html2rss/request_session/pager.rb,
lib/html2rss/request_session/pager/base.rb,
lib/html2rss/request_session/pager/offset.rb,
lib/html2rss/request_session/pager/rel_next.rb,
lib/html2rss/request_session/pager/json_cursor.rb,
lib/html2rss/request_session/pager/url_template.rb,
lib/html2rss/request_session/pager/custom_selector.rb
Overview
Coordinates multi-request feed builds on top of RequestService.
Defined Under Namespace
Modules: Pager
Class Method Summary collapse
-
.build(config:, strategy:, budget:, policy:, scrape_url: nil, logger: Html2rss::Log) ⇒ RequestSession
Builds a request session from config, strategy, and shared budget/policy.
Instance Method Summary collapse
-
#effective_page_budget(requested_pages) ⇒ Integer
Returns the effective page budget after applying the policy ceiling.
-
#fetch_initial_response ⇒ RequestService::Response
Executes the initial request for the session.
-
#follow_up(url:, relation:, origin_url:) ⇒ RequestService::Response
Executes a follow-up request sharing policy, headers, and budget.
-
#initialize(context:, strategy:, logger: Html2rss::Log) ⇒ RequestSession
constructor
A new instance of RequestSession.
-
#max_requests ⇒ Integer
Returns the configured request budget for the session.
-
#page_responses(initial_response, pagination_config: nil) ⇒ Enumerable<RequestService::Response>
Returns an enumerable sequence of page responses for the given initial response and optional pagination configuration.
-
#remember!(url) ⇒ Set<Html2rss::Url>
Records a visited url in the session.
-
#url ⇒ Html2rss::Url
The session's current request URL.
-
#visited?(url) ⇒ Boolean
Whether the url was already visited in this session.
Constructor Details
#initialize(context:, strategy:, logger: Html2rss::Log) ⇒ RequestSession
Returns a new instance of RequestSession.
35 36 37 38 39 40 |
# File 'lib/html2rss/request_session.rb', line 35 def initialize(context:, strategy:, logger: Html2rss::Log) @context = context @strategy = strategy @logger = logger @visited_urls = Set.new end |
Class Method Details
.build(config:, strategy:, budget:, policy:, scrape_url: nil, logger: Html2rss::Log) ⇒ RequestSession
Builds a request session from config, strategy, and shared budget/policy.
Context owns URL/header/request normalization once; callers must not pre-normalize through a passthrough bag.
rubocop:disable Metrics/ParameterLists -- scrape_url override stays beside config
22 23 24 25 26 27 |
# File 'lib/html2rss/request_session.rb', line 22 def build(config:, strategy:, budget:, policy:, scrape_url: nil, logger: Html2rss::Log) context = RequestService::Context.new( url: scrape_url || config.url, headers: config.headers, request: config.request, policy:, budget: ) new(context:, strategy:, logger:) end |
Instance Method Details
#effective_page_budget(requested_pages) ⇒ Integer
Returns the effective page budget after applying the policy ceiling.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/html2rss/request_session.rb', line 79 def effective_page_budget(requested_pages) effective_pages = [requested_pages, context.policy.max_requests].min return effective_pages if effective_pages == requested_pages logger.warn( "#{self.class}: pagination max_pages=#{requested_pages} " \ "exceeds system ceiling=#{context.policy.max_requests}; " \ "clamping to #{effective_pages}" ) effective_pages end |
#fetch_initial_response ⇒ RequestService::Response
Executes the initial request for the session.
46 47 48 |
# File 'lib/html2rss/request_session.rb', line 46 def fetch_initial_response execute(context).tap { |response| remember!(response.url) } end |
#follow_up(url:, relation:, origin_url:) ⇒ RequestService::Response
Executes a follow-up request sharing policy, headers, and budget.
70 71 72 |
# File 'lib/html2rss/request_session.rb', line 70 def follow_up(url:, relation:, origin_url:) execute(context.follow_up(url:, relation:, origin_url:)).tap { |response| remember!(response.url) } end |
#max_requests ⇒ Integer
Returns the configured request budget for the session.
95 96 97 |
# File 'lib/html2rss/request_session.rb', line 95 def max_requests context.policy.max_requests end |
#page_responses(initial_response, pagination_config: nil) ⇒ Enumerable<RequestService::Response>
Returns an enumerable sequence of page responses for the given initial response and optional pagination configuration.
57 58 59 60 61 |
# File 'lib/html2rss/request_session.rb', line 57 def page_responses(initial_response, pagination_config: nil) return [initial_response] unless pagination_config Pager.for(pagination_config, session: self, initial_response:) end |
#remember!(url) ⇒ Set<Html2rss::Url>
Records a visited url in the session.
111 112 113 |
# File 'lib/html2rss/request_session.rb', line 111 def remember!(url) visited_urls.add(normalize_url(url)) end |
#url ⇒ Html2rss::Url
Returns the session's current request URL.
117 |
# File 'lib/html2rss/request_session.rb', line 117 def url = context.url |
#visited?(url) ⇒ Boolean
Returns whether the url was already visited in this session.
102 103 104 |
# File 'lib/html2rss/request_session.rb', line 102 def visited?(url) visited_urls.include?(normalize_url(url)) end |