Skip to content

Commit

Permalink
1. Store expiration timestamp of access token.
Browse files Browse the repository at this point in the history
2. Implement basic command line handling mechinism.
  • Loading branch information
kk1fff committed Apr 2, 2012
1 parent e446f03 commit 3c3ccea
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
13 changes: 12 additions & 1 deletion src/config.js
Expand Up @@ -19,7 +19,18 @@ var app_cfg = {
'fb_auth_scope' : '',

// Default configure file name
'default_config_file' : HOME() + '/.0xfb'
'default_config_file' : HOME() + '/.0xfb',

// Available program options
// 'option name' : (true if it has a value)
// Ex:
// If you expect user to type "-p 60"
// you should set
// '-p' : true
'program_options' : {
'--website' : true, // Example
'--silence' : false
}

};

Expand Down
27 changes: 19 additions & 8 deletions src/fb_oauth_client.js
Expand Up @@ -59,14 +59,25 @@ var getTokenWithUsersHelp = function(success_callback,
exports.getAuthToken = function(success_callback,
failure_callback) {
var USER_CONFIG = require('./config.js').user_config();
if (USER_CONFIG.hasOwnProperty('fb_auth_token')) {
success_callback(USER_CONFIG['fb_auth_token'], 0);

if (USER_CONFIG.hasOwnProperty('fb_auth_token')
&& USER_CONFIG['fb_auth_token_expire'] > Date.now()/1000) {
// Success callback
success_callback(USER_CONFIG['fb_auth_token'],
USER_CONFIG['fb_auth_token_expire']);
} else {
getTokenWithUsersHelp(function(access_token, expires_in) { // When success.
USER_CONFIG['fb_auth_token'] = access_token;
USER_CONFIG['fb_auth_token_expire'] =
Date.now() / 1000 + expires_in;
success_callback(access_token, expires_in); // To caller's callback
}, failure_callback);
getTokenWithUsersHelp(
function(access_token, expires_in) { // When success.

// Record to user config.
USER_CONFIG['fb_auth_token'] = access_token;
USER_CONFIG['fb_auth_token_expire'] =
parseInt(Date.now()/1000) + parseInt(expires_in);

// Success callback
success_callback(USER_CONFIG['fb_auth_token'],
USER_CONFIG['fb_auth_token_expire']);
},
failure_callback);
}
};
57 changes: 52 additions & 5 deletions src/index.js
@@ -1,8 +1,55 @@
// Command format:
// ./0xfb [Program option] <Command> [Command argument]


var CONFIG = require('./config.js').app_config();

// prog_command:
// Map command to module that run the command.
// Each module must has run() function. And the first argument of this
// function would be program option, while the second one would be
// command argument. Move to ./config.js ?
var progCommand = {
// Update photo
'uploadphoto' : './upload_photo.js'
};

// It is a chance for program to make global setting before and command
// is issued. Command will be executed right after this step.
var handleSetting = function(progOpt, command, cmdArg) {
// So far it's empty XD.
}

// Parse command line, collect program option, command, command argrment.
var initRun = function() {

var program_option = [];
var command = '';
var command_argument = [];

// [TODO Patrick] Here I assume there is no program options, so we just
// use that first argument as command

command = process.argv[2];
command_argument = process.argv.slice(3);

if (!progCommand.hasOwnProperty(command)) {
// Invalid command.
process.exit(1);
}

handleSetting(program_option, command, command_argument);

// Go
require(progCommand[command]).run(program_option, command_argument);
}

// *** Start with fetching access token ***
var fbOauthClient = require('./fb_oauth_client.js');
fbOauthClient.getAuthToken(function(access_token, expires) {
// Debug !!
console.log(" Access token: %s", access_token);
console.log(" Expire Time: %d, now: %d", expires, parseInt(Date.now()/1000));

fbOauthClient.getAuthToken(function(access_token, expires_in) {
console.log("We got token:");
console.log(" access_token: %s", access_token);
console.log(" expires_in: %s", expires_in);
process.exit(0);
initRun();
});
5 changes: 5 additions & 0 deletions src/upload_photo.js
@@ -0,0 +1,5 @@
exports.run = function(progOpt, cmdArgs) {
console.log(progOpt);
console.log(cmdArgs);
process.exit(0);
}

0 comments on commit 3c3ccea

Please sign in to comment.