Module: Html2rss::Config::ClassMethods

Included in:
Html2rss::Config
Defined in:
lib/html2rss/config/class_methods.rb

Overview

Public class-level helpers for loading, validating, and exporting config.

Constant Summary collapse

UNSET =

Sentinel to differentiate omitted params from explicit ‘nil`.

Object.new.freeze

Instance Method Summary collapse

Instance Method Details

#auto_source_config(url:, items_selector: nil, request_controls: nil) ⇒ Hash{Symbol => Object}

Builds a top-level auto-source feed config for the public shortcut APIs.

Parameters:

  • url (String)

    source page URL

  • items_selector (String, nil) (defaults to: nil)

    optional selector hint for item extraction

  • request_controls (Html2rss::RequestControls, nil) (defaults to: nil)

    explicit request controls to write

Returns:

  • (Hash{Symbol => Object})

    feed config hash ready for #from_hash



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/html2rss/config/class_methods.rb', line 115

def auto_source_config(url:, items_selector: nil, request_controls: nil)
  config = {
    channel: default_config[:channel].merge(url:),
    auto_source: AutoSource::DEFAULT_CONFIG
  }

  request_controls ||= Html2rss::RequestControls.new
  request_controls.apply_to(config)

  config[:selectors] = { items: { selector: items_selector, enhance: true } } if items_selector
  config
end

#default_configHash{Symbol => Object}

Provides a default configuration.

Returns:

  • (Hash{Symbol => Object})

    a hash with default configuration values.



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/html2rss/config/class_methods.rb', line 132

def default_config
  {
    strategy: default_strategy_name,
    request: {
      max_redirects: RequestService::Policy::DEFAULTS[:max_redirects],
      max_requests: RequestService::Policy::DEFAULTS[:max_requests]
    },
    channel: { time_zone: 'UTC' },
    headers: RequestHeaders.browser_defaults,
    stylesheets: []
  }
end

#default_strategy_nameSymbol

Returns the default strategy for feed orchestration.

Returns:

  • (Symbol)

    the default strategy for feed orchestration



146
147
148
# File 'lib/html2rss/config/class_methods.rb', line 146

def default_strategy_name
  :auto
end

#from_hash(config, params: UNSET) ⇒ Html2rss::Config

Processes the provided configuration hash, applying dynamic parameters if given, and returns a new configuration object.

Parameters:

  • config (Hash{Symbol => Object})

    the configuration hash.

  • params (Hash{Symbol => Object}, Hash{String => Object}, nil) (defaults to: UNSET)

    dynamic parameters for string formatting.

Returns:



104
105
106
# File 'lib/html2rss/config/class_methods.rb', line 104

def from_hash(config, params: UNSET)
  new(resolve_effective_config(config, params:))
end

#json_schemaHash{String => Object}

Returns the exported JSON Schema for html2rss configuration.

Returns:

  • (Hash{String => Object})

    JSON Schema represented as a Ruby hash



15
16
17
# File 'lib/html2rss/config/class_methods.rb', line 15

def json_schema
  Schema.json_schema
end

#json_schema_json(pretty: true) ⇒ String

Returns the exported JSON Schema as JSON.

Parameters:

  • pretty (Boolean) (defaults to: true)

    whether to pretty-print the JSON output

Returns:

  • (String)

    serialized JSON Schema



24
25
26
# File 'lib/html2rss/config/class_methods.rb', line 24

def json_schema_json(pretty: true)
  pretty ? JSON.pretty_generate(json_schema) : JSON.generate(json_schema)
end

#load_yaml(file, feed_name = nil, multiple_feeds_key: MultipleFeedsConfig::CONFIG_KEY_FEEDS) ⇒ Hash{Symbol => Object}

Loads the feed configuration from a YAML file.

Supports multiple feeds defined under the specified key (default :feeds).

rubocop:disable Metrics/MethodLength

Parameters:

  • file (String)

    the YAML file to load.

  • feed_name (String, nil) (defaults to: nil)

    the feed name when using multiple feeds.

  • multiple_feeds_key (Symbol) (defaults to: MultipleFeedsConfig::CONFIG_KEY_FEEDS)

    the key under which multiple feeds are defined.

Returns:

  • (Hash{Symbol => Object})

    the configuration hash.

Raises:

  • (ArgumentError)

    if the file doesn’t exist or feed is not found.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/html2rss/config/class_methods.rb', line 76

def load_yaml(file, feed_name = nil, multiple_feeds_key: MultipleFeedsConfig::CONFIG_KEY_FEEDS)
  raise ArgumentError, "File '#{file}' does not exist" unless File.exist?(file)
  raise ArgumentError, "`#{multiple_feeds_key}` is a reserved feed name" if feed_name == multiple_feeds_key

  yaml = YAML.safe_load_file(file, symbolize_names: true)

  return yaml unless yaml.key?(multiple_feeds_key)

  unless feed_name
    available_feeds = yaml.fetch(multiple_feeds_key).keys.join(', ')
    raise ArgumentError,
          "Feed name is required under `#{multiple_feeds_key}`. Available feeds: #{available_feeds}"
  end

  config = yaml.dig(multiple_feeds_key, feed_name.to_sym)
  raise ArgumentError, "Feed '#{feed_name}' not found under `#{multiple_feeds_key}` key." unless config

  MultipleFeedsConfig.to_single_feed(config, yaml, multiple_feeds_key:)
end

#schema_pathString

Returns the packaged JSON Schema file path.

Returns:

  • (String)

    absolute path to the packaged JSON Schema file



49
50
51
# File 'lib/html2rss/config/class_methods.rb', line 49

def schema_path
  Schema.path
end

#validate(config, params: UNSET) ⇒ Dry::Validation::Result

Validates a configuration hash with the runtime validator.

Parameters:

  • config (Hash{Symbol => Object})

    the configuration hash

  • params (Hash{Symbol => Object}, Hash{String => Object}, nil) (defaults to: UNSET)

    dynamic parameters for string formatting

Returns:

  • (Dry::Validation::Result)

    validation result after defaults and deprecations are applied



34
35
36
37
38
39
40
41
42
43
# File 'lib/html2rss/config/class_methods.rb', line 34

def validate(config, params: UNSET)
  prepared_config = prepare_for_validation(resolve_effective_config(config, params:))

  Validator.new.call(prepared_config)
rescue DynamicParams::ParamsMissing => error
  prepared_config = prepare_for_validation(HashUtil.deep_symbolize_keys(config, context: 'config'))
  prepared_config[:dynamic_params_error] = error.message

  Validator.new.call(prepared_config)
end

#validate_yaml(file, feed_name = nil, multiple_feeds_key: MultipleFeedsConfig::CONFIG_KEY_FEEDS, params: UNSET) ⇒ Dry::Validation::Result

Loads and validates a YAML configuration file.

Parameters:

  • file (String)

    the YAML file to load

  • feed_name (String, nil) (defaults to: nil)

    optional feed name for multi-feed files

  • multiple_feeds_key (Symbol) (defaults to: MultipleFeedsConfig::CONFIG_KEY_FEEDS)

    key under which multiple feeds are defined

  • params (Hash{Symbol => Object}, Hash{String => Object}, nil) (defaults to: UNSET)

    dynamic parameters for string formatting

Returns:

  • (Dry::Validation::Result)

    validation result after defaults and deprecations are applied



61
62
63
# File 'lib/html2rss/config/class_methods.rb', line 61

def validate_yaml(file, feed_name = nil, multiple_feeds_key: MultipleFeedsConfig::CONFIG_KEY_FEEDS, params: UNSET)
  validate(load_yaml(file, feed_name, multiple_feeds_key:), params:)
end