Sha256: bf6a95ccb1ed296ecd4f0e9a7c7d7be5ffa1462df70e786692ea8b94d29fff2f

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

require 'rss'
require 'mime/types'

module Html2rss
  ##
  # The purpose is to build the feed, consisting of
  #
  # - the 'channel' and
  # - the 'item'
  #
  # parts.
  class FeedBuilder
    def initialize(config)
      @config = config
    end

    ##
    # @return [RSS:Rss]
    def rss
      RSS::Maker.make('2.0') do |maker|
        add_channel(maker.channel)

        items.each { |item| add_item(item, maker.items.new_item) }
      end
    end

    def self.add_categories(categories, item_maker)
      categories.each { |category| item_maker.categories.new_category.content = category }
    end

    def self.add_enclosure_from_url(url, item_maker)
      return unless url

      enclosure = item_maker.enclosure
      content_type = MIME::Types.type_for(File.extname(url).delete('.'))

      enclosure.type = content_type.any? ? content_type.first.to_s : 'application/octet-stream'
      enclosure.length = 0
      enclosure.url = url
    end

    def self.add_guid(item, item_maker)
      guid = item_maker.guid
      guid.content = Digest::SHA1.hexdigest(item.title)
      guid.isPermaLink = false
    end

    private

    attr_reader :config

    def add_channel(channel_maker)
      %i[language author title description link ttl].each do |attribute_name|
        channel_maker.public_send("#{attribute_name}=", config.public_send(attribute_name))
      end

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

    def items
      @items ||= Item.from_url(config.url, config)
    end

    def add_item(item, item_maker)
      item.available_attributes.each do |attribute_name|
        item_maker.public_send("#{attribute_name}=", item.public_send(attribute_name))
      end

      self.class.add_categories(item.categories, item_maker)
      self.class.add_enclosure_from_url(item.enclosure_url, item_maker) if item.enclosure?
      self.class.add_guid(item, item_maker)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
html2rss-0.8.2 lib/html2rss/feed_builder.rb