diff --git a/lib/html2rss/config.rb b/lib/html2rss/config.rb index 813a628..c40e07d 100644 --- a/lib/html2rss/config.rb +++ b/lib/html2rss/config.rb @@ -43,12 +43,16 @@ def headers global_config.fetch('headers', {}) end - def options(name) + def attribute_options(name) feed_config.dig('selectors').fetch(name, {}).merge('channel' => channel_config) end + def attribute?(name) + attribute_names.include?(name.to_s) + end + def categories - feed_config.dig('selectors').fetch('categories', []) + feed_config.dig('selectors').fetch('categories', []).map(&:to_sym) end def selector(name) @@ -56,9 +60,9 @@ def selector(name) end def attribute_names - attribute_names = feed_config.fetch('selectors', {}).keys.map(&:to_s) - attribute_names.delete('items') - attribute_names + @attribute_names ||= feed_config.fetch('selectors', {}).keys.map(&:to_s).tap do |attrs| + attrs.delete('items') + end end private diff --git a/lib/html2rss/item.rb b/lib/html2rss/item.rb index b5fe358..bce5ac8 100644 --- a/lib/html2rss/item.rb +++ b/lib/html2rss/item.rb @@ -15,17 +15,18 @@ def initialize(xml, config) private_class_method :new def respond_to_missing?(method_name, _include_private = false) - config.attribute_names.include?(method_name) || super + config.attribute?(method_name) || super end def method_missing(method_name, *_args) - attribute_config = config.options(method_name.to_s) - return super unless attribute_config + return super unless respond_to_missing?(method_name) - extractor = ItemExtractors.get_extractor(attribute_config['extractor']) - value = extractor.new(xml, attribute_config).get + attribute_options = config.attribute_options(method_name) - post_process(value, attribute_config.fetch('post_process', false)) + extractor = ItemExtractors.get_extractor(attribute_options['extractor']) + value = extractor.new(xml, attribute_options).get + + post_process(value, attribute_options.fetch('post_process', false)) end def available_attributes @@ -33,8 +34,12 @@ def available_attributes @config.attribute_names) - ['categories'] end + ## + # At least a title or a description is required to be a valid RSS 2.0 item. def valid? - [title.to_s, description.to_s].join('') != '' + title = self.title if config.attribute?(:title) + description = self.description if config.attribute?(:description) + [title, description].join != '' end ## diff --git a/spec/html2rss_spec.rb b/spec/html2rss_spec.rb index 4c471bf..3fc1834 100644 --- a/spec/html2rss_spec.rb +++ b/spec/html2rss_spec.rb @@ -130,10 +130,10 @@ end describe 'item.category' do - subject(:categories) { item.css('category').map(&:text) } + subject(:categories) { item.css('category').to_s } it 'sets the author as category' do - expect(categories).to include 'manni' + expect(categories).to include 'manni' end end