Sha256: 0e768d3bd6d4da6212faa325becc800f132fc13320c27419357674bd363ef263

Contents?: true

Size: 1.82 KB

Versions: 6

Compression:

Stored size: 1.82 KB

Contents

class NewsFeed < ActiveRecord::Base
  attr_accessible :title, :url
  include ExpireEditableFragment
  default_scope :order => "news_feeds.position"
  belongs_to :library_group, :validate => true

  validates_presence_of :title, :url, :library_group
  validates_associated :library_group
  validates_length_of :url, :maximum => 255

  acts_as_list

  after_save :expire_cache
  after_destroy :expire_cache

  def self.per_page
    10
  end

  def expire_cache
    expire_fragment_cache
    Rails.cache.delete('news_feed_all')
    true
  end

  def expire_fragment_cache
    Role.all.each do |role|
      Rails.cache.delete("views/news_feed_content_#{id}_#{role.name}")
      logger.info "#{Time.zone.now} feed reloaded! : #{url}"
    end
  end

  def content(clear_cache = false)
    #page_url = URI.parse(url.rewrite_my_url)
    #if page_url.port == 80
    #  if Feedbag.feed?(url)
    #    feed_url = url
    #  else
    #    feed_url = Feedbag.find(url).first
    #  end
    #else
      # auto-discovery 非対応
      feed_url = url
    #end
    begin
      if clear_cache or body.blank?
        feed = open(feed_url) do |f|
          f.read
        end
        if rss = RSS::Parser.parse(feed, false)
          self.body = feed
          save!
        end
      end
    rescue StandardError, Timeout::Error
      nil
    end
    # tDiary の RSS をパースした際に to_s が空になる
    # rss = RSS::Parser.parse(feed)
    # rss.to_s
    # => ""
    #if rss.nil?
      begin
        rss = RSS::Parser.parse(body)
      rescue RSS::InvalidRSSError
        rss = RSS::Parser.parse(body, false)
      rescue RSS::NotWellFormedError, TypeError
        nil
      end
    #end
  end

  def force_reload
    expire_cache
    content(true)
  end

  def self.fetch_feeds
    NewsFeed.all.each do |news_feed|
      news_feed.force_reload
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
enju_news-0.1.0.pre2 app/models/news_feed.rb
enju_news-0.1.0.pre app/models/news_feed.rb
enju_news-0.0.5 app/models/news_feed.rb
enju_news-0.0.4 app/models/news_feed.rb
enju_news-0.0.3 app/models/news_feed.rb
enju_news-0.0.2 app/models/news_feed.rb