module Feedjira::FeedUtilities
Constants
- UPDATABLE_ATTRIBUTES
Attributes
Public Class Methods
Source
# File lib/feedjira/feed_utilities.rb, line 10 def self.included(base) base.extend ClassMethods end
Public Instance Methods
Source
# File lib/feedjira/feed_utilities.rb, line 43 def last_modified @last_modified ||= entries.reject { |e| e.published.nil? }.max_by(&:published)&.published end
Source
# File lib/feedjira/feed_utilities.rb, line 51 def new_entries @new_entries ||= [] end
Source
# File lib/feedjira/feed_utilities.rb, line 55 def new_entries? !new_entries.empty? end
Source
# File lib/feedjira/feed_utilities.rb, line 82 def sanitize_entries! entries.each(&:sanitize!) end
Source
# File lib/feedjira/feed_utilities.rb, line 70 def update_attribute(feed, name) old_value = send(name) new_value = feed.send(name) if old_value == new_value false else send(:"#{name}=", new_value) true end end
Source
# File lib/feedjira/feed_utilities.rb, line 59 def update_from_feed(feed) self.new_entries += find_new_entries_for(feed) entries.unshift(*self.new_entries) @updated = false UPDATABLE_ATTRIBUTES.each do |name| @updated ||= update_attribute(feed, name) end end
Source
# File lib/feedjira/feed_utilities.rb, line 47 def updated? @updated || false end
Private Instance Methods
Source
# File lib/feedjira/feed_utilities.rb, line 93 def find_new_entries_for(feed) return feed.entries if entries.empty? latest_entry = entries.first found_new_entries = [] feed.entries.each do |entry| break unless new_entry?(entry, latest_entry) found_new_entries << entry end found_new_entries end
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.
Source
# File lib/feedjira/feed_utilities.rb, line 108 def new_entry?(entry, latest) nil_ids = entry.entry_id.nil? && latest.entry_id.nil? new_id = entry.entry_id != latest.entry_id new_url = entry.url != latest.url (nil_ids || new_id) && new_url end