Sha256: a346de3a51c381e6ded6f57572983f2381b81ae3b099e7fcafd00a92e0065eb6

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

require 'rss'
require_relative 'item'

module Html2rss
  class FeedBuilder
    attr_reader :config

    def initialize(feed_config)
      @config = feed_config
    end

    def rss
      RSS::Maker.make('2.0') do |maker|
        add_channel_to_maker(maker)

        feed_items.map do |feed_item|
          add_item_to_items(feed_item, maker.items)
        end
      end
    end

    private

    def add_channel_to_maker(maker)
      %i[language author title description link ttl].each do |attribute_name|
        maker.channel.send("#{attribute_name}=".to_sym, config.send(attribute_name))
      end

      maker.channel.generator = "html2rss V. #{::Html2rss::VERSION}"
      maker.channel.lastBuildDate = Time.now.to_s
    end

    def feed_items
      @feed_items ||= Item.from_url config.url, config
    end

    def add_item_to_items(feed_item, items)
      return unless feed_item.valid?

      items.new_item do |rss_item|
        feed_item.available_attributes.each do |attribute_name|
          rss_item.send("#{attribute_name}=".to_sym, feed_item.send(attribute_name))
        end

        feed_item.categories.each do |category|
          rss_item.categories.new_category.content = category
        end

        rss_item.guid.content = Digest::SHA1.hexdigest(feed_item.title)
        rss_item.guid.isPermaLink = false
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
html2rss-0.3.3 lib/html2rss/feed_builder.rb
html2rss-0.3.2 lib/html2rss/feed_builder.rb
html2rss-0.3.1 lib/html2rss/feed_builder.rb
html2rss-0.3.0 lib/html2rss/feed_builder.rb
html2rss-0.2.2 lib/html2rss/feed_builder.rb
html2rss-0.2.1 lib/html2rss/feed_builder.rb