Skip to main content
deleted 11 characters in body; deleted 12 characters in body
Source Link
Sky Sanders
  • 12.1k
  • 3
  • 32
  • 60
new Soapi.RouteFactory("", apiKey)
    .ApiSites()
    .getResponse(function(data) {
        // initialize the application
    }, function(error) {
        // die
    });
var route = new Soapi.RouteFactory(site.api_endpoint, apiKey).Users();
route.filter = username;
route.pagesize = 50
route.page = 1;

var count = sites.length // so we know when all sites have returned results

route.getResponse(function(data) {
    // aggregate the users
    users = users.concat(data.usersitems); // aggregate the results from this site
    if (--count == 0) {
        // all sites reported in. display the results
    }
}, function(error) {
    count--; // still need to pop the count
    // report error
});
new Soapi.RouteFactory("", apiKey)
    .UsersByIdAssociated(association_id)
    .getResponse(function(data) {
        for (var i = 0; i < data.associated_usersitems.length; i++) {
            // render the account info
        }
    }, function(error) {
        // log error
    }, 1000);
// set a very short timeout of 1 second
// because there are many users without
// association id and the app has not
// the logic yet to simply not render a link
// and we want a failure to come swiftly
new Soapi.RouteFactory("", apiKey)
    .ApiSites()
    .getResponse(function(data) {
        // initialize the application
    }, function(error) {
        // die
    });
var route = new Soapi.RouteFactory(site.api_endpoint, apiKey).Users();
route.filter = username;
route.pagesize = 50
route.page = 1;

var count = sites.length // so we know when all sites have returned results

route.getResponse(function(data) {
    // aggregate the users
    users = users.concat(data.users); // aggregate the results from this site
    if (--count == 0) {
        // all sites reported in. display the results
    }
}, function(error) {
    count--; // still need to pop the count
    // report error
});
new Soapi.RouteFactory("", apiKey)
    .UsersByIdAssociated(association_id)
    .getResponse(function(data) {
        for (var i = 0; i < data.associated_users.length; i++) {
            // render the account info
        }
    }, function(error) {
        // log error
    }, 1000);
// set a very short timeout of 1 second
// because there are many users without
// association id and the app has not
// the logic yet to simply not render a link
// and we want a failure to come swiftly
Soapi.RouteFactory("", apiKey)
    .ApiSites()
    .getResponse(function(data) {
        // initialize the application
    }, function(error) {
        // die
    });
var route = Soapi.RouteFactory(site.api_endpoint, apiKey).Users();
route.filter = username;
route.pagesize = 50
route.page = 1;

var count = sites.length // so we know when all sites have returned results

route.getResponse(function(data) {
    // aggregate the users
    users = users.concat(data.items); // aggregate the results from this site
    if (--count == 0) {
        // all sites reported in. display the results
    }
}, function(error) {
    count--; // still need to pop the count
    // report error
});
Soapi.RouteFactory("", apiKey)
    .UsersByIdAssociated(association_id)
    .getResponse(function(data) {
        for (var i = 0; i < data.items.length; i++) {
            // render the account info
        }
    }, function(error) {
        // log error
    }, 1000);
// set a very short timeout of 1 second
// because there are many users without
// association id and the app has not
// the logic yet to simply not render a link
// and we want a failure to come swiftly
Source Link
Sky Sanders
  • 12.1k
  • 3
  • 32
  • 60

###Under the hood

The code required to produce the pretty output, for what it does, is fairly brief but it does distract from the core functions that do all the heavy lifting.

Stack Users Lite is built upon 3 API calls

  • /sites (Route.ApiSites)
  • /users (Route.Users)
  • /users/{id}/associated (Route.UsersByIdAssociated)

###Route.ApiSites Route.ApiSites is a StackAuth route that retrieves the current list of Stack Exchange sites from http://stackauth.com/0.9/sites.

new Soapi.RouteFactory("", apiKey)
    .ApiSites()
    .getResponse(function(data) {
        // initialize the application
    }, function(error) {
        // die
    });

You may notice that there is no target site specified in the RouteFactory constructor. This is because the stackauth methods are static and unlike the other routes in Soapi.js, the target is hardcoded. This means that you may either instantiate a factory with no target site specifically for retrieving target sites or you may simply use any instantiated factory instance regardless of the target site specified in the construction.

The same principal applies to Route.UsersByIdAssociated.

###Route.Users Route.Users is called for each of the sites previously retrieved from http://stackauth.com/0.9/sites using the criteria provided in the single input element.

The number of results retrieved and shown is limited by setting the pagesize property of the Route before requesting a response.

Each of these results is aggregated into a common array and then rendered to the page.

var route = new Soapi.RouteFactory(site.api_endpoint, apiKey).Users();
route.filter = username;
route.pagesize = 50
route.page = 1;

var count = sites.length // so we know when all sites have returned results

route.getResponse(function(data) {
    // aggregate the users
    users = users.concat(data.users); // aggregate the results from this site
    if (--count == 0) {
        // all sites reported in. display the results
    }
}, function(error) {
    count--; // still need to pop the count
    // report error
});

###Route.UsersByIdAssociated When a username is clicked in the results list, Route.UsersByIdAssociated calls http://stackauth.com/0.9//users/a1c71168-ffb9-42a5-8d71-ce38ad8f2c79/associated to retrieve a list of all the users accounts throughout the Stack Exchange network and displays those in the details view.

new Soapi.RouteFactory("", apiKey)
    .UsersByIdAssociated(association_id)
    .getResponse(function(data) {
        for (var i = 0; i < data.associated_users.length; i++) {
            // render the account info
        }
    }, function(error) {
        // log error
    }, 1000);
// set a very short timeout of 1 second
// because there are many users without
// association id and the app has not
// the logic yet to simply not render a link
// and we want a failure to come swiftly

###Conclusion So there you have it. Examined in isolation from the GUI goo, this simple application seems even more simple.

Simple is good (rt).