engine-data-module

Engine-Data-Module ==================

Engine-Data-Module

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 engine-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:

    ldengine://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 "ldengine", indicating a standard engine-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.netldengine://{owner}//@acct:appdotnet:{accountID}/user/{userID}User Profile for {userID}
ldengine://{owner}//@acct:appdotnet:{accountID}/user/followingList of users that {accountID} is following.
ldengine://{owner}//@acct:appdotnet:{accountID}/user/followersList of users who follow {accountID}.
ldengine://{owner}//@acct:appdotnet:{accountID}/posts/mentionsList of posts mentioning {accountID}.
ldengine://{owner}//@acct:appdotnet:{accountID}/posts/createdList of posts created by {accountID}.
Gmailldengine://{owner}//@acct:gmail:{accountID}/user/{userID}User Profile for {userID}
ldengine://{owner}//@acct:gmail:{accountID}/mailbox/_indexList of mailboxes in ownerID's account.
ldengine://{owner}//@acct:gmail:{accountID}/mailbox/{mailboxName}List of messages in mailbox named {mailboxName}, by URI
ldengine://{owner}//@acct:gmail:{accountID}/message/x-gm-msgid/{messageID}Email message by gmail message ID
ldengine://{owner}//@acct:gmail:{accountID}/message/{mailboxName}/message-id/{msgID}Email message in given mailbox according to its "message-id" SMTP header
ldengine://{owner}//@acct:gmail:{accountID}/message/{mailboxName}/uid/{msgID}Email message in given mailbox with server-assigned UID {msgID}
ldengine://{owner}//@acct:gmail:{accountID}/message/{mailboxName}/{seqNumber}Email message in given mailbox according to its sequence number
IMAPldengine://{owner}//@acct:imap:{username}@{server}/user/{username}User profile for {username} (This data is very limited)
ldengine://{owner}//@acct:imap:{username}@{server}/mailbox/_indexList of mailboxes in ownerID's account.
ldengine://{owner}//@acct:imap:{username}@{server}/mailbox/{mailboxName}List of messages in mailbox named {mailboxName}, by URI
ldengine://{owner}//@acct:imap:{username}@{server}/message/{mailboxName}/uid/{msgID}Email message in given mailbox with server-assigned UID {msgID}
ldengine://{owner}//@acct:imap:{username}@{server}/message/{mailboxName}/{seqNumber}Email message in given mailbox according to its sequence number
Twitterldengine://{owner}//@acct:twitter:{accountID}/user/{userID}User Profile for {userID}
ldengine://{owner}//@acct:twitter:{accountID}/status/sentStatus messages sent by {accountID}
ldengine://{owner}//@acct:twitter:{accountID}/{userID}/relationship/outgoing/confirmedList of users that {userID} is following.
ldengine://{owner}//@acct:twitter:{accountID}/{userID}/relationship/outgoing/pendingList of users that {userID} has requested to follow.
ldengine://{owner}//@acct:twitter:{accountID}/status/mentionsStatus messages that mention {accountID}.
ldengine://{owner}//@acct:twitter:{accountID}/{userID}/relationship/incoming/confirmedList of users that follow {userID}.
ldengine://{owner}//@acct:twitter:{accountID}/{userID}/relationship/incoming/pendingList of users that have requested to follow {userID}
ldengine://{owner}//@acct:twitter:{accountID}/direct/sentDirect messages sent by {accountID}
ldengine://{owner}//@acct:twitter:{accountID}/direct/receivedDirect messages received by {accountID}
Facebookldengine://{owner}//@acct:facebook:{accountID}/user/{userID}User Profile for {userID}
ldengine://{owner}//@acct:facebook:{accountID}/contactsUser's friends profile list
ldengine://{owner}//@acct:facebook:{accountID}/fnewsUser's news feed
ldengine://{owner}//@acct:facebook:{accountID}/fstatusesUser's status feed
ldengine://{owner}//@acct:facebook:{accountID}/feventsUser's created events.
ldengine://{owner}//@acct:facebook:{accountID}/fgroupsUser's group details
ldengine://{owner}//@acct:facebook:{accountID}/flikesUser's likes
ldengine://{owner}//@acct:facebook:{accountID}/flinksLinks shared by user
ldengine://{owner}//@acct:facebook:{accountID}/fphotosUser's photos
ldengine://{owner}//@acct:facebook:{accountID}/fpostsPosts shared by user
ldengine://{owner}//@acct:facebook:{accountID}/falbumsalbums shared by user
ldengine://{owner}//@acct:facebook:{accountID}/fnotesUser's notes
ldengine://{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 engine-data-module library. 
var EDM = require( 'engine-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',
            { 
        // engine-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( 
                                'ldengine://curtis//@acct:twitter:90542269/user/90542269',
                                function( errorresult ) {
                                    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( 
                                'ldengine://curtis//@acct:twitter:90542269/status/mentions',
                                function( errorresult ) {
                                    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( 
                                'ldengine://curtis//@acct:twitter:90542269/user/130852105',
                                function( errorresult ) {
                                    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.