Sha256: 92166b2ccee40f5eb9ad1faa0a8081c8302312790eb3023da8ac612d69853c90

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

require 'rubygems'
require 'builder'

require 'uri'

module Murlsh

  class AtomFeed

    def initialize(root_url, options={})
      options = {
        :filename => 'atom.xml',
        :title => 'Atom feed' }.merge(options)
      @root_url = root_url
      @filename = options[:filename]
      @title = options[:title]

      setup_id_fields
    end

    def setup_id_fields
      uri_parsed = URI(@root_url)

      m = uri_parsed.host.match(/^(.*?)\.?([^.]+\.[^.]+)$/)

      @host, @domain = (m ? m.captures : [uri_parsed.host, ''])

      @path = uri_parsed.path
    end

    def write(entries, path)
      open(path, 'w') do |f|
        f.flock(File::LOCK_EX)

        make(entries, :target => f)

        f.flock(File::LOCK_UN)
      end
    end

    def make(entries, options={})
      xm = Builder::XmlMarkup.new(options)
      xm.instruct! :xml

      xm.feed(:xmlns => 'http://www.w3.org/2005/Atom') {
        xm.id(@root_url)
        xm.link(:href => URI.join(@root_url, @filename), :rel => 'self')
        xm.title(@title)
        xm.updated(entries.collect { |mu| mu.time }.max.xmlschema)
        entries.each do |mu|
          xm.entry {
            xm.author { xm.name(mu.name) }
            xm.title(mu.title)
            xm.id(entry_id(mu))
            xm.summary(mu.title)
            xm.updated(mu.time.xmlschema)
            xm.link(:href => mu.url)
            enclosure(xm, mu)
          }
        end
      }
      xm
    end

    def entry_id(url)
      "tag:#{@domain},#{url.time.strftime('%Y-%m-%d')}:#{@host}#{@path}#{url.id}"
    end

    def enclosure(xm, mu)
      xm.link(:rel => 'enclosure', :type => mu.content_type, :href => mu.url,
        :title => 'Full-size') if mu.is_image?
    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
murlsh-0.2.4 lib/murlsh/atom_feed.rb
murlsh-0.2.3 lib/murlsh/atom_feed.rb
murlsh-0.2.2 lib/murlsh/atom_feed.rb
murlsh-0.2.1 lib/murlsh/atom_feed.rb