Class: Ausca::RSS::Joiner
- Inherits:
-
Object
- Object
- Ausca::RSS::Joiner
- Defined in:
- lib/ausca/rss/joiner.rb
Instance Method Summary collapse
-
#fetch ⇒ Object
Fetch and combine the remote feeds.
-
#filter(items) ⇒ Object
Filter the output feed items based on optional constraints.
-
#generate ⇒ Object
Generates the output RSS feed.
-
#initialize(opts) ⇒ Joiner
constructor
A new instance of Joiner.
- #options ⇒ Object
- #save(rss) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Joiner
Returns a new instance of Joiner.
27 28 29 30 |
# File 'lib/ausca/rss/joiner.rb', line 27 def initialize(opts) .merge!(opts) [:feeds].to_a unless [:feeds].is_a? Array end |
Instance Method Details
#fetch ⇒ Object
Fetch and combine the remote feeds
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ausca/rss/joiner.rb', line 71 def fetch items = [] # File.open(options[:feeds], 'r').each_line { |f| } [:feeds].each { |url| p "Fetching: #{url}" open(url) do |rss| doc = Nokogiri::XML(rss.read) # Nokogiri::XML::ParseOptions::NOCDATA doc.xpath('//item').map do |i| items << { title: i.xpath('title').text, description: i.xpath('description').text, link: RSS::Util.item_source_url(i), image: RSS::Util.item_image_url(i), category: i.xpath('category').text, pubDate: Time.parse(i.xpath('pubDate').text) } end end } items.sort_by { |k| k[:pubDate] }.reverse end |
#filter(items) ⇒ Object
Filter the output feed items based on optional constraints
94 95 96 |
# File 'lib/ausca/rss/joiner.rb', line 94 def filter items items = items.first([:max_items]) if [:max_items] > 0 end |
#generate ⇒ Object
Generates the output RSS feed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ausca/rss/joiner.rb', line 33 def generate items = self.fetch items = self.filter items rss = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml| xml.rss(version: [:version]) do xml.channel do xml.title [:title] xml.description [:description] xml.link [:link] xml. [:author] items.each do |source_item| xml.item do xml.title source_item[:title] xml.description do xml.cdata source_item[:description] # CGI::unescape_html end xml.link source_item[:link] xml.category source_item[:category] unless source_item[:category].empty? xml.pubDate source_item[:pubDate].httpdate # optional image if source_item[:image] xml.image do xml.url source_item[:image] end end end end end end end self.save rss.to_xml end |
#options ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/ausca/rss/joiner.rb', line 7 def @options ||= { # Array of source feed URLs to join feeds: [], # Max output feed items max_items: 20, # Output RSS file path output_path: "feed.rss", # RSS variables version: "2.0", title: "my title", description: "my description", link: "my link", author: "my author", } end |
#save(rss) ⇒ Object
98 99 100 101 |
# File 'lib/ausca/rss/joiner.rb', line 98 def save rss File.write([:output_path], rss) #File.open(options[:output_path], 'w') { |f| rss } end |