Class: Html2rss::RequestSession

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(context:, strategy:, logger: Html2rss::Log) ⇒ RequestSession

Returns a new instance of RequestSession.

Parameters:

  • context (RequestService::Context)

    initial request context

  • strategy (Symbol)

    request strategy to use for all requests in the session

  • logger (Logger) (defaults to: Html2rss::Log)

    logger used for operational warnings



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.

Parameters:

  • config (Html2rss::Config)

    validated feed config

  • strategy (Symbol)

    request strategy for the session

  • budget (RequestService::Budget)

    shared request budget

  • policy (RequestService::Policy)

    request policy (from FeedPipeline::RuntimePolicy.resources_for)

  • logger (Logger) (defaults to: Html2rss::Log)

    logger used for operational warnings

Returns:



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.

Parameters:

  • requested_pages (Integer)

    configured page budget

Returns:

  • (Integer)

    effective page budget for the session



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_responseRequestService::Response

Executes the initial request for the session.

Returns:



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.

Parameters:

  • url (String, Html2rss::Url)

    follow-up request url

  • relation (Symbol)

    why the follow-up is being made

  • origin_url (String, Html2rss::Url)

    effective origin for same-origin checks

Returns:



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_requestsInteger

Returns the configured request budget for the session.

Returns:

  • (Integer)

    maximum requests allowed for the feed build



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.

Parameters:

  • initial_response (RequestService::Response)

    initial page response

  • pagination_config (Hash, Integer, nil) (defaults to: nil)

    pagination configuration

Returns:



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.

Parameters:

  • url (String, Html2rss::Url)

    URL used to update relation tracking state

Returns:



108
109
110
# File 'lib/html2rss/request_session.rb', line 108

def remember!(url)
  visited_urls.add(normalize_url(url))
end

#visited?(url) ⇒ Boolean

Returns whether the url was already visited in this session.

Parameters:

  • url (String, Html2rss::Url)

    follow-up target URL for the request

Returns:

  • (Boolean)

    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