module Feedjira::FeedUtilities
Constants
- UPDATABLE_ATTRIBUTES
Attributes
etag[RW]
last_modified[W]
new_entries[W]
updated[W]
Public Class Methods
included(base)
click to toggle source
# File lib/feedjira/feed_utilities.rb, line 8 def self.included(base) base.extend ClassMethods end
Public Instance Methods
has_new_entries?()
click to toggle source
# File lib/feedjira/feed_utilities.rb, line 48 def has_new_entries? new_entries.size > 0 end
last_modified()
click to toggle source
# File lib/feedjira/feed_utilities.rb, line 33 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
new_entries()
click to toggle source
# File lib/feedjira/feed_utilities.rb, line 44 def new_entries @new_entries ||= [] end
sanitize_entries!()
click to toggle source
# File lib/feedjira/feed_utilities.rb, line 74 def sanitize_entries! entries.each {|entry| entry.sanitize!} end
update_attribute(feed, name)
click to toggle source
# File lib/feedjira/feed_utilities.rb, line 63 def update_attribute(feed, name) old_value, new_value = send(name), feed.send(name) if old_value != new_value send("#{name}=", new_value) true else false end end
update_from_feed(feed)
click to toggle source
# File lib/feedjira/feed_utilities.rb, line 52 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) end end
updated?()
click to toggle source
# File lib/feedjira/feed_utilities.rb, line 40 def updated? @updated || false end
Private Instance Methods
existing_entry?(test_entry)
click to toggle source
# File lib/feedjira/feed_utilities.rb, line 100 def existing_entry?(test_entry) entries.any? { |entry| entry.id == test_entry.id } end
find_new_entries_for(feed)
click to toggle source
# File lib/feedjira/feed_utilities.rb, line 80 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 id 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| if entry.entry_id.nil? && latest_entry.entry_id.nil? break if entry.url == latest_entry.url else break if entry.entry_id == latest_entry.entry_id || entry.url == latest_entry.url end found_new_entries << entry end found_new_entries end