forked from awendt/headhunter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
headhunter.rb
62 lines (53 loc) · 1.57 KB
/
headhunter.rb
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
require 'rubygems'
require 'sinatra'
require 'net/http'
require 'json'
require 'twitter'
configure do
require 'memcached'
CACHE = Memcached.new
end
DEFAULT_AVATAR = "http://static.twitter.com/images/default_profile_normal.png"
helpers do
def avatar_valid?(url)
Twitter.head(url).code == "200"
end
def grab_avatar_for(user)
avatar_url = Twitter.get("/users/#{user}.json")["profile_image_url"]
avatar_url ||= DEFAULT_AVATAR
end
def google_chart
["http://chart.apis.google.com/chart?chs=400x250", "cht=gom", "chd=t:#{(@plot_value)}",
"chco=FF0000,FF8040,FFFF00,00FF00,00FFFF,0000FF,800080", "chxt=x,y",
"chxl=0:||1:|low|normal|plenty"].join("&")
end
def cache_for(time)
response['Cache-Control'] = "public, max-age=#{time.to_i}"
end
end
get '/favicon.ico' do
cache_for 7*24*60*60
status 404
end
get '/:user' do
cache_for 10*60
user = params[:user]
begin
avatar_url = CACHE.get(user)
raise Memcached::NotFound unless avatar_valid?(avatar_url)
rescue Memcached::NotFound
avatar_url = grab_avatar_for(user)
CACHE.set(user, avatar_url) unless avatar_url == DEFAULT_AVATAR
end
redirect avatar_url
end
get '/' do
cache_for 3*60
status = Twitter.get("/1/account/rate_limit_status.json")
@remaining_hits = status['remaining_hits'].to_i
@reset_to = status['hourly_limit']
@reset_in_minutes = (status['reset_time_in_seconds'].to_i - Time.now.to_i)/60
@plot_value = @reset_in_minutes == 0 ? 50 : (@remaining_hits * 1.0 / @reset_in_minutes * 20).round
content_type 'text/html', :charset => 'utf-8'
erb :index
end