0.0.4 • Published 10 years ago

personal-data-module v0.0.4

Weekly downloads
2
License
MIT
Repository
github
Last release
10 years ago

Personal Data Model

Access a variety of data sources in a simple, consistent way.

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:

	{protocol}://{Local User ID}//@{Remote Data Source}/{Path describing the desired document}

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:

SourceURIMeaning
App.netapinetwork://{owner}//@acct:appdotnet:{accountID}/user/{userID}User Profile for {userID}
apinetwork://{owner}//@acct:appdotnet:{accountID}/user/followingList of users that {accountID} is following.
apinetwork://{owner}//@acct:appdotnet:{accountID}/user/followersList of users who follow {accountID}.
apinetwork://{owner}//@acct:appdotnet:{accountID}/posts/mentionsList of posts mentioning {accountID}.
apinetwork://{owner}//@acct:appdotnet:{accountID}/posts/createdList of posts created by {accountID}.
Gmailapinetwork://{owner}//@acct:gmail:{accountID}/user/{userID}User Profile for {userID}
apinetwork://{owner}//@acct:gmail:{accountID}/mailbox/_indexList of mailboxes in ownerID's account.
apinetwork://{owner}//@acct:gmail:{accountID}/mailbox/{mailboxName}List of messages in mailbox named {mailboxName}, by URI
apinetwork://{owner}//@acct:gmail:{accountID}/message/x-gm-msgid/{messageID}Email message by gmail message ID
apinetwork://{owner}//@acct:gmail:{accountID}/message/{mailboxName}/message-id/{msgID}Email message in given mailbox according to its "message-id" SMTP header
apinetwork://{owner}//@acct:gmail:{accountID}/message/{mailboxName}/uid/{msgID}Email message in given mailbox with server-assigned UID {msgID}
apinetwork://{owner}//@acct:gmail:{accountID}/message/{mailboxName}/{seqNumber}Email message in given mailbox according to its sequence number
IMAPapinetwork://{owner}//@acct:imap:{username}@{server}/user/{username}User profile for {username} (This data is very limited)
apinetwork://{owner}//@acct:imap:{username}@{server}/mailbox/_indexList of mailboxes in ownerID's account.
apinetwork://{owner}//@acct:imap:{username}@{server}/mailbox/{mailboxName}List of messages in mailbox named {mailboxName}, by URI
apinetwork://{owner}//@acct:imap:{username}@{server}/message/{mailboxName}/uid/{msgID}Email message in given mailbox with server-assigned UID {msgID}
apinetwork://{owner}//@acct:imap:{username}@{server}/message/{mailboxName}/{seqNumber}Email message in given mailbox according to its sequence number
Twitterapinetwork://{owner}//@acct:twitter:{accountID}/user/{userID}User Profile for {userID}
apinetwork://{owner}//@acct:twitter:{accountID}/status/sentStatus messages sent by {accountID}
apinetwork://{owner}//@acct:twitter:{accountID}/{userID}/relationship/outgoing/confirmedList of users that {userID} is following.
apinetwork://{owner}//@acct:twitter:{accountID}/{userID}/relationship/outgoing/pendingList of users that {userID} has requested to follow.
apinetwork://{owner}//@acct:twitter:{accountID}/status/mentionsStatus messages that mention {accountID}.
apinetwork://{owner}//@acct:twitter:{accountID}/{userID}/relationship/incoming/confirmedList of users that follow {userID}.
apinetwork://{owner}//@acct:twitter:{accountID}/{userID}/relationship/incoming/pendingList of users that have requested to follow {userID}
apinetwork://{owner}//@acct:twitter:{accountID}/direct/sentDirect messages sent by {accountID}
apinetwork://{owner}//@acct:twitter:{accountID}/direct/receivedDirect messages received by {accountID}
Facebookapinetwork://{owner}//@acct:facebook:{accountID}/user/{userID}User Profile for {userID}
apinetwork://{owner}//@acct:facebook:{accountID}/contactsUser's friends profile list
apinetwork://{owner}//@acct:facebook:{accountID}/fnewsUser's news feed
apinetwork://{owner}//@acct:facebook:{accountID}/fstatusesUser's status feed
apinetwork://{owner}//@acct:facebook:{accountID}/feventsUser's created events.
apinetwork://{owner}//@acct:facebook:{accountID}/fgroupsUser's group details
apinetwork://{owner}//@acct:facebook:{accountID}/flikesUser's likes
apinetwork://{owner}//@acct:facebook:{accountID}/flinksLinks shared by user
apinetwork://{owner}//@acct:facebook:{accountID}/fphotosUser's photos
apinetwork://{owner}//@acct:facebook:{accountID}/fpostsPosts shared by user
apinetwork://{owner}//@acct:facebook:{accountID}/falbumsalbums shared by user
apinetwork://{owner}//@acct:facebook:{accountID}/fnotesUser's notes
apinetwork://{owner}//@acct:facebook:{accountID}/fvideosVideos shared by user

Code Sample

// We're using https://github.com/caolan/async for flow control later on.
// It's a nice package.
var async = require( 'async' );

// Include the personal-data-module library.
var EDM = 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 = new EDM.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 = new EDM.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.