Class: Twinkies::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/twinkies/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Server



5
6
7
8
9
# File 'lib/twinkies/server.rb', line 5

def initialize(config)
  @tweet_db = config.tweet_db
  @username = config.username
  @password = config.password
end

Instance Method Details

#handle_requestObject



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
# File 'lib/twinkies/server.rb', line 33

def handle_request
  username = @username # builder must instance_eval?

  get '/feed.xml' do
    pieces = ['http://', request.env['SERVER_NAME']]
    pieces << ":#{request.env['SERVER_PORT']}" unless request.env['SERVER_PORT'].to_i == 80
    pieces << request.env['REQUEST_PATH']
    request_path = pieces.join

    builder do |xml|
      xml.instruct!
      xml.rss :version => '2.0', "xmlns:atom" => "http://www.w3.org/2005/Atom" do
        xml.channel do
          xml.title "#{username}'s twitter URL feed"
          xml.description "#{username}'s twitter URL feed"
          xml.link "http://twitter.com/#{username}"
          xml.tag! "atom:link", :rel => "self", :href => request_path

          Item.latest.each do |tweet|
            xml.item do
              xml.title "#{tweet.user} - #{tweet.text}"
              xml.link tweet.link
              xml.pubDate tweet.created_at.rfc822
              xml.guid tweet.guid, :isPermaLink => false
            end
          end
        end
      end
    end
  end
end

#runObject



11
12
13
14
15
# File 'lib/twinkies/server.rb', line 11

def run
  setup_db
  start_tweet_refresher
  handle_request
end

#setup_dbObject



17
18
19
20
# File 'lib/twinkies/server.rb', line 17

def setup_db
  DataMapper.setup(:default, "sqlite3://#{@tweet_db}")
  DataMapper.auto_upgrade!
end

#start_tweet_refresherObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/twinkies/server.rb', line 22

def start_tweet_refresher
  Thread.abort_on_exception = true
  Thread.new do
    loop do
      puts "refreshing tweets..."
      Item.refresh @username, @password
      sleep 60
    end
  end
end