Permalink
Browse files

return the response object (ala request module).

Also, add foreman support for the example.
  • Loading branch information...
1 parent 698659e commit 8da95fba2112ed4af4fb6a92a7c05a88504bb4e9 @smurthas committed Oct 4, 2012
Showing with 67 additions and 71 deletions.
  1. +2 −1 .gitignore
  2. +1 −0 Procfile
  3. +16 −18 example/test.js
  4. +48 −52 lib/fitbit_client.js
View
@@ -1 +1,2 @@
-node_modules
+node_modules
+.env
View
@@ -0,0 +1 @@
+web: node example/test.js $CONSUMER_KEY $CONSUMER_SECRET
View
@@ -1,30 +1,28 @@
-var express = require('express'),
- connect = require('connect'),
- app = express.createServer(connect.bodyParser(),
+var express = require('express');
+var connect = require('connect');
+var app = express.createServer(connect.bodyParser(),
connect.cookieParser('sess'));
-var fs = require('fs');
var fitbitClient = require('../')(process.argv[2], process.argv[3]);
var token;
app.get('/', function (req, res) {
- fitbitClient.getAccessToken(req, res, function (error, newToken) {
- if(newToken) {
- token = newToken;
- res.writeHead(200, {'Content-Type':'text/html'});
- res.end('<html>Now <a href="/getStuff">get stuff</a></html>');
- }
- });
+ fitbitClient.getAccessToken(req, res, function (error, newToken) {
+ if(newToken) {
+ token = newToken;
+ res.writeHead(200, {'Content-Type':'text/html'});
+ res.end('<html>Now <a href="/getStuff">get stuff</a></html>');
+ }
+ });
});
app.get('/getStuff', function (req, res) {
- fitbitClient.apiCall('GET', '/user/-/activities/date/2011-05-25.json',
- {token: {oauth_token_secret: token.oauth_token_secret, oauth_token: token.oauth_token}},
- function(err, resp) {
- res.writeHead(200, 'application/json');
- res.end(JSON.stringify(resp));
- });
-
+ fitbitClient.apiCall('GET', '/user/-/activities/date/2011-05-25.json',
+ {token: {oauth_token_secret: token.oauth_token_secret, oauth_token: token.oauth_token}},
+ function(err, resp, json) {
+ if (err) return res.send(err, 500);
+ res.json(json);
+ });
});
app.get('/cookie', function(req, res) {
View
@@ -4,16 +4,16 @@
* Copyright (c) 2011 Simon Murtha-Smith <simon@murtha-smith.com>
*/
-var url = require("url"),
- http = require('http'),
- OAuth = require('oauth').OAuth,
- querystring = require("querystring"),
- Serializer = require('serializer');
+var url = require('url');
+var http = require('http');
+var OAuth = require('oauth').OAuth;
+var querystring = require('querystring');
+var Serializer = require('serializer');
var baseURI = 'http://api.fitbit.com/1';
module.exports = function (api_key, api_secret, callbackURI) {
- var client = {version: '0.0.2'};
+ var client = {version: '0.1.0'};
var serializer = Serializer.createSecureSerializer(api_key, api_secret);
var oAuth = new OAuth('http://api.fitbit.com/oauth/request_token',
@@ -24,67 +24,63 @@ module.exports = function (api_key, api_secret, callbackURI) {
function requestCallback(callback) {
- return function (err, data, response) {
- if (err) {
- callback(err, data);
- } else {
- try {
- callback(null, JSON.parse(data));
- } catch (exc) {
- callback(exc, data);
- }
- }
- };
+ return function (err, data, response) {
+ if (err) return callback(err, data);
+ try {
+ callback(null, response, JSON.parse(data));
+ } catch (exc) {
+ callback(exc, response, data);
+ }
+ };
}
function get(path, params, token, callback) {
- oAuth.get(baseURI + path + '?' + querystring.stringify(params),
+ oAuth.get(baseURI + path + '?' + querystring.stringify(params),
token.oauth_token, token.oauth_token_secret, requestCallback(callback));
}
function post(path, params, token, callback) {
- oAuth.post(baseURI + path, token.oauth_token, token.oauth_token_secret, params, null, requestCallback(callback));
+ oAuth.post(baseURI + path, token.oauth_token, token.oauth_token_secret,
+ params, null, requestCallback(callback));
}
// PUBLIC
client.apiCall = function (method, path, params, callback) {
- var token = params.token;
- delete params.token;
- if (method === 'GET')
- get(path, params, token, callback);
- else if (method === 'POST')
- post(path, params, token, callback);
+ var token = params.token;
+ delete params.token;
+ if (method === 'GET') get(path, params, token, callback);
+ else if (method === 'POST') post(path, params, token, callback);
}
client.getAccessToken = function (req, res, callback) {
- var sess;
- if(req.cookies && req.cookies["fitbit_client"]) {
- try { sess = serializer.parse(req.cookies["fitbit_client"]) }catch(E){ }
- }
- var qs = url.parse(req.url, true).query;
+ var sess;
+ if(req.cookies && req.cookies.fitbit_client) {
+ try { sess = serializer.parse(req.cookies.fitbit_client) }catch(E){ }
+ }
+ var qs = url.parse(req.url, true).query;
- var has_token = qs && qs.oauth_token,
- has_secret = sess && sess.token_secret;
+ var has_token = qs && qs.oauth_token,
+ has_secret = sess && sess.token_secret;
- if(has_token && has_secret) { // Access token
- oAuth.getOAuthAccessToken(qs.oauth_token,
- sess.tokenSecret,
- qs.oauth_verifier,
- function (error, oauth_token, oauth_token_secret, additionalParameters) {
- if (error) return callback(error, null);
- callback(null, {oauth_token: oauth_token, oauth_token_secret: oauth_token_secret});
- });
- } else { // Request token
- oAuth.getOAuthRequestToken({oauth_callback: callbackURI},
- function (error, oauth_token, oauth_token_secret, oauth_authorize_url, additionalParameters) {
- if (error) return callback(error, null);
- res.cookie('fitbit_client',
- serializer.stringify({token_secret:oauth_token_secret}),
- { path: '/', httpOnly: false }); // stash the secret
- res.redirect("http://www.fitbit.com/oauth/authorize?oauth_token=" + oauth_token);
- });
- }
- };
+ if(has_token && has_secret) { // Access token
+ oAuth.getOAuthAccessToken(qs.oauth_token,
+ sess.tokenSecret,
+ qs.oauth_verifier,
+ function (error, oauth_token, oauth_token_secret, additionalParameters) {
+ if (error) return callback(error, null);
+ callback(null, {oauth_token: oauth_token, oauth_token_secret: oauth_token_secret});
+ });
+ } else { // Request token
+ oAuth.getOAuthRequestToken({oauth_callback: callbackURI},
+ function (error, oauth_token, oauth_token_secret, oauth_authorize_url, additionalParameters) {
+ if (error) return callback(error, null);
+ res.cookie('fitbit_client',
+ serializer.stringify({token_secret:oauth_token_secret}),
+ { path: '/', httpOnly: false }); // stash the secret
+ res.redirect('http://www.fitbit.com/oauth/authorize?oauth_token=' + oauth_token);
+ });
+ }
+ };
- return client;
+ return client;
};

0 comments on commit 8da95fb

Please sign in to comment.