cirandas.net

ref: master

app/models/external_feed.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class ExternalFeed < ApplicationRecord

  belongs_to :blog
  validates_presence_of :blog_id
  validates_presence_of :address, :if => lambda {|efeed| efeed.enabled}
  validates_uniqueness_of :blog_id

  scope :enabled, -> { where enabled: true }
  scope :expired, -> {
    where '(fetched_at is NULL) OR (fetched_at < ?)', Time.now - FeedUpdater.update_interval
  }

  attr_accessible :address, :enabled, :only_once

  delegate :environment, :to => :blog, :allow_nil => true

  def add_item(title, link, date, content)
    return if content.blank?
    doc = Nokogiri::HTML.fragment content
    doc.css('*').each do |p|
      if p.instance_of? Nokogiri::XML::Element
        p.remove_attribute 'style'
        p.remove_attribute 'class'
      end
    end
    content = doc.to_s

    article = TextArticle.new
    article.name = title
    article.profile = blog.profile
    article.body = content
    article.published_at = date
    article.source = link
    article.profile = blog.profile
    article.parent = blog
    article.author_name = self.feed_title
    unless blog.children.exists?(:slug => article.slug)
      article.save!
      article.delay.create_activity
    end
    article.valid?
  end

  def address=(new_address)
    self.fetched_at = nil unless address == new_address
    super(new_address)
  end

  def clear
    # do nothing
  end

  def finish_fetch
    if self.only_once && self.update_errors.zero?
      self.enabled = false
    end
    self.fetched_at = Time.now
    self.save!
  end

  def limit
    0
  end

end