Personal Data Model

Usage

Aside from authentication setup, there's essentially one call to recover data in the personal-data-module:

datamodule.fetcher.fetch( uri, callback );

where callback takes two arguments, an error and a result document.

How is this possible? Every piece of data that can be recovered has a URI associated with it. This URI contains the user identification information, as well as a reference to the data that we want to recover. For example:

apinetwork://curtis//@acct:twitter:90542269/status/mentions

is the URI of a document which includes all of the a particular twitter account's mentions. Also included in the URI is the user ID of the user in our local system, so that we can keep users' data separate from each other. The URI format is according to the following:

In the above example, {protocol} is "apinetwork", indicating a standard personal-data-module data document URI. {Local User ID} is "curtis", which is the user who created the example, and the key used to look up that user's access keys in the TokenStore. {Remote Data Source} is "acct:twitter:90542269", which indicates an account from Twitter, belonging to user #90542269. The path is "status/mentions", which is means we want user statuses which mention that user.

Sample URIs

Within the URI format described above, there is some variation in the data which can be recovered from each data source. The goal is to make these URIs as consistent as possible, so some revision is not unthinkable. Here are samples of the URIs we have so far:

Code Sample

// We're using https://github.com/caolan/async for flow control later on.

// It's a nice package.

varasync=require('async');

// Include the personal-data-module library.

varEDM=require('personal-data-module');

// In a real application, you'd probably want to store the tokens in a database,

// but in this sample we'll use a simple in-memory datastore.

var tokenStore =newEDM.DummyTokenStore();

// The DummyTokenStore requires us to load it with the tokens we got when we registered

// our app with Twitter.

tokenStore.storeApplicationTokens(

'twitter',

{

"consumerKey":"GetThisFromTwitter",

"consumerSecret":"GetThisFromTwitterAsWellAndDontTellItToAnyone"

},

function(){

// Then we can actually instantiate the module.

var datamodule =newEDM.DataModule(

{

tokenStore: tokenStore,

// Specify which data sources we intend to use. Anything listed in here

// that doesn't have keys in the tokenStore will throw an error when it tries

//to initialize.

services:['twitter']

}

);

// The DummyTokenStore needs to be populated with user access tokens as well.

// These would generally also be stored in a database in a real product. You

// can use pretty much any string you want as a username.

tokenStore.storeUserTokens(

'curtis','acct:twitter:90542269',

{

// personal-data-module doesn't provide any of the authentication flow, you'll

// need to provde that. https://github.com/jaredhanson/passport is an excellent

// choice when it comes to authenticating with remote services.

token:'YouGetThisTokenWhenAUserAuthenticatesWithTwitter',

tokenSecret:'YouAlsoGetThisTokenWhenTheUserAuthenticates'

},

function(){

// Now that we have everything set up, we can actually runa few sample queries.

// We're using async.series here to run the samples in series without having to

// Deeply indent and make things hard to read.

async.series(

[

function(done){

// For "curtis"'s twitter account, recover his user profile.

datamodule.fetcher.fetch(

'apinetwork://curtis//@acct:twitter:90542269/user/90542269',

function(error,result){

if( error )

{

done( error );

}

else

{

console.log('******************************************');

console.log('Twitter User Profile:')

console.log( result );

console.log('******************************************');

done();

}

}

);

},

function(done){

// Now let's recover his most recent mentions.

datamodule.fetcher.fetch(

'apinetwork://curtis//@acct:twitter:90542269/status/mentions',

function(error,result){

if( error )

{

done( error );

}

else

{

console.log('******************************************');

console.log('Twitter Mentions of that user:')

console.log( result );

console.log('******************************************');

done();

}

}

);

},

function(done){

// Recovering a user profile also works for a user other than the account owner.

datamodule.fetcher.fetch(

'apinetwork://curtis//@acct:twitter:90542269/user/130852105',

function(error,result){

if( error )

{

done( error );

}

else

{

console.log('******************************************');

console.log('Twitter User Profile for a user other than the account holder:')

console.log( result );

console.log('******************************************');

done();

}

}

);

}

],

function(error){

if( error )

throw error;

}

);

}

);

}

);

MIT License

Copyright 2012-2013 Engine, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.