Sha256: 1c161fb5df652a5659c30733b1c4443f4d93dbc995dd737c0e658747c7e8c12d

Contents?: true

Size: 1.88 KB

Versions: 88

Compression:

Stored size: 1.88 KB

Contents

module Feedzirra
  module FeedUtilities
    UPDATABLE_ATTRIBUTES = %w(title feed_url url last_modified)
    
    attr_writer   :new_entries, :updated, :last_modified
    attr_accessor :etag

    def last_modified
      @last_modified ||= begin
        entry = entries.reject {|e| e.published.nil? }.sort_by { |entry| entry.published if entry.published }.last
        entry ? entry.published : nil
      end
    end
    
    def updated?
      @updated
    end
    
    def new_entries
      @new_entries ||= []
    end
    
    def has_new_entries?
      new_entries.size > 0
    end
    
    def update_from_feed(feed)
      self.new_entries += find_new_entries_for(feed)
      self.entries.unshift(*self.new_entries)
      
      updated! if UPDATABLE_ATTRIBUTES.any? { |name| update_attribute(feed, name) }
    end
    
    def update_attribute(feed, name)
      old_value, new_value = send(name), feed.send(name)
      
      if old_value != new_value
        send("#{name}=", new_value)
      end
    end
    
    def sanitize_entries!
      entries.each {|entry| entry.sanitize!}
    end
    
    private
    
    def updated!
      @updated = true
    end
    
    def find_new_entries_for(feed)
      # this implementation is a hack, which is why it's so ugly.
      # it's to get around the fact that not all feeds have a published date.
      # however, they're always ordered with the newest one first.
      # So we go through the entries just parsed and insert each one as a new entry
      # until we get to one that has the same url as the the newest for the feed
      latest_entry = self.entries.first
      found_new_entries = []
      feed.entries.each do |entry|
        break if entry.url == latest_entry.url
        found_new_entries << entry
      end
      found_new_entries
    end
    
    def existing_entry?(test_entry)
      entries.any? { |entry| entry.url == test_entry.url }
    end
  end
end

Version data entries

88 entries across 88 versions & 26 rubygems

Version Path
Spectives-feedzirra-0.0.28 lib/feedzirra/feed_utilities.rb
Spectives-feedzirra-0.0.29 lib/feedzirra/feed_utilities.rb
Spectives-feedzirra-0.0.31 lib/feedzirra/feed_utilities.rb
Spectives-logophobia-feedzirra-0.0.31 lib/feedzirra/feed_utilities.rb
UnderpantsGnome-feedzirra-0.0.14 lib/feedzirra/feed_utilities.rb
UnderpantsGnome-feedzirra-0.0.15 lib/feedzirra/feed_utilities.rb
UnderpantsGnome-feedzirra-0.0.16 lib/feedzirra/feed_utilities.rb
astro-feedzirra-0.0.12 lib/feedzirra/feed_utilities.rb
astro-feedzirra-0.0.8.20090419 lib/feedzirra/feed_utilities.rb
dakrone-feedzirra-0.0.16.1 lib/feedzirra/feed_utilities.rb
dinsley-feedzirra-0.0.10 lib/feedzirra/feed_utilities.rb
dinsley-feedzirra-0.0.11 lib/feedzirra/feed_utilities.rb
dinsley-feedzirra-0.0.8 lib/feedzirra/feed_utilities.rb
dinsley-feedzirra-0.0.9 lib/feedzirra/feed_utilities.rb
eric-feedzirra-0.0.8 lib/feedzirra/feed_utilities.rb
jeffrafter-feedzirra-0.0.4 lib/feedzirra/feed_utilities.rb
jsl-feedzirra-0.0.12.1 lib/feedzirra/parser/feed_utilities.rb
jsl-feedzirra-0.0.12.2 lib/feedzirra/parser/feed_utilities.rb
jsl-feedzirra-0.0.12.3 lib/feedzirra/parser/feed_utilities.rb
jsl-feedzirra-0.0.12.4 lib/feedzirra/parser/feed_utilities.rb