-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.coffee
116 lines (101 loc) · 3.02 KB
/
app.coffee
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
# Module dependencies.
express = require('express')
oauth = require('oauth')
helpers = require('./helpers').helpers
app = express()
rkOptions = exports.options =
client_id : process.env.CLIENT_ID,
client_secret : process.env.CLIENT_SECRET,
auth_url : 'https://runkeeper.com/apps/authorize',
access_token_url : 'https://runkeeper.com/apps/token',
redirect_uri : 'http://uva.herokuapp.com/runkeeper_callback',
app.configure('development', ->
# Not sure if this is kosher, but I want to override the base redirect_uri
# for the development environment. Happy to receive corrections :)
rkOptions.redirect_uri = 'http://localhost:3000/runkeeper_callback'
)
runkeeper = require(__dirname + '/runkeeper.js')
client = new runkeeper.HealthGraph(rkOptions)
calendar = require(__dirname + '/calendar_display.js')
consumer = ->
new oauth.OAuth2(
rkOptions.client_id,
rkOptions.client_secret,
'http://runkeeper.com',
'/apps/authorize',
'/apps/token')
# Configuration
app.configure(->
app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
app.use(express.logger())
app.use(express.bodyParser())
app.use(express.cookieParser())
app.use(express.static(__dirname + '/public'))
)
app.engine('jade', require('jade').__express)
app.configure('development', ->
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
app.use(express.session(
secret: 'sdofyi234oglkc@oydf'
))
)
app.configure('production', ->
app.use(express.errorHandler())
app.use(express.session(
secret: 'sdofyi234oglkc@oydf'
))
)
# Routes
app.get('/', (req, res) ->
res.render('index')
)
app.get('/runkeeper_login', (req, res) ->
oa = consumer()
res.redirect(oa.getAuthorizeUrl(
response_type: 'code',
redirect_uri : rkOptions.redirect_uri
))
)
app.get('/runkeeper_callback', (req, res) ->
client.getNewToken(req.param('code'), (access_token) ->
req.session.access_token = access_token
client.access_token = access_token
res.redirect('/calendar')
)
)
app.get('/calendar', (req, res) ->
# Early return in case the access_token isn't set
if (!req.session.access_token)
res.redirect('/')
return
errors = []
client.profile((profile) ->
fitnessActivities = []
profileInfo = {}
try
profileInfo = JSON.parse(profile)
catch error
errors.push(error)
client.fitnessActivityFeed((activities) ->
try
fitnessActivities = JSON.parse(activities)['items']
calDisplay = new calendar.CalendarDisplay(fitnessActivities)
catch error
console.log(error)
errors.push(error)
res.render('calendar',
title : 'calendar data!'
user : profileInfo
activities : calDisplay.activitiesByMonth()
calData : calDisplay.getElts()
errors : errors
h : helpers
)
)
)
)
port = process.env.PORT || 3000
app.listen(port, ->
console.log("Express server listening on port %d in %s mode", port, app.settings.env)
)