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 10
def self.included(base)
  base.extend ClassMethods
end

Public Instance Methods

last_modified() click to toggle source
# File lib/feedjira/feed_utilities.rb, line 43
def last_modified
  @last_modified ||= begin
    published = entries.reject { |e| e.published.nil? }
    entry = published.max_by(&:published)
    entry ? entry.published : nil
  end
end
new_entries() click to toggle source
# File lib/feedjira/feed_utilities.rb, line 55
def new_entries
  @new_entries ||= []
end
new_entries?() click to toggle source
# File lib/feedjira/feed_utilities.rb, line 59
def new_entries?
  !new_entries.empty?
end
sanitize_entries!() click to toggle source
# File lib/feedjira/feed_utilities.rb, line 86
def sanitize_entries!
  entries.each(&:sanitize!)
end
update_attribute(feed, name) click to toggle source
# File lib/feedjira/feed_utilities.rb, line 74
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
update_from_feed(feed) click to toggle source
# File lib/feedjira/feed_utilities.rb, line 63
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
updated?() click to toggle source
# File lib/feedjira/feed_utilities.rb, line 51
def updated?
  @updated || false
end

Private Instance Methods

find_new_entries_for(feed) click to toggle source

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.

# File lib/feedjira/feed_utilities.rb, line 97
def find_new_entries_for(feed)
  return feed.entries if entries.length.zero?

  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
new_entry?(entry, latest) click to toggle source
# File lib/feedjira/feed_utilities.rb, line 112
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