Class: Html2rss::Config

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

Overview

The Config class abstracts from the config data structure and provides default values.

Instance Method Summary collapse

Constructor Details

#initialize(feed_config, global_config = {}) ⇒ Config

Returns a new instance of Config.



8
9
10
11
12
# File 'lib/html2rss/config.rb', line 8

def initialize(feed_config, global_config = {})
  @global_config = global_config.deep_symbolize_keys
  @feed_config = feed_config.deep_symbolize_keys
  @channel_config = @feed_config.fetch(:channel, {})
end

Instance Method Details

#attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def attribute?(name)
  attribute_names.include?(name)
end

#attribute_namesObject



83
84
85
# File 'lib/html2rss/config.rb', line 83

def attribute_names
  @attribute_names ||= feed_config.fetch(:selectors, {}).keys.tap { |attrs| attrs.delete(:items) }
end

#attribute_options(name) ⇒ Object



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

def attribute_options(name)
  feed_config.dig(:selectors).fetch(name, {}).merge(channel: channel_config)
end

#authorObject



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

def author
  channel_config.fetch :author, 'html2rss'
end

#category_selectorsObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/html2rss/config.rb', line 68

def category_selectors
  categories = feed_config.dig(:selectors, :categories)
  return [] unless categories

  categories = categories.keep_if { |category| category.to_s != '' }
  categories.map!(&:to_sym)
  categories.uniq!

  categories
end

#descriptionObject



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

def description
  channel_config.fetch :description, "Latest items from #{url}."
end

#generated_titleObject



26
27
28
29
30
31
32
33
# File 'lib/html2rss/config.rb', line 26

def generated_title
  uri = URI(url)

  nicer_path = uri.path.split('/')
  nicer_path.reject! { |part| part == '' }

  nicer_path.any? ? "#{uri.host}: #{nicer_path.join(' ').titleize}" : uri.host
end

#headersObject



56
57
58
# File 'lib/html2rss/config.rb', line 56

def headers
  global_config.fetch(:headers, {}).merge(channel_config.fetch(:headers, {}))
end

#items_orderObject



87
88
89
# File 'lib/html2rss/config.rb', line 87

def items_order
  feed_config.dig(:selectors, :items, :order)&.to_sym
end

#json?Boolean

Returns:

  • (Boolean)


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

def json?
  channel_config.fetch :json, false
end

#languageObject



35
36
37
# File 'lib/html2rss/config.rb', line 35

def language
  channel_config.fetch :language, 'en'
end

#selector(name) ⇒ Object



79
80
81
# File 'lib/html2rss/config.rb', line 79

def selector(name)
  feed_config.dig(:selectors, name, :selector)
end

#time_zoneObject



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

def time_zone
  channel_config.fetch :time_zone, 'UTC'
end

#titleObject



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

def title
  channel_config.fetch(:title) { generated_title }
end

#ttlObject



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

def ttl
  channel_config.fetch :ttl, 360
end

#urlObject Also known as: link



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

def url
  channel_config.dig :url
end