Skip to content

Commit

Permalink
Refactoring and adding success callback to the sync process
Browse files Browse the repository at this point in the history
Using a Queue to allow for linear processing of user data that can be chained
together. Now that there is a known completion point a callback can be given to
execute when the process is complete.

The dependency on `http` has been removed as well and instead the `UserSync`
module now operates on a stream. This allows the JSON data to come from a
number of sources including stdin.
  • Loading branch information
beaucollins committed Jul 2, 2012
1 parent 2b37e18 commit bbfcbef
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 114 deletions.
31 changes: 21 additions & 10 deletions README.md
Expand Up @@ -25,6 +25,10 @@ publicly. See [users.json][] for an example.
Step 2) Run the jquery-user-sync command providing the necessary arguments:

jquery-user-sync JSON_URL --endpoint=http://site.wordpress.com/ --username=admin --password=123456

Or you can pipe the users in too:

curl http://example.com/users.json | jquery-user-sync --endpoint=http://site.wordpress.com/ --username=admin --password=123456

Step 3) That's it

Expand All @@ -43,18 +47,25 @@ Update your <code>package.json</code>:
},
...

Then require and run:
Then require and run, provide the WP settings, a stream of JSON of users and a success callback:

var sync = require('wordpress-user-sync');

sync.run({
var http = require('http'),
url = require('url'),
sync = require('wordpress-user-sync'),
settings = {
wordpress_api_url: 'http://blog.com/',
jquery_users_json_url: 'http://example.com/somejson.json',
username: 'admin',
password: 'password'
});
}

TODO:
-----

Add a way to provide a callback for when the syncing is done.
http.get( url.parse('http://example.com/users.json'), function(res){
sync.run( settings, res, function( error, users ){
if(e){
console.log(error);
} else {
console.log("All done");
}
} );
} );


27 changes: 23 additions & 4 deletions bin/jquery-user-sync
Expand Up @@ -3,12 +3,21 @@
var path = require('path'),
fs = require('fs'),
optparse = require('optparse'),
lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib');
lib = path.join(path.dirname(fs.realpathSync(__filename)), '../lib'),
http = require('http'),
https = require('https'),
url = require('url');

function run(settings, stream){
require(path.join(lib, 'wordpress-user-sync')).run(settings, stream, function(){
console.log("Completed!");
// console.log.apply(console, arguments);
});
}

try {

var settings = {};
var settings = {}, json_url;

options = new optparse.OptionParser([
['-u', '--username USERNAME', 'WordPress account username for accessing the XML-RPC API'],
Expand All @@ -18,7 +27,7 @@ try {

// URL referencing the USERS
options.on(2, function(url){
settings.jquery_users_json_url = url;
json_url = url;
});

options.on('username', function(key, username){
Expand All @@ -35,7 +44,17 @@ try {

options.parse(process.argv);

require(path.join(lib, 'wordpress-user-sync')).run(settings);
if (json_url) {
var u = url.parse(json_url), protocol = u.protocol == 'https' ? https : http;
console.log( "Fetching users from", json_url );
protocol.get( u, function( res ){
run( settings, res );
} );
} else {
console.log( "Reading users from STDIN" );
run( settings, process.stdin );
}


} catch (e) {
process.stderr.write("Failed with error:\n");
Expand Down
48 changes: 39 additions & 9 deletions lib/queue.js
@@ -1,26 +1,56 @@
var Queue = function( items ){
var Queue = function( items, operator ){
var queue = items.slice();
this.operator = operator;

this.run = function( callback ){
this.run = function( operator, callback ){

if ( !callback ) {
callback = operator;
operator = this.operator;
}

if (typeof(operator) != 'function') {
throw( "operator must be a function" );
}

if (typeof(callback) != 'function') {
throw( "callback must be a function" );
}

if ( queue.length == 0 ) {
callback( null, items );
return;
};

// shift an item off
var item = queue.shift();
var item = queue.shift(), next, self = this;

try {
this.operate( item )
} catch(e) {
// fire the callback with the error and stop operating
if ( queue.length == 0 ) {
// queue is done, so we're going
next = function( e ){
callback( e, items );
};
} else {
next = function(){
self.run( operator, callback );
}
}
// operate on the item
// do it again

try{
operator( item, next );
} catch( e ){
callback( e );
}

}

}

module.exports = {
createQueue: function( items, operation, callback ){
var queue = new Queue( items );
queue.operation = operation;
queue.run(operation, callback)
},
Queue: Queue
}

0 comments on commit bbfcbef

Please sign in to comment.