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

Returns a new instance of Selectors.

Parameters:

  • config (Hash<Symbol, Object>)


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

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

Instance Method Details

#category_selector_namesSet<Symbol>

Returns:

  • (Set<Symbol>)


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

def category_selector_names
  selector_keys_for(:categories)
end

#guid_selector_namesSet<Symbol>

Returns:

  • (Set<Symbol>)


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

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

#item_selector_namesSet<Symbol>

Returns:

  • (Set<Symbol>)


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

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

#items_orderSymbol?

Returns:

  • (Symbol, nil)


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

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

#selector(name) ⇒ Selector

Parameters:

  • name (Symbol)

Returns:

Raises:



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

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

Parameters:

  • name (Symbol)

Returns:

  • (true, false)


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

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

#selector_string(name) ⇒ String

Returns the CSS/XPath selector.

Parameters:

  • name (Symbol)

Returns:

  • (String)


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

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