Skip to content

Commit

Permalink
fix: rss contains additional categories (#39)
Browse files Browse the repository at this point in the history
* fix: rss contains additional categories

* use respond_to_missing?
  • Loading branch information
gildesmarais committed Sep 19, 2019
1 parent a07b969 commit ed164ef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
14 changes: 9 additions & 5 deletions lib/html2rss/config.rb
Expand Up @@ -43,22 +43,26 @@ 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)
feed_config.dig('selectors', name, 'selector')
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
Expand Down
19 changes: 12 additions & 7 deletions lib/html2rss/item.rb
Expand Up @@ -15,26 +15,31 @@ 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
@available_attributes ||= (%w[title link description author comments updated] &
@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

##
Expand Down
4 changes: 2 additions & 2 deletions spec/html2rss_spec.rb
Expand Up @@ -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 '<category>manni</category>'
end
end

Expand Down

0 comments on commit ed164ef

Please sign in to comment.