Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Split logic into modules.

Signed-off-by: Diwank Singh Tomer <diwank.singh@gmail.com>
  • Loading branch information...
commit 391ff609194c01b1c432d30f5893b6b2c18865df 1 parent 34e393d
@creatorrr authored
View
22 src/globals.coffee
@@ -0,0 +1,22 @@
+# Helpers
+pick = (obj, keys...) ->
+ copy = {}
+ copy[key] = obj[key] for key in keys
+
+ copy
+
+extend = (dest, sources...) ->
+ for src in sources
+ dest[key] = value for own key, value of src
+
+ dest
+
+# Get config
+rdio = pick process.env, 'RDIO_CONSUMER', 'RDIO_SECRET', 'HEROKU_URL'
+misc = CALLBACK: 'auth'
+
+# Globals
+module.exports = globals = extend {}, rdio, misc
+
+unless globals.RDIO_CONSUMER and globals.RDIO_SECRET
+ throw new Error 'Invalid rdio credentials'
View
108 src/index.coffee
@@ -3,112 +3,26 @@
# search rdio for song
# External libraries
-Rdio = require 'node-rdio'
sockets = require 'hubot.io'
-# Modules
-pages = require './pages'
+# Load globals
+{CALLBACK} = require './globals'
module.exports = (robot) ->
# Initialize socket.io
- io = sockets robot
+ robot.io = sockets robot
- # Get config.
- {RDIO_CONSUMER, RDIO_SECRET, HEROKU_URL} = process.env
- throw new Error 'Invalid rdio credentials' unless RDIO_CONSUMER and RDIO_SECRET
-
- CALLBACK = 'auth'
+ # Load modules
+ pages = require './pages'
+ routes = (require './routes') robot
+ listeners = (require './listeners') robot
# Initialize this thing.
- robot.respond /init rdio/i, (msg) ->
- rdio = new Rdio [
- RDIO_CONSUMER
- RDIO_SECRET
- ]
-
- rdio.beginAuthentication HEROKU_URL+CALLBACK, (error, authUrl) ->
- if error
- robot.logger.debug error
- return msg.send "Error: #{ error }"
-
- requestToken = rdio.token[0]
- requestSecret = rdio.token[1]
-
- robot.brain.set('RdioRequestToken', requestToken)
- .set("RdioRequestSecret-#{requestToken}", requestSecret)
- .save()
-
- msg.send "Go to #{ authUrl } to verify your rdio account."
-
- robot.router.get "/#{ CALLBACK }", (req, res) ->
- res.writeHead 200,
- 'Content-Type': 'text/html'
-
- requestToken = req.query['oauth_token']
- requestSecret = robot.brain.get "RdioRequestSecret-#{requestToken}"
-
- verifier = req.query['oauth_verifier']
-
- unless requestToken and requestSecret and verifier
- return res.end pages.error
- message: 'Error: Invalid request token'
-
- rdio = new Rdio [
- RDIO_CONSUMER
- RDIO_SECRET
- ], [
- requestToken
- requestSecret
- ]
-
- rdio.completeAuthentication verifier, (error) ->
- if error
- robot.logger.debug error
- return msg.send "Error: #{ error }"
-
- accessToken = rdio.token[0]
- accessSecret = rdio.token[1]
-
- robot.brain
- .remove('RdioAccessToken')
- .remove("RdioRequestSecret-#{requestToken}")
-
- .set('RdioAccessToken', accessToken)
- .set("RdioAccessSecret-#{accessToken}", accessSecret)
- .save()
-
- res.end pages.home
- title: "Yay! Your access token is #{ accessToken }"
-
- robot.router.get '/', (req, res) ->
- res.writeHead 200,
- 'Content-Type': 'text/html'
-
- res.end pages.home
- title: 'Pataku?'
-
- robot.respond /test rdio/i, (msg) ->
- accessToken = robot.brain.get 'RdioAccessToken'
- accessSecret = robot.brain.get "RdioAccessSecret-#{accessToken}"
-
- if not accessToken and accessSecret
- return msg.send 'Please login to your rdio account first.'
-
- rdio = new Rdio [
- RDIO_CONSUMER
- RDIO_SECRET
- ], [
- accessToken
- accessSecret
- ]
-
- rdio.call 'currentUser', (error, data) ->
- if error
- robot.logger.warn "Error: #{ error }"
- return msg.send "Error: #{ error }"
-
- msg.send "Success: #{ data }"
+ robot.respond /init rdio/i, listeners.init
+ robot.respond /test rdio/i, listeners.test
+ robot.router.get '/', routes.home
+ robot.router.get "/#{ CALLBACK }", routes.auth
# client.post "#{ RDIO_API_ENDPOINT }",
# accessToken,
View
49 src/listeners.coffee
@@ -0,0 +1,49 @@
+# External libraries
+Rdio = require 'node-rdio'
+
+# Load globals
+{RDIO_CONSUMER, RDIO_SECRET, HEROKU_URL, CALLBACK} = require './globals'
+
+module.exports = listeners = (robot) ->
+ init: (msg) ->
+ rdio = new Rdio [
+ RDIO_CONSUMER
+ RDIO_SECRET
+ ]
+
+ rdio.beginAuthentication HEROKU_URL+CALLBACK, (error, authUrl) ->
+ if error
+ robot.logger.debug error
+ return msg.send "Error: #{ error }"
+
+ requestToken = rdio.token[0]
+ requestSecret = rdio.token[1]
+
+ robot.brain
+ .set('RdioRequestToken', requestToken)
+ .set("RdioRequestSecret-#{requestToken}", requestSecret)
+ .save()
+
+ msg.send "Go to #{ authUrl } to verify your rdio account."
+
+ test: (msg) ->
+ accessToken = robot.brain.get 'RdioAccessToken'
+ accessSecret = robot.brain.get "RdioAccessSecret-#{accessToken}"
+
+ if not accessToken and accessSecret
+ return msg.send 'Please login to your rdio account first.'
+
+ rdio = new Rdio [
+ RDIO_CONSUMER
+ RDIO_SECRET
+ ], [
+ accessToken
+ accessSecret
+ ]
+
+ rdio.call 'currentUser', (error, data) ->
+ if error
+ robot.logger.warn "Error: #{ error }"
+ return msg.send "Error: #{ error }"
+
+ msg.send "Success: #{ data }"
View
18 src/pages.coffee
@@ -1,7 +1,7 @@
{compile} = require 'coffeecup'
-module.exports = pages =
- home: compile ->
+pages =
+ home: ->
doctype 5
html ->
body ->
@@ -16,6 +16,18 @@ module.exports = pages =
socket = io.connect()
socket.on 'gaga', (data) -> console.log data
- error: compile ->
+ error: ->
body ->
p @message
+
+ redirect: ->
+ input {type: 'hidden', id: 'redirect', value: @redirect}
+ coffeescript ->
+ redirect = ->
+ url = (document.getElementById 'redirect').value
+ window.location.href = url
+
+ setTimeout redirect, 3000
+
+# Precompile pages and export.
+module.exports[name] = compile page for name, page of pages
View
59 src/routes.coffee
@@ -0,0 +1,59 @@
+# External libraries
+Rdio = require 'node-rdio'
+
+# Load globals
+{RDIO_CONSUMER, RDIO_SECRET} = require './globals'
+
+module.exports = routes = (robot) ->
+ home: (req, res) ->
+ res.writeHead 200,
+ 'Content-Type': 'text/html'
+
+ res.end pages.home
+ title: 'Pataku?'
+
+ auth: (req, res) ->
+ res.writeHead 200,
+ 'Content-Type': 'text/html'
+
+ requestToken = req.query['oauth_token']
+ requestSecret = robot.brain.get "RdioRequestSecret-#{requestToken}"
+
+ verifier = req.query['oauth_verifier']
+
+ unless requestToken and requestSecret and verifier
+ return res.end pages.error
+ message: 'Error: Invalid request token'
+
+ rdio = new Rdio [
+ RDIO_CONSUMER
+ RDIO_SECRET
+ ], [
+ requestToken
+ requestSecret
+ ]
+
+ rdio.completeAuthentication verifier, (error) ->
+ if error
+ robot.logger.debug error
+ return msg.send "Error: #{ error }"
+
+ oldAccessToken = robot.brain.get 'RdioAccessToken'
+
+ accessToken = rdio.token[0]
+ accessSecret = rdio.token[1]
+
+ robot.brain
+ .remove('RdioRequestToken')
+ .remove("RdioRequestSecret-#{requestToken}")
+
+ .remove('RdioAccessToken')
+ .remove("RdioAccessSecret-#{oldAccessToken}")
+
+ .set('RdioAccessToken', accessToken)
+ .set("RdioAccessSecret-#{accessToken}", accessSecret)
+ .save()
+
+ res.end pages.redirect
+ message: "Yay! Your access token is #{ accessToken }"
+ redirect: '/'
Please sign in to comment.
Something went wrong with that request. Please try again.