-
Notifications
You must be signed in to change notification settings - Fork 1
/
fourrific.rb
81 lines (60 loc) · 1.65 KB
/
fourrific.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
begin
# Try to require the preresolved locked set of gems.
require File.expand_path('../.bundle/environment', __FILE__)
rescue LoadError
# Fall back on doing an unlocked resolve at runtime.
require "rubygems"
require "bundler"
Bundler.setup
end
require 'sinatra'
Bundler.require
require File.dirname(__FILE__) + '/base'
enable :sessions
get '/' do
@ip = @env['REMOTE_ADDR']
if params[:oauth_token]
h = Fourrific::Authorize.new
t = h.access_token(params[:oauth_token],session[:request_secret], params[:oauth_verifier])
session[:token] = t[:token]
session[:secret] = t[:secret]
#if you reload the page with the oauth param, you 500, better to redirect and kill the possibility
redirect '/'
elsif session[:token].nil?
redirect '/login'
end
@g = Fourrific::IPGeocode.new(@ip)
@city = @g.you_are_in
begin
c = Fourrific::Checkins.new(session[:token],session[:secret])
@c = c.friends(@g)
if c.error && c.error.length > 0
raise c.error
end
end
if c.first_is_far?
@first_is_far = true
end
erb :index
end
get '/login' do
h = Fourrific::Authorize.new
t = h.get_tokens
session[:request_token] = t[:request_token]
session[:request_secret] = t[:secret]
@url = t[:url]
redirect "#{@url}"
erb :login
end
error do
@msg = env['sinatra.error']
erb :unauthorized
end
error 404 do
@msg = "404. That page doesn't exist. <a href='/'>Go home</a>."
erb :unauthorized
end
error OAuth::Unauthorized do
@msg = "For some reason, I can't log you into foursquare. Try clearing your cache and cookies, and <a href='/'>starting over</a>."
erb :unauthorized
end