Browse files

Initial commit.

Note: Test is currently a WIP.
  • Loading branch information...
0 parents commit fd62ad77793eca59562b405faaebf6419b3dce99 @dawnerd committed Jun 22, 2011
Showing with 201 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +19 −0 package.json
  3. +56 −0 rdio.js
  4. +77 −0 test/app.js
  5. +8 −0 test/package.json
  6. +8 −0 test/public/stylesheets/style.css
  7. +21 −0 test/test/app.test.js
  8. +4 −0 test/views/index.jade
  9. +6 −0 test/views/layout.jade
2 .gitignore
@@ -0,0 +1,2 @@
+._*
+node_modules
19 package.json
@@ -0,0 +1,19 @@
+{
+ "name": "rdio",
+ "description": "API for rdio",
+ "keywords": ["rdio", "music", "api"],
+ "version": "1.0.0",
+ "homepage": "https://github.com/dawnerd/node-rdio",
+ "authors": ["Troy Whiteley <troy@somanyscientists.com>"],
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/dawnerd/node-rdio.git"
+ },
+ "dependencies": {
+ "oauth": ">= 0.9.x"
+ },
+ "scripts" : {
+ "test": "node ./test/app.js"
+ },
+ "main": "rdio.js"
+}
56 rdio.js
@@ -0,0 +1,56 @@
+/***
+ *
+ * | |o
+ * ,---.,---.,---|,---. ,---.,---|.,---.
+ * | || || ||---'---| | ||| |
+ * ` '`---'`---'`---' ` `---'``---'
+ *
+ * Author: Troy Whiteley <troy@somanyscientists.com>
+ * Homepage: http://github.com/dawnerd/
+ * License: CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
+ *
+ *
+ * Config options:
+ * rdio_api_key
+ * rdio_api_shared
+ * callback_url - URL user will be redirected to upon login
+ */
+
+module.exports = function(config, oauth) {
+ if(typeof oauth == 'undefined') {
+ var oauth = require('oauth').OAuth;
+ }
+
+ config.rdio_oauth_request = 'http://api.rdio.com/oauth/request_token';
+ config.rdio_oauth_access = 'http://api.rdio.com/oauth/access_token';
+ config.rdio_oauth_auth = 'https://www.rdio.com/oauth/authorize?oauth_token=';
+ config.rdio_api = 'http://api.rdio.com/1/';
+
+ //setup oauth
+ var oa = new OAuth(
+ config.rdio_oauth_request,
+ config.rdio_oauth_access,
+ config.rdio_api_key,
+ config.rdio_api_shared,
+ "1.0", config.callback_url, "HMAC-SHA1");
+
+ //public methods
+ return {
+ getRequestToken: function(callback) {
+ oa.getOAuthRequestToken(callback);
+ },
+ getAccessToken: function(auth_token, auth_token_secret, oauth_verifier, callback) {
+ oa.getOAuthAccessToken(auth_token, auth_token_secret, oauth_verifier, callback);
+ },
+ api: function(auth_token, auth_token_secret, data, callback) {
+ oa.post(
+ config.rdio_api,
+ auth_token,
+ auth_token_secret,
+ data,
+ null,
+ callback
+ );
+ }
+ };
+};
77 test/app.js
@@ -0,0 +1,77 @@
+
+/**
+ * Module dependencies.
+ */
+
+var express = require('express');
+
+var app = module.exports = express.createServer();
+
+// Configuration
+
+app.configure(function(){
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'jade');
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(app.router);
+ app.use(express.static(__dirname + '/public'));
+});
+
+app.configure('development', function(){
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+});
+
+app.configure('production', function(){
+ app.use(express.errorHandler());
+});
+
+app.use(express.logger());
+app.use(express.bodyParser());
+app.use(express.cookieParser());
+app.use(express.session({
+ secret: "98yKGKgkdrg94tnkfdh"
+}));
+
+app.dynamicHelpers({
+ session: function(req, res){
+ return req.session;
+ }
+});
+
+//load rdio
+var rdio = require('rdio')({
+ rdio_api_key: '8nfujzs8m5td6wy5cquap8vb',
+ rdio_api_shared: 'FajApfbYva',
+ callback_url: 'http://localhost:3000/oauth/callback'
+})
+
+// Routes
+app.get('/', function(req, res){
+ res.render('index', {
+ title: 'node-rdio'
+ });
+});
+
+app.get('/oauth/login', function(req, res, params) {
+ if (!req.session.oauth_access_token) {
+ oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results) {
+ if (error) {
+ console.log('error');
+ console.log(error);
+ } else {
+ // store the tokens in the session
+ req.session.oauth_token = oauth_token;
+ req.session.oauth_token_secret = oauth_token_secret;
+
+ // redirect the user to authorize the token
+ res.redirect(ct.config.rdio_oauth_auth + oauth_token);
+ }
+ });
+ } else {
+ res.redirect("/");
+ }
+});
+
+app.listen(3000);
+console.log("To test node-rdio, open a browser to http://localhost:%d", app.address().port);
8 test/package.json
@@ -0,0 +1,8 @@
+{
+ "name": "node-rdio-test"
+ , "version": "0.0.1"
+ , "private": true
+ , "dependencies": {
+ "jade": ">= 0.0.1"
+ }
+}
8 test/public/stylesheets/style.css
@@ -0,0 +1,8 @@
+body {
+ padding: 50px;
+ font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
+}
+
+a {
+ color: #00B7FF;
+}
21 test/test/app.test.js
@@ -0,0 +1,21 @@
+
+// Run $ expresso
+
+/**
+ * Module dependencies.
+ */
+
+var app = require('../app')
+ , assert = require('assert');
+
+
+module.exports = {
+ 'GET /': function(){
+ assert.response(app,
+ { url: '/' },
+ { status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' }},
+ function(res){
+ assert.includes(res.body, '<title>Express</title>');
+ });
+ }
+};
4 test/views/index.jade
@@ -0,0 +1,4 @@
+- if(!session.oauth_access_token)
+ a(href: '/oauth/login') Click here to login
+- else
+
6 test/views/layout.jade
@@ -0,0 +1,6 @@
+!!!
+html
+ head
+ title= title
+ link(rel='stylesheet', href='/stylesheets/style.css')
+ body!= body

0 comments on commit fd62ad7

Please sign in to comment.