-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
111 lines (83 loc) · 2.85 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
require 'sinatra'
require 'net/https'
require 'json'
$LOAD_PATH << './config'
require 'AppConfig'
# Config param
CLIENT_ID = AppConfig::CLIENT_ID
CLIENT_SECRET = AppConfig::CLIENT_SECRET
REDIRECT_URI = AppConfig::REDIRECT_URI
VERSION = AppConfig::VERSION
enable :sessions
get '/' do
@url = "/authenticate"
erb :index
end
get '/authenticate' do
if session[:token]
redirect "/app"
elsif params[:code]
json = getJSON("https://foursquare.com/oauth2/access_token?client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&grant_type=authorization_code&redirect_uri=" + REDIRECT_URI + "&code=" + params[:code])
session[:token] = JSON.parse(json)['access_token']
# XXX : do i need id ?
# json = getJSON("https://api.foursquare.com/v2/users/self?oauth_token=" + token)
#puts json
# id = JSON.parse(json)['response']['user']['id']
# Store id and token in file
# File.open('usersToken.txt', 'w') do |f|
# f.puts id + " : " + token + "\n"
# end
# XXX : must i store ids and tokens ?
# as said in https://developer.foursquare.com/overview/auth
# "4. Save this access token for this user in your database."
# or use session to store token ?
redirect "/app"
else
# Authorize app
redirect "https://fr.foursquare.com/oauth2/authenticate?client_id=" + CLIENT_ID + "&response_type=code&redirect_uri=" + REDIRECT_URI
end
end
get '/app' do
if session[:token]
@venues = getJSON("https://api.foursquare.com//v2/users/self/venuehistory?oauth_token=" + session[:token])
erb :app
else
"Error: something strange happened here"
end
end
get '/test' do
if session[:token]
checkin = getJSON("https://api.foursquare.com//v2/users/self/checkins?oauth_token=" + session[:token] + "&limit=1")
nbCheckins = JSON.parse(checkin)['response']['checkins']['count'] # here i have the total number of checkins
limit = 250
checkins = []
for i in 0..(nbCheckins / limit)
result = getJSON("https://api.foursquare.com//v2/users/self/checkins?oauth_token=" + session[:token] + "&offset=#{i*250}&limit=#{limit}")
puts "Get checkins from #{i*250} to #{(i+1)*250}"
JSON.parse(result)['response']['checkins']['items'].each do |checkin|
checkins.push checkin
end
end
#puts "Checkins : #{checkins.length}"
checkinDates = []
checkins.each do |checkin|
checkin['venue']['id'] == "4beba7896295c9b684fe8708" ? (checkinDates.push checkin['createdAt']) : nil
end
checkinDates.each do |date|
puts "4beba7896295c9b684fe8708 : " + Time.at(date).to_s
end
#puts @checkins
#erb :test
# now use d3js to make nice graph
# http://square.github.com/crossfilter/
else
"Error: something strange happened here"
end
end
def getJSON(url)
uri = URI.parse(url + "&v=" + VERSION)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
return http.request(request).body
end