Module: Rutt::DB::Feed
Instance Method Summary collapse
- #add(url, refresh = true) ⇒ Object
- #all ⇒ Object
- #delete(feed) ⇒ Object
- #get(id) ⇒ Object
- #make_table! ⇒ Object
- #mark_as_read(feed) ⇒ Object
- #refresh ⇒ Object
- #refresh_for(feed) ⇒ Object
- #unread(feed_id) ⇒ Object
Instance Method Details
#add(url, refresh = true) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/rutt/db/feed.rb', line 19 def add(url, refresh=true) $db.execute("insert or ignore into feeds (url) values ('#{url}')") $db.execute("select * from feeds where id = (select last_insert_rowid())") do |feed| refresh_for(feed) end if refresh end |
#all ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/rutt/db/feed.rb', line 35 def all $db.execute(%{ select f.*, (select count(*) from items iu where iu.feed_id = f.id) as num_items, (select count(*) from items ir where ir.read = 0 and ir.feed_id = f.id) as unread from feeds f where unread > 0 order by id desc }) end |
#delete(feed) ⇒ Object
30 31 32 33 |
# File 'lib/rutt/db/feed.rb', line 30 def delete(feed) $db.execute("delete from items where feed_id = ?", feed['id']) $db.execute("delete from feeds where id = ?", feed['id']) end |
#get(id) ⇒ Object
26 27 28 |
# File 'lib/rutt/db/feed.rb', line 26 def get(id) end |
#make_table! ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rutt/db/feed.rb', line 6 def make_table! $db.execute(%{ create table if not exists feeds ( id integer PRIMARY KEY, title text, url text, update_interval integer default 3600, created_at NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at NOT NULL DEFAULT CURRENT_TIMESTAMP, UNIQUE(url)) }) end |
#mark_as_read(feed) ⇒ Object
70 71 72 |
# File 'lib/rutt/db/feed.rb', line 70 def mark_as_read(feed) $db.execute("update items set read = 1 where feed_id = ?", feed['id']) end |
#refresh ⇒ Object
44 45 46 47 48 49 |
# File 'lib/rutt/db/feed.rb', line 44 def refresh feeds = $db.execute("select * from feeds") results = Parallel.map(feeds, :in_threads => 8) do |feed| refresh_for(feed) end end |
#refresh_for(feed) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rutt/db/feed.rb', line 51 def refresh_for(feed) content = open(feed['url']).read rss = FeedParser::Feed::new(content) puts rss.title $db.execute("update feeds set title = ? where id = ?", rss.title, feed['id']) rss.items.each do |item| $db.execute("insert or ignore into items (feed_id, title, url, published_at) values (?, ?, ?, ?)", feed['id'], item.title, item.link, item.date.to_i) end rescue Exception => e # no-op end |
#unread(feed_id) ⇒ Object
66 67 68 |
# File 'lib/rutt/db/feed.rb', line 66 def unread(feed_id) $db.execute("select * from items where feed_id = ? and read = 0", feed) end |