Sha256: 4dc3df1885269b440c5a9bd834f0b50fa390c42806a053fb8f73c3d8eaa3869a

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

class NewsFeed < ActiveRecord::Base
  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
  before_save :fetch

  acts_as_list

  def self.per_page
    10
  end

  def fetch
    begin
      feed = Faraday.get(url).body.force_encoding('UTF-8')
      if rss = RSS::Parser.parse(feed, false)
        self.body = feed
      end
    rescue StandardError, Timeout::Error
      nil
    end
  end

  def content
    if body
    # 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
  end

  def force_reload
    save!
  end

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

# == Schema Information
#
# Table name: news_feeds
#
#  id               :integer          not null, primary key
#  library_group_id :integer          default(1), not null
#  title            :string
#  url              :string
#  body             :text
#  position         :integer
#  created_at       :datetime
#  updated_at       :datetime
#

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
enju_news-0.2.0 app/models/news_feed.rb
enju_news-0.2.0.beta.3 app/models/news_feed.rb
enju_news-0.2.0.beta.2 app/models/news_feed.rb
enju_news-0.1.1 app/models/news_feed.rb
enju_news-0.1.0 app/models/news_feed.rb
enju_news-0.1.0.pre9 app/models/news_feed.rb