Sha256: b23a1c017b02af317135e35431460aac822c9a7d9880a18a9284d953a8409521

Contents?: true

Size: 1.9 KB

Versions: 29

Compression:

Stored size: 1.9 KB

Contents

module Feedzirra
  module FeedUtilities
    UPDATABLE_ATTRIBUTES = %w(title feed_url url last_modified etag)

    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 = false
      UPDATABLE_ATTRIBUTES.each do |name|
        updated = update_attribute(feed, name)
        @updated ||= updated
      end
    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 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
      return feed.entries if self.entries.length == 0
      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

29 entries across 29 versions & 11 rubygems

Version Path
codders-feedzirra-0.2.0.rc2 lib/feedzirra/feed_utilities.rb
feedzirra-0.2.0.rc2 lib/feedzirra/feed_utilities.rb
feedzirra-0.2.0.rc1 lib/feedzirra/feed_utilities.rb
fandango-0.2.1 lib/vendor/feedzirra/lib/feedzirra/feed_utilities.rb
fandango-0.2.0 lib/vendor/feedzirra/lib/feedzirra/feed_utilities.rb
fandango-0.1.9 lib/vendor/feedzirra/lib/feedzirra/feed_utilities.rb
fandango-0.1.8 lib/vendor/feedzirra/lib/feedzirra/feed_utilities.rb
fandango-0.1.7 lib/vendor/feedzirra/lib/feedzirra/feed_utilities.rb
fandango-0.1.6 lib/vendor/feedzirra/lib/feedzirra/feed_utilities.rb
fandango-0.1.5 lib/vendor/feedzirra/lib/feedzirra/feed_utilities.rb
feedzirra-0.1.2 lib/feedzirra/feed_utilities.rb
fandango-0.1.4 lib/vendor/feedzirra/lib/feedzirra/feed_utilities.rb
seanwalbran-feedzirra-0.1.3 lib/feedzirra/feed_utilities.rb
seanwalbran-feedzirra-0.1.2 lib/feedzirra/feed_utilities.rb
localist-feedzirra-0.2.1 lib/feedzirra/feed_utilities.rb
feedzirra-0.1.1 lib/feedzirra/feed_utilities.rb
feedzirra-0.1.0 lib/feedzirra/feed_utilities.rb
feedzirra-0.0.31 lib/feedzirra/feed_utilities.rb
feedzirra-0.0.30 lib/feedzirra/feed_utilities.rb
penso-feedzirra-0.0.24 lib/feedzirra/feed_utilities.rb