forked from amrit/Sinfoursq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
133 lines (69 loc) · 2.65 KB
/
main.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
require 'rubygems'
require 'sinatra'
require 'sinatra/static_assets'
require 'json'
require 'omniauth'
require 'omniauth-foursquare'
require 'foursquare2'
require 'quimby'
#require 'sinatra/reloader'
require 'net/https'
#require 'sinatra_more/markup_plugin'
#<pre>#{JSON.pretty_generate(request.env['omniauth.auth'])}</pre>
#TODO require 'omniauth-att'
class SinatraApp < Sinatra::Base
configure do
set :sessions, true
set :public_folder, Proc.new { File.join(root, "public") }
end
use OmniAuth::Builder do
provider :foursquare, 'FQ4UOTLCSL1IU3RX3GLM3E10RDZK2EFB0I1K5BO3ZX1EAUNM','5EVMUNNAQDZDKORGOSTPPESDHZWX0ZUTLMPSXYGXYCUFCSGE',
:client_options => { :ssl => { :ca_file => "/opt/local/etc/openssl/ca-bundle.crt" } }
#provider :att, 'client_id', 'client_secret', :callback_url => (ENV['BASE_DOMAIN']
end
get '/' do
#erb :login
erb "<h1> Hello </h1>"
end
get '/auth/:provider/callback' do
omniauth = request.env['omniauth.auth']
oauth_token = omniauth['credentials']['token']
@@client = Foursquare2::Client.new(:oauth_token => oauth_token, :ssl => { :verify => OpenSSL::SSL::VERIFY_PEER, :ca_file => "/opt/local/etc/openssl/ca-bundle.crt" })
erb "
<script type=text/javascript>
jQuery(document).ready(function ($) {
$('#tabs').tab();
});
</script>
<h1><strong> Welcome, #{omniauth['info']['first_name']} ! </strong></h1>
<ul id=tabs class=nav nav-tabs data-tabs=tabs>
<li><a href=/auth/foursquare/callback data-toggle=tab>Venues</a></li>
<li><a href=/campaign data-toggle=tab>Campaigns</a></li>
<li><a href=/special data-toggle=tab>Specials</a></li>
</ul>
<h2> Managed Venues </h2>
<pre>#{JSON.pretty_generate(@@client.managed_venues)} </pre>
"
end
post '/auth/:provider/callback' do
require 'foursquare2'
erb "<p> #{@@client.user_friends('self', options = {:limit => params[:lim] }) } </p>"
end
get '/auth/failure' do
erb "<h1>Authentication Failed:</h1><h3>message:<h3> <pre>#{params}</pre>"
end
get '/auth/:provider/deauthorized' do
erb "#{params[:provider]} has deauthorized this app."
end
get '/protected' do
throw(:halt, [401, "Not authorized\n"]) unless session[:authenticated]
erb "<pre>#{request.env['omniauth.auth'].to_json}</pre><hr>
<a href='/logout'>Logout</a>"
end
get '/logout' do
session[:authenticated] = false
redirect '/'
end
end
SinatraApp.run! if __FILE__ == $0
__END__