public
Description: Read an RSS feed (even HTTPS with authentication) and rebroadcast it to a Twitter account.
Clone URL: git://github.com/trak3r/rss2twitter.git
rss2twitter / lib / database.rb
100644 49 lines (38 sloc) 1.137 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
require 'active_record'
require 'shorturl'
 
ActiveRecord::Base.logger = Logger.new(STDERR)
ActiveRecord::Base.colorize_logging = false
 
ActiveRecord::Base.establish_connection(
    :adapter => "sqlite3",
    :dbfile => 'rss2twitter.db'
)
 
class Item < ActiveRecord::Base
 
  def to_s
    "#{self.optimized_title[0..(tweet_limit-self.short_url.length)]} #{self.short_url}"
  end
  
  protected
  
  def tweet_limit
    138 # leave one off for fudging
  end
 
  def short_url
    @cached_short_url ||= ShortURL.shorten(self.link, :tinyurl)
  end
  
  def optimized_title
    # for Trac feeds (which is why I wrote this) strip off some
    # extraneous verbiage to save every last precious character
    tidbits = self.title.scan( /^Changeset \[(.*?)\]\: (.*)/ ).flatten
    if 2 == tidbits.length
      return tidbits[1] # sprintf( "%s %s", *tidbits )
    else # not a Trac changeset or we failed to parse it
      return self.title
    end
  end
 
end
 
unless Item.table_exists?
  ActiveRecord::Schema.define do
    create_table :items do |table|
        table.column :title, :string
        table.column :link, :string
    end
  end
end