Class: Html2rss::Config::Selectors

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/config/selectors.rb

Overview

Holds the configurations of the selectors.

Defined Under Namespace

Classes: InvalidSelectorName, Selector

Constant Summary collapse

ITEMS_SELECTOR_NAME =
:items

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Selectors



19
20
21
22
# File 'lib/html2rss/config/selectors.rb', line 19

def initialize(config)
  validate_config(config)
  @config = config
end

Instance Method Details

#category_selector_namesSet<Symbol>



48
49
50
# File 'lib/html2rss/config/selectors.rb', line 48

def category_selector_names
  selector_keys_for(:categories)
end

#guid_selector_namesSet<Symbol>



54
55
56
# File 'lib/html2rss/config/selectors.rb', line 54

def guid_selector_names
  selector_keys_for(:guid, default: :title_or_description)
end

#item_selector_namesSet<Symbol>



69
70
71
# File 'lib/html2rss/config/selectors.rb', line 69

def item_selector_names
  @item_selector_names ||= config.keys.reject { |key| key == ITEMS_SELECTOR_NAME }.to_set
end

#items_orderSymbol?



75
76
77
# File 'lib/html2rss/config/selectors.rb', line 75

def items_order
  config.dig(ITEMS_SELECTOR_NAME, :order)&.to_sym
end

#selector(name) ⇒ Selector



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

def selector(name)
  raise InvalidSelectorName, "invalid selector name: #{name}" unless selector?(name)

  keywords = config[name].slice(*available_keys)

  if (additional_keys = keywords.keys - available_keys).any?
    Log.warn "additional keys (#{additional_keys.join(', ')}) present in selector #{name}"
  end

  Selector.new(keywords)
end

#selector?(name) ⇒ true, false



27
28
29
# File 'lib/html2rss/config/selectors.rb', line 27

def selector?(name)
  name != ITEMS_SELECTOR_NAME && item_selector_names.include?(name)
end

#selector_string(name) ⇒ String

Returns the CSS/XPath selector.



63
64
65
# File 'lib/html2rss/config/selectors.rb', line 63

def selector_string(name)
  Selector.new(config[name]).selector
end