-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
183 lines (148 loc) · 4.8 KB
/
app.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require "sinatra"
require "oauth"
require "oauth/consumer"
require 'grackle'
enable :sessions
require 'lib/logic'
include Ear
# TODO: In order to go about 5000 for the popularity limit, I need to
# add paging (cursor) for the follower_ids
# TODO: To speed this up, I should investigate ways to retrieve more
# than one user's profile information at a time
POPULARITY_LIMIT = 5000
DEFAULT_TTL = 72000
configure do
require 'memcached'
CACHE = Memcached.new
end
# If you've logged in, initalize your data
####################
before do
session[:oauth] ||= {}
consumer_key = ENV["consumer_key"]
consumer_secret = ENV["consumer_secret"]
@consumer ||= OAuth::Consumer.new(consumer_key, consumer_secret, :site => "http://twitter.com")
if !session[:oauth][:request_token].nil? && !session[:oauth][:request_token_secret].nil?
@request_token = OAuth::RequestToken.new(@consumer, session[:oauth][:request_token], session[:oauth][:request_token_secret])
end
if !session[:oauth][:access_token].nil? && !session[:oauth][:access_token_secret].nil?
@access_token = OAuth::AccessToken.new(@consumer, session[:oauth][:access_token], session[:oauth][:access_token_secret])
end
if @access_token
@client = Grackle::Client.new(:auth => {
:type => :oauth,
:consumer_key => consumer_key,
:consumer_secret => consumer_secret,
:token => @access_token.token,
:token_secret => @access_token.secret
})
end
end
# Some of the language used on the results page
####################
helpers do
def following_me_status(following)
if following
"<div class='following_me_status' id='follows_yes'>This person follows you.</div>"
else
"<div class='following_me_status' id='follows_no'>This person does <strong>not</strong> follow you.</div>"
end
end
def results_cloud_intro(joined_size, following)
if following and joined_size == 0
"But you do not have any other mutual followers"
elsif following and joined_size == 1
"And you have <span class='cloud_hi'>1 mutual follower</span>"
elsif following
"And you have <span class='cloud_hi'>#{joined_size} mutual followers</span>"
elsif joined_size == 0
"And you do not have any mutual followers"
elsif joined_size == 1
"But you do have <span class='cloud_hi'>1 mutual follower</span>"
else
"But you do have <span class='cloud_hi'>#{joined_size} mutual followers</span>"
end
end
end
# The URL map for Sinatra is below
##########################
get '/' do
if @access_token
load_user_info
erb :home
else
erb :start
end
end
post '/user' do
if params[:username].blank?
@access_token = nil
erb :start
else
user_obj = lookup_user_on_twitter(params[:username].downcase)
if too_popular(user_obj)
@popular_user = params[:username] || "You"
erb :selfpopularity
redirect '/'
end
end
end
get '/show' do
load_user_info
@otheruser = params[:otheruser] || ""
if (@otheruser.downcase == @user_name.downcase)
@error = "That's you! It takes two to have a conversation."
redirect '/'
end
other_user_obj = lookup_user_on_twitter(@otheruser)
if other_user_obj.nil?
@error = "Couldn't find that user."
redirect '/'
end
# check to make sure other user is not too cool for school
if too_popular(other_user_obj)
erb :popularity
else
my_follows = get_follower_info(@user_name)
other_follows = get_follower_info(@otheruser)
if (my_follows == false) or (my_follows.empty?)
@error = "That username does not seem to have any followers."
redirect '/'
end
if (other_follows == false) or (other_follows.empty?)
@error = "Twitter choked on that username. Please try again."
redirect '/'
end
joined_ids = mutual_follower_ids(my_follows, other_follows)
@joined = populate_mutual_followers(joined_ids)
@following = do_they_follow_you(@otheruser, @user_id)
erb :results
end
end
post '/show' do
redirect '/' if params[:otheruser].empty?
redirect "/show?otheruser=#{params[:otheruser]}"
end
get '/about' do
erb :about
end
## These URLs implement Oauth login for Twitter
###########################
get "/request" do
@request_token = @consumer.get_request_token(:oauth_callback => "http://#{request.host}/auth")
session[:oauth][:request_token] = @request_token.token
session[:oauth][:request_token_secret] = @request_token.secret
redirect @request_token.authorize_url
end
get "/auth" do
@access_token = @request_token.get_access_token :oauth_verifier => params[:oauth_verifier]
session[:oauth][:access_token] = @access_token.token
session[:oauth][:access_token_secret] = @access_token.secret
redirect "/"
end
get "/logout" do
response.delete_cookie("user_info")
@access_token = nil
session[:oauth] = {}
erb :start
end