public
Description: frth
Homepage:
Clone URL: git://github.com/bruce/frth.git
kastner (author)
Mon Nov 03 19:01:12 -0800 2008
commit  13e4473bb848516983eaa39b879ef328bfbcb7da
tree    2b07ae46c26ad491943e8c9a621be9caa7c7b63f
parent  5efc101149a80f3ea72aecb73499663de680d347
frth / front-row.rb
100644 67 lines (51 sloc) 1.245 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
64
65
66
67
$:.unshift "sinatra/lib"
 
require 'sinatra'
require 'rubygems'
require 'active_support'
require 'json'
require 'open-uri'
 
TWITTER_WAIT_TIMEOUT = 60 unless Object.const_defined?("TWITTER_WAIT_TIMEOUT")
 
before do
  # set UTF-8
  header "Content-Type" => "text/html; charset=utf-8"
 
  # set css body id
  @body_id = "home"
  
  # set up a cache object
  CACHE = MemCache.new 'localhost:11211', :namespace => 'front_row' unless Object.const_defined?("CACHE")
end
 
get "/" do
  @title = "Tweet Row to History"
  erb :index
end
 
get "/tweets" do
  CACHE["tweets"]
end
 
get "/fetch-tweets-from-twitter" do
  return CACHE["tweets"] if CACHE["last_fetch"] && (Time.now - CACHE["last_Fetch"]) < TWITTER_WAIT_TIMEOUT
  
  CACHE["last_Fetch"] = Time.now
  CACHE["tweets"] = get_tweets
  
  redirect "/"
end
 
def tweeters
  %w|wbruce damon|
end
 
def tweet_terms
%w|front|
end
 
def get_tweets
begin
tweeters.collect do |tweeter|
get_tweets_for(tweeter)
end
rescue
CACHE["tweets"] || {}
  end
end
 
def get_tweets_for(user)
  json = json_for_url(CGI.escape("from:#{user} #{tweet_terms.join(" ")}"))
  JSON.parse(json)
end
 
def json_for_url(terms)
  open(url % terms).read
end
 
def url; "http://search.twitter.com/search.json?q=%s"; end