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:, 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.
32 33 34 35 36 37 |
# File 'lib/html2rss/request_session.rb', line 32 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:, 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.
20 21 22 23 24 25 |
# File 'lib/html2rss/request_session.rb', line 20 def build(config:, strategy:, budget:, policy:, logger: Html2rss::Log) context = RequestService::Context.new( 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.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/html2rss/request_session.rb', line 76 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.
43 44 45 |
# File 'lib/html2rss/request_session.rb', line 43 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.
67 68 69 |
# File 'lib/html2rss/request_session.rb', line 67 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.
92 93 94 |
# File 'lib/html2rss/request_session.rb', line 92 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.
54 55 56 57 58 |
# File 'lib/html2rss/request_session.rb', line 54 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.
108 109 110 |
# File 'lib/html2rss/request_session.rb', line 108 def remember!(url) visited_urls.add(normalize_url(url)) end |
#url ⇒ Html2rss::Url
Returns the session's current request URL.
114 |
# File 'lib/html2rss/request_session.rb', line 114 def url = context.url |
#visited?(url) ⇒ Boolean
Returns whether the url was already visited in this session.
99 100 101 |
# File 'lib/html2rss/request_session.rb', line 99 def visited?(url) visited_urls.include?(normalize_url(url)) end |