public
Description: A Ruby RSS Archiver
Homepage: http://cecs.anu.edu.au/~mreid/code/feed_bag.html
Clone URL: git://github.com/mreid/feed-bag.git
feed-bag / models.rb
100755 63 lines (53 sloc) 1.476 kb
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
# Feed Bag - A RSS Feed Archiver
#
# AUTHOR
# Mark D. Reid <mark.reid@anu.edu.au>
#
# CREATED
# 2008-01-18
 
# NOTE: Sequel must have already been opened with a DB before these models are.
 
require 'rubygems'
require 'sequel'
 
# Feeds URLs are stored here along with when they were last checked.
class Feed < Sequel::Model(:feeds)
  set_schema do
    primary_key :id
    text :name
    text :url
    time :last_checked
    time :created
  end
  
  after_create do
    feed = FeedNormalizer::FeedNormalizer.parse open(url)
    set(:name => feed.title, :created => Time.now, :last_checked => Time.parse("Jan 1, 1970"))
    puts "\tThe new feed is called '#{name}'"
  end
  
  # Returns all the entries for this feed
  def entries; Entry.filter(:feed_id => pk); end
  
  # Gets the most recent timestamp for any entry from this feed
  def last_time
    last = entries.order(:time.desc).first
    if last.nil?
      last_checked
    else
      last.time
    end
  end
 
  # Updates this feed so its last_checked is the most recent entry's timestamp
  def tick
    set(:last_checked => last_time)
  end
end
 
# An Entry is a single element of a Feed.
class Entry < Sequel::Model(:entries)
  set_schema do
    primary_key :id
    text :url
    text :title
    text :content
    text :description
    time :time
 
    foreign_key :feed_id, :table => :feeds
    index :url
  end
end