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: 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>)


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

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

Instance Method Details

#category_selector_namesSet<Symbol>

Returns:

  • (Set<Symbol>)


38
39
40
# File 'lib/html2rss/config/selectors.rb', line 38

def category_selector_names
  selector_keys_for(:categories)
end

#guid_selector_namesSet<Symbol>

Returns:

  • (Set<Symbol>)


44
45
46
# File 'lib/html2rss/config/selectors.rb', line 44

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

#item_selector_namesSet<Symbol>

Returns:

  • (Set<Symbol>)


59
60
61
# File 'lib/html2rss/config/selectors.rb', line 59

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

#items_orderSymbol?

Returns:

  • (Symbol, nil)


65
66
67
# File 'lib/html2rss/config/selectors.rb', line 65

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

#selector(name) ⇒ Selector

Parameters:

  • name (Symbol)

Returns:

Raises:

  • (ArgumentError)


30
31
32
33
34
# File 'lib/html2rss/config/selectors.rb', line 30

def selector(name)
  raise ArgumentError, "invalid item's selector name: #{name}" unless selector?(name)

  Selector.new(config[name])
end

#selector?(name) ⇒ true, false

Parameters:

  • name (Symbol)

Returns:

  • (true, false)


23
24
25
# File 'lib/html2rss/config/selectors.rb', line 23

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)


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

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