Module: Html2rss::RequestSession::Pager

Defined in:
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

Factory for building pagination strategy instances.

Defined Under Namespace

Classes: Base, CustomSelector, JsonCursor, Offset, RelNext, UrlTemplate

Constant Summary collapse

STRATEGIES =

Mapping of strategy symbols to strategy class implementations.

Returns:

  • (Hash{Symbol => Class})
{
  rel_next: RelNext,
  custom_selector: CustomSelector,
  url_template: UrlTemplate,
  offset: Offset,
  json_cursor: JsonCursor
}.freeze

Class Method Summary collapse

Class Method Details

.for(config, session:, initial_response:) ⇒ RequestSession::Pager::Base

Returns a pager instance for the provided configuration.

Parameters:

  • config (Hash, Integer, nil)

    pagination configuration

  • session (RequestSession)

    request session used to execute follow-ups

  • initial_response (RequestService::Response)

    initial page response

Returns:



45
46
47
48
49
50
51
52
53
54
# File 'lib/html2rss/request_session/pager.rb', line 45

def self.for(config, session:, initial_response:)
  normalized_config = normalize_config(config)
  strategy_name = normalized_config.fetch(:strategy, :rel_next).to_sym

  klass = STRATEGIES.fetch(strategy_name) do
    raise ArgumentError, "Unknown pagination strategy: #{strategy_name}"
  end

  klass.new(session:, initial_response:, config: normalized_config)
end

.normalize_config(config) ⇒ Hash

Normalizes integer, hash, or nil inputs into a strategy config hash.

Parameters:

  • config (Hash, Integer, nil)

Returns:

  • (Hash)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/html2rss/request_session/pager.rb', line 61

def self.normalize_config(config)
  case config
  when Integer
    { max_pages: config, strategy: :rel_next }
  when Hash
    normalized = config.transform_keys(&:to_sym)
    normalized[:strategy] = (normalized[:strategy] || :rel_next).to_sym
    normalized
  else
    { max_pages: Base::DEFAULT_MAX_PAGES, strategy: :rel_next }
  end
end

.request_slots_for(config) ⇒ Integer

Returns the number of request slots needed for the pagination configuration.

Parameters:

  • config (Hash, Integer, nil)

    pagination configuration

Returns:

  • (Integer)

    number of follow-up requests needed



31
32
33
34
35
36
# File 'lib/html2rss/request_session/pager.rb', line 31

def self.request_slots_for(config)
  return 0 unless config

  max_pages = normalize_config(config)[:max_pages] || Base::DEFAULT_MAX_PAGES
  [max_pages - 1, 0].max
end

.strategy_namesArray<String>

Returns the supported pagination strategy names.

Returns:

  • (Array<String>)

    strategy names derived from STRATEGIES



22
23
24
# File 'lib/html2rss/request_session/pager.rb', line 22

def self.strategy_names
  STRATEGIES.keys.map(&:to_s).freeze
end