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
-
#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.
-
#default_config ⇒ Hash{Symbol => Object}
Provides a default configuration.
-
#default_strategy_name ⇒ Symbol
The default strategy for feed orchestration.
-
#from_hash(config, params: UNSET) ⇒ Html2rss::Config
Processes the provided configuration hash, applying dynamic parameters if given, and returns a new configuration object.
-
#json_schema ⇒ Hash{String => Object}
Returns the exported JSON Schema for html2rss configuration.
-
#json_schema_json(pretty: true) ⇒ String
Returns the exported JSON Schema as JSON.
-
#load_yaml(file, feed_name = nil, multiple_feeds_key: MultipleFeedsConfig::CONFIG_KEY_FEEDS) ⇒ Hash{Symbol => Object}
Loads the feed configuration from a YAML file.
-
#schema_path ⇒ String
Returns the packaged JSON Schema file path.
-
#validate(config, params: UNSET) ⇒ Dry::Validation::Result
Validates a configuration hash with the runtime validator.
-
#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.
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.
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_config ⇒ Hash{Symbol => Object}
Provides a default configuration.
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_name ⇒ Symbol
Returns 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.
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_schema ⇒ Hash{String => Object}
Returns the exported JSON Schema for html2rss configuration.
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.
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
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_path ⇒ String
Returns the packaged JSON Schema file path.
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.
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. 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.
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 |