lib/html2rss/item.rb in html2rss-0.0.1 vs lib/html2rss/item.rb in html2rss-0.1.0
- old
+ new
@@ -1,9 +1,10 @@
require 'faraday'
require 'open-uri'
require 'nokogiri'
require_relative 'item_extractor'
+require_relative 'attribute_post_processors'
module Html2rss
class Item
attr_reader :xml, :config
@@ -22,28 +23,39 @@
extractor = attribute_config['extractor'] || 'text'
proc = ItemExtractor.const_get extractor.upcase.to_sym
value = proc.call(xml, attribute_config)
- post_process(method_name, value)
+ post_process_options = attribute_config.fetch('post_process', false)
+ value = post_process(value, post_process_options) if post_process_options
+
+ value
end
- def post_process(method_name, value)
- case method_name
- when :link
- URI(value)
- when :updated
- Time.parse(value).to_s
- else
- value
- end
+ def available_attributes
+ # TODO: support optional attributes, e.g. category, enclosure, source
+ @available_attributes ||= (%w[title link description author comments updated] & @config.attribute_names)
end
+ def valid?
+ return false if [title.to_s, description.to_s].join('') == ''
+
+ true
+ end
+
def self.from_url(url, config)
connection = Faraday.new(url: url, headers: config.headers)
page = Nokogiri::HTML(connection.get.body)
- page.css(config.selector('items')).map { |xml_item|
+ page.css(config.selector('items')).map do |xml_item|
new xml_item, config
- }
+ end
+ end
+
+ private
+
+ def post_process(value, options)
+ Html2rss::AttributePostProcessors.get_processor(options)
+ .new(value, options, self)
+ .get
end
end
end