Permalink
Browse files

1.0.0

  • Loading branch information...
1 parent 106e408 commit 43a441aee852d5eaca003c1c9e698fafb21f9d04 @masylum committed Jun 5, 2011
Showing with 159 additions and 170 deletions.
  1. +1 −0 .gitignore
  2. +6 −0 Makefile
  3. +45 −49 Readme.md
  4. +47 −52 lib/facebook_client.js
  5. +6 −3 package.json
  6. +0 −48 test/client.js
  7. +0 −10 test/views/client.jade
  8. +0 −2 test/views/done.jade
  9. +0 −6 test/views/layout.jade
  10. +54 −0 tests/facebook_client.js
View
@@ -0,0 +1 @@
+node_modules
View
@@ -0,0 +1,6 @@
+NODE = node
+
+test:
+ @$(NODE) tests/facebook_client.js
+
+.PHONY: test
View
@@ -1,64 +1,60 @@
# facebook-js
-Easy peasy facebook client for connect.
+Minimalistic facebook API client
- npm install facebook-js
+``` bash
+npm install facebook-js
+```
## Usage
-facebook-js has three methods.
+facebook-js has just three methods.
- * `getAuthorizeUrl(_client_id_, _redirect_uri_, _options_)` Gets the url to facebook.
- * `getAccesToken(_params_, _callback_)`: Uses oAuth module to retrieve the access_token
- * `apiCall(_http_method_, _path_, _params_, _callback_)`: Does a call to facebook graph API.
+ * `getAuthorizeUrl(options)`: Returns the authorize url.
+ * `getAccessToken(key, secret, code, redirect_uri, callback[error, access_token, refresh_token])`: Uses oAuth module to callback the access_token.
+ * `apiCall(http_method, path, params, callback[error, response, body])`: Does a call to facebook graph API and callbacks when its done.
## Example using express.js
- var express = require('express'),
- connect = require('connect'),
- facebookClient = require('./../')(
- 'appID',
- 'appSecret'
- ),
- app = express.createServer(
- connect.bodyDecoder(),
- connect.cookieDecoder(),
- connect.session()
- );
-
- app.get('/', function (req, res) {
- res.redirect(facebookClient.getAuthorizeUrl({
- client_id: 'appID',
- redirect_uri: 'http://yourhost.com:3003/auth',
- scope: 'offline_access,publish_stream'
- }));
- });
-
- app.get('/auth', function (req, res) {
- facebookClient.getAccessToken({redirect_uri: 'http://yourhost.com:3003/auth', code: req.param('code')}, function (error, token) {
- res.render('client.jade', {locals: {token: token}});
- });
- });
-
- app.post('/message', function (req, res) {
- facebookClient.apiCall('POST', '/me/feed',
- {access_token: req.param('access_token'), message: req.param('message')},
- function (error, result) {
- res.render('done.jade');
- }
- );
- });
-
- app.listen(3003);
+``` javascript
+var express = require('express')
+ , fb = require('facebook-js')
+ , app = express.createServer(
+ express.bodyDecoder()
+ , express.cookieDecoder()
+ , express.session()
+ );
+
+app.get('/', function (req, res) {
+ res.redirect(fb.getAuthorizeUrl({
+ client_id: 'appID',
+ redirect_uri: 'http://yourhost.com:3003/auth',
+ scope: 'offline_access,publish_stream'
+ }));
+});
+
+app.get('/auth', function (req, res) {
+ fb.getAccessToken('appID', 'appSecret', req.param('code'), 'http://yourhost.com:3003/auth'}, function (error, access_token, refresh_token) {
+ res.render('client', {access_token: access_token, refresh_token: refresh_token});
+ });
+});
+
+app.post('/message', function (req, res) {
+ fb.apiCall('POST', '/me/feed',
+ {access_token: req.param('access_token'), message: req.param('message')},
+ function (error, response, body) {
+ res.render('done', {body: body});
+ }
+ );
+});
+
+app.listen(3000);
+```
## Test
To test and see this module working:
- * Install the module: `npm install facebook-js`
- * Clone this repo and open the test folder
- * Add a host to your hosts file `127.0.0.1 yourhost.com`
- * Create a facebook app with the url pointing to `http://yourhost.com:3003/`
- * Set up the appID and the appSecret of your facebook app on the client.js file
- * Run it! `node test/client.js`
- * Open your browser at `yourhost.com:3003`
+``` bash
+make
+```
@@ -2,60 +2,55 @@ var querystring = require('querystring')
, OAuth = require("oauth").OAuth2
, request = require('request');
-module.exports = function () {
- var CLIENT = {}
- , _facebook_graph_url = 'https://graph.facebook.com';
+var CLIENT = {}
+ , _facebook_graph_url = 'https://graph.facebook.com';
- /* gets the authorize url
- *
- * @param {Object} options
- * @return {String}
- */
- CLIENT.getAuthorizeUrl = function (options) {
- options = options || {};
- return _facebook_graph_url + '/oauth/authorize?' + querystring.stringify(options);
- };
-
- /* Does an API call to facebook and callbacks
- * when the result is available.
- *
- * @param {String} method
- * @param {String} path
- * @param {Object} params
- * @param {Function} callback
- */
- CLIENT.apiCall = function (method, path, params, callback) {
- CLIENT.apiRequest(method, path, params).on('end', callback);
- };
+/* gets the authorize url
+ *
+ * @param {Object} options
+ * @return {String}
+ */
+CLIENT.getAuthorizeUrl = function (options) {
+ options = options || {};
+ return _facebook_graph_url + '/oauth/authorize?' + querystring.stringify(options);
+};
- /* Does an API call to facebook and returns
- * the request stream.
- *
- * @param {String} method
- * @param {String} path
- * @param {Object} params
- * @return {Stream}
- */
- CLIENT.apiRequest = function (method, path, params) {
- return request({
- uri: _facebook_graph_url + path + '?' + querystring.stringify(params)
- , method: method
- });
- };
+/* Does an API call to facebook and callbacks
+ * when the result is available.
+ *
+ * @param {String} method
+ * @param {String} path
+ * @param {Object} params
+ * @param {Function} callback
+ * @return {Request}
+ */
+CLIENT.apiCall = function (method, path, params, callback) {
+ callback = callback || function () {};
- /* Does an API call to facebook and returns
- * the request stream.
- *
- * @param {String} key
- * @param {String} secret
- * @param {String} code
- * @param {String} redirect_uri
- * @param {Function} callback
- */
- CLIENT.getAccessToken = function (key, secret, code, redirect_uri, callback) {
- var oAuth = new OAuth(key, secret, _facebook_graph_url);
- oAuth.getOAuthAccessToken(code, {redirect_uri: redirect_uri}, callback);
- };
+ return request({
+ method: method
+ , uri: _facebook_graph_url + path + '?' + querystring.stringify(params)
+ }, function (error, response, body) {
+ try {
+ callback(error, response, JSON.parse(body));
+ } catch (e) {
+ callback(e);
+ }
+ });
+};
- return CLIENT;
+/* Does an API call to facebook and returns
+ * the request stream.
+ *
+ * @param {String} key
+ * @param {String} secret
+ * @param {String} code
+ * @param {String} redirect_uri
+ * @param {Function} callback
+ */
+CLIENT.getAccessToken = function (key, secret, code, redirect_uri, callback) {
+ var oAuth = new OAuth(key, secret, _facebook_graph_url);
+ oAuth.getOAuthAccessToken(code, {redirect_uri: redirect_uri}, callback);
};
+
+module.exports = CLIENT;
View
@@ -1,14 +1,17 @@
{
"name": "facebook-js",
- "description": "Minimalistic facebook API module",
- "version": "0.1.0",
+ "description": "Minimalistic facebook API client",
+ "version": "1.0.0",
"author": "Pau Ramon <masylum@gmail.com>",
"keywords": ["facebook"],
"main" : "lib/facebook_client.js",
"directories" : { "lib" : "./lib" },
"dependencies": {
"oauth": ">= 0.8.2",
- "request": "1.9.5"
+ "request": "1.9.6"
+ },
+ "devDependencies": {
+ "testosterone": "1.2.0"
},
"repository" : {"type": "git" , "url": "http://github.com/masylum/facebook-js.git" },
"engines": { "node": ">= 0.4.0" }
View
@@ -1,48 +0,0 @@
-var express = require('express'),
- connect = require('connect'),
- facebookClient = require('facebook-js')(
- 'appID',
- 'appSecret'
- ),
- app = express.createServer(
- connect.bodyDecoder(),
- connect.cookieDecoder(),
- connect.session()
- );
-
-app.set('views', __dirname);
-
-app.get('/', function (req, res) {
- res.redirect(facebookClient.getAuthorizeUrl({
- client_id: 'appID',
- redirect_uri: 'http://yourhost.com:3003/auth',
- scope: 'offline_access,publish_stream'
- }));
-});
-
-app.get('/auth', function (req, res) {
- facebookClient.getAccessToken({redirect_uri: 'http://yourhost.com:3003/auth', code: req.param('code')}, function (error, token) {
- res.render('client.jade', {
- layout: false,
- locals: {
- token: token
- }
- });
- });
-});
-
-app.post('/message', function (req, res) {
- facebookClient.apiCall(
- 'POST',
- '/me/feed',
- {access_token: req.param('access_token'), message: req.param('message')},
- function (error, result) {
- console.log(error);
- console.log(result);
- res.render('done.jade', {layout: false});
- }
- );
-});
-
-app.listen(3003);
-
@@ -1,10 +0,0 @@
-h1 Test, facebook-js
-form(action: '/message', method: 'post')
- label token
- input(name: 'access_token', value: token && token.access_token)
- br
- label message
- textarea(name: 'message')
- br
- input(type: 'submit') Post
-
@@ -1,2 +0,0 @@
-h1 Message published succesfully
-a(href: '/') back
@@ -1,6 +0,0 @@
-!!!
-html
- head
- title example
- body
- != body
@@ -0,0 +1,54 @@
+var testosterone = require('testosterone')({title: 'models/facebook'})
+ , assert = testosterone.assert
+ , querystring = require('querystring')
+ , facebook_client = require('../')
+ , _access_token = '2227470867|2.AQCFxOjuFBkTnTM0.3600.1307314800.0-758964068|VAwjSdyKcGET3LwLWUBdAjwgZnA'
+ , _id = '167896963275275'
+ , _callback = 'example.com/done';
+
+testosterone
+
+ .add('`getAuthorizeUrl`', function (done) {
+ var url = facebook_client.getAuthorizeUrl({ client_id: _id
+ , redirect_uri: _callback
+ , scope: 'offline_access,publish_stream'
+ });
+
+ assert.equal(url, 'https://graph.facebook.com/oauth/authorize?client_id='
+ + _id + '&redirect_uri=' + querystring.escape(_callback)
+ + '&scope=' + querystring.escape('offline_access,publish_stream')
+ );
+ done();
+ })
+
+ .add('`apiCall` ok', function (done) {
+ facebook_client.apiCall(
+ 'GET'
+ , '/search'
+ , { access_token: _access_token
+ , type: 'place'
+ , center: '35.6869444,-105.9372222'
+ , distance: 5
+ }
+ , function (error, response, body) {
+ assert.equal(error, null);
+ assert.equal(response.statusCode, 200);
+ assert.equal(body.data[0].name, 'Museum of Contemporary Native Arts');
+ done();
+ }
+ );
+ })
+
+ .add('`apiCall` error', function (done) {
+ facebook_client.apiCall(
+ 'FOO'
+ , '/inexistant_url'
+ , {}
+ , function (error, response, body) {
+ assert.ok(error);
+ done();
+ }
+ );
+ })
+
+ .run();

0 comments on commit 43a441a

Please sign in to comment.