Attention! This is no longer the active package for the xAPI Wrapper. The new version can be found here.
ADL's Experience API wrapper for nodejs. The wrapper simplifies the process of communicating with an xAPI LRS.
Getting Started
Install the module with: npm install adl-xapiwrapper
Start the node Read-Eval-Print-Loop (REPL): node
or nodejs
depending on your installation.
See node's documentation for more information.
var adl = ;var opts = "url":"https://lrs.adlnet.gov/xapi/" "auth": "user":"tom" "pass":"1234" ;var mylrs = opts; mylrs;>> 200>> statements: more:""
Documentation and Examples
Module Attributes
xapiversion
- the version of the Experience API Spec this conforms to
build
- an ISO date representing when this script was built
debugLevel
- the minimum logging level to process ['error','warn','info]
// these values will vary based on the version of wrapper you havevar adl = ;adlxapiversion;>> '1.0.1'adlbuild;>> '2013-11-14T20:14Z'adldebugLevel;>> 'warn'
Module Functions
XAPI Request
function xapi_request(options, data, callback)
Encapsulates making requests to an LRS. See the section on this wrapper's
instance functions for scripted ways to make common
calls to an LRS. This uses the request module.
see requests
options
- options object used by the request module. see optionsdata
- the payload for POSTs and PUTscallback
- function to process after request has completed.- Parameters passed to callback:
error
- an error message if something went wrongresponse
- the response objectbody
- the body of the response if there is one
var adl = ;var myopts = "url":"https://lrs.adlnet.gov/xapi/statements" "auth": "user":"tom" "pass":"1234" "method":"GET" "qs": "limit":1 ;adldebugLevel = 'info';adl;>> info: 200>> info: "statements": <statements> "more": <more url>
Hash
function hash(string)
Sha1 hash of a string. Used for hashing contents sent to
the document endpoints (state, activity profile, agent profile) to
use for concurrency checks.
var doc = "This is my activity profile";var myhash = adlhashdoc
Log
function log(level, message)
Writes message to console based on level. Levels are error
, warn
, info
.
Level filtering can be changed by setting adl.debugLevel
.
var adl = ;adldebugLevel;>> 'warn'adl;> undefinedadldebugLevel = 'info';>> 'info'adl;>> info: this is an informational message
UUID
function ruuid()
Generates a UUID that can be used anywhere the Experience API Spec
specifies one, such as Statement ID or Registration.
adl;>> '47df99dd-e75f-484d-a85b-78cc988ae7c7'
Date from ISO String
function dateFromISOString(isostring)
Converts and ISO date time string into a JavaScript date object.
var date = adl;date;>> Thu Nov 14 2013 15:14:00 GMT-date>> 'Thu Nov 14 2013'
Instance Functions
Send Statements
function sendStatements(statements, callback)
Sends a single or a list of statements to the LRS.
Parameters:
statements
- the single statement as a JSON object, or list of statements as a JSON array of objectscallback
- function to process after request has completed.- Parameters passed to callback:
error
- an error message if something went wrongresponse
- the response objectbody
- the body of the response if there is one
var adl = ;adldebugLevel = 'info';var myconfig = "url":"https://lrs.adlnet.gov/xapi/" "auth": "user":"tom" "pass":"1234" ;var mylrs = myconfig; var stmt = "actor" : "mbox" : "mailto:tom@example.com" "verb" : "id" : "http://adlnet.gov/expapi/verbs/answered" "display" : "en-US" : "answered" "object" : "id" : "http://adlnet.gov/expapi/activities/question"; mylrs;>> info: 200>> info: <statement ids>
Get Statements
function getStatements(searchparams, more, callback)
Sends a single or a list of statements to the LRS.
Parameters:
searchparams
- JSON object of search parameters. See the xAPI Spec for parameters.more
- the url to more results.callback
- function to process after request has completed.- Parameters passed to callback:
error
- an error message if something went wrongresponse
- the response objectbody
- the body of the response if there is one
Get all Statements
var adl = ;adldebugLevel = 'info';var myconfig = "url":"https://lrs.adlnet.gov/xapi/" "auth": "user":"tom" "pass":"1234" ;var mylrs = myconfig;mylrs;>> info: 200>> info: "statements":<statements> "more":<url to more results>>> info: 50 // depends on LRS
Get more Statements
var adl = ;adldebugLevel = 'info';var myconfig = "url":"https://lrs.adlnet.gov/xapi/" "auth": "user":"tom" "pass":"1234" ;var mylrs = myconfig;mylrs;>> info: 200>> info: "statements":<statements> "more":<url to more results>>> info: going to get more statements>> info: 200>> info: "statements":<statements> "more":<url to more results>
Get Statements based on Search Parameters
var adl = ;adldebugLevel = 'info';var myconfig = "url":"https://lrs.adlnet.gov/xapi/" "auth": "user":"tom" "pass":"1234" ;var mylrs = myconfig;var myopts = "verb":"http://adlnet.gov/expapi/verbs/answered";mylrs;>> info: 200>> info: "statements":<statements with verb 'answered'> "more":<url to more results>>> info: 50 // depends on LRS
Get Activities
function getActivities(activityid, callback)
Gets the complete Activity object from the LRS.
Parameters:
activityid
- the id of the Activity requestedcallback
- function to process after request has completed.- Parameters passed to callback:
error
- an error message if something went wrongresponse
- the response objectbody
- the body of the response if there is one
var adl = ;adldebugLevel = 'info';var myconfig = "url":"https://lrs.adlnet.gov/xapi/" "auth": "user":"tom" "pass":"1234" ;var mylrs = myconfig;mylrs;>> info: 200>> info: <complete Activity object>
Send State
function sendState(activityid, agent, stateid, registration, stateval, matchHash, noneMatchHash, callback)
Sends state information about the agents experience of the activity.
Parameters:
activityid
- the id of the Activity this state is aboutagent
- the agent this Activity state is related tostateid
- the id you want associated with this stateregistration
- (optional) the registraton id associated with this statestateval
- the statematchHash
- the hash of the state to replace or * to replace anynoneMatchHash
- the hash of the current state or * to indicate no previous statecallback
- function to process after request has completed.- Parameters passed to callback:
error
- an error message if something went wrongresponse
- the response objectbody
- the body of the response if there is one
Get State
function getState(activityid, agent, stateid, registration, since, callback)
Get activity state from the LRS
Parameters:
activityid
- the id of the Activity this state is aboutagent
- the agent this Activity state is related tostateid
- (optional - if not included, the response will be a list of stateids associated with the activity and agent) the id you want associated with this stateregistration
- (optional) the registraton id associated with this statesince
- date object telling the LRS to return objects newer than the date suppliedcallback
- function to process after request has completed.- Parameters passed to callback:
error
- an error message if something went wrongresponse
- the response objectbody
- the body of the response if there is one
Send / Retrieve New Activity state
var adl = ;adldebugLevel = 'info';var myconfig = "url":"https://lrs.adlnet.gov/xapi/" "auth": "user":"tom" "pass":"1234" ;var mylrs = myconfig; var myactid = "http://example.com/activity/trails/appalachian";var agent = "mbox":"mailto:hikerbob@example.com";var mystateid = "training:appalachian-trail";var mystate = "trees":"hemlock""blue spruce""scene":"forest"; mylrs;>> info: response status: 204 mylrs;>> info: status code: 200>> info: the state: "trees":"hemlock""blue spruce""scene":"forest"
Change Activity State
var statehash = adlhashJSON;mystate'checkpoint' = "pa-w-blaze-6";mylrs;>> info: status code: 204 mylrs;>> info: state: "checkpoint": "pa-w-blaze-6" "scene": "forest" "trees": "hemlock" "blue spruce"
Get all state ids for given Activity and Agent
mylrs;>> info: state ids: "training:appalachian-trail"
Get state ids for a given Activity and Agent since a specified time
var sincehere = var newstateid = "content:settings";var newstate = "fps":"30""resolution":"1680x1050";mylrs;>> info: status: 204 // get all state idsmylrs;>> info: state ids: "content:settings" "training:appalachian-trail" // get ids of states saved since ..mylrs;>> info: state ids since Mon Nov 18 2013 09:31:15 GMT-: "content:settings"
Send Activity Profile
function sendActivityProfile(activityid, profileid, profileval, matchHash, noneMatchHash, callback)
Sends an Activity Profile to the LRS.
Parameters:
activityid
- the id of the Activity this profile is aboutprofileid
- the id you want associated with this profileprofileval
- the profilematchHash
- the hash of the profile to replace or * to replace anynoneMatchHash
- the hash of the current profile or * to indicate no previous profilecallback
- function to process after request has completed.- Parameters passed to callback:
error
- an error message if something went wrongresponse
- the response objectbody
- the body of the response if there is one
Get Activity Profile
function getActivityProfile(activityid, profileid, since, callback)
Get activity profile from the LRS
Parameters:
activityid
- the id of the Activity this profile is aboutprofileid
- (optional - if not included, the response will be a list of profileids associated with the activity) the id of the profilesince
- date object telling the LRS to return objects newer than the date suppliedcallback
- function to process after request has completed.- Parameters passed to callback:
error
- an error message if something went wrongresponse
- the response objectbody
- the body of the response if there is one
Send / Retrieve New Activity Profile
var adl = ;adldebugLevel = 'info';var myconfig = "url":"https://lrs.adlnet.gov/xapi/" "auth": "user":"tom" "pass":"1234" ;var mylrs = myconfig; var profile = "type":"question";var activityid = "http://adlnet.gov/expapi/activities/question";var profileid = "question:profile"; mylrs;>> info: status: 204 mylrs;>> info: profile: "type":"question"
Update Activity Profile
var profhash = adlhashJSON;profile"updated" = true; mylrs;>> info: status: 204 mylrs;>> info: profile: "updated": true "type": "question"
Get all profiles about a specific Activity
mylrs;>> info: profile ids: "question:profile"
Get profiles about an Activity since a certain time
var sincehere = ; var newprofile = "another" : "profile";var newprofileid = "another:profile"; mylrs;>> info: status: 204 // get all idsmylrs;>> info: profile ids: "question:profile" "another:profile" // get ids of Activity Profiles saved since date...mylrs;>> info: profile ids: "another:profile"
Get Agents
function getAgents(agent, callback)
Gets a special Person object containing all the values
of an Agent the LRS knows about. The Person object's
identifying properties are arrays and it may have more
than one identifier. See more about Person in the spec
Parameters:
agent
- JSON object representing an agent ex: {"mbox":"mailto:tom@example.com"}callback
- function to process after request has completed.- Parameters passed to callback:
error
- an error message if something went wrongresponse
- the response objectbody
- the body of the response if there is one
var adl = ;adldebugLevel = 'info';var myconfig = "url":"https://lrs.adlnet.gov/xapi/" "auth": "user":"tom" "pass":"1234" ;var mylrs = myconfig; mylrs;>> info: the Person: <Person object>
Send Agent Profile
function sendAgentProfile(agent, profileid, profileval, matchHash, noneMatchHash, callback)
Sends an Agent Profile to the LRS.
Parameters:
agent
- the agent this profile is related toprofileid
- the id you want associated with this profileprofileval
- the profilematchHash
- the hash of the profile to replace or * to replace anynoneMatchHash
- the hash of the current profile or * to indicate no previous profilecallback
- function to process after request has completed.- Parameters passed to callback:
error
- an error message if something went wrongresponse
- the response objectbody
- the body of the response if there is one
Get Agent Profile
function getAgentProfile(agent, profileid, since, callback)
Gets an Agent Profile from the LRS.
Parameters:
agent
- the agent associated with this profileprofileid
- (optional - if not included, the response will be a list of profileids associated with the agent) the id of the profilesince
- date object telling the LRS to return objects newer than the date suppliedcallback
- function to process after request has completed.- Parameters passed to callback:
error
- an error message if something went wrongresponse
- the response objectbody
- the body of the response if there is one
Send / Retrieve New Agent Profile
var adl = ;adldebugLevel = 'info';var myconfig = "url":"https://lrs.adlnet.gov/xapi/" "auth": "user":"tom" "pass":"1234" ;var mylrs = myconfig; var profile = "competencies":"http://adlnet.gov/competency/sitting-quietly" "http://adlnet.gov/competency/watching-tv" "current path":"http://adlnet.gov/competency/knitting";var agent = "mbox":"mailto:tom@example.com";var profileid = "competencies"; mylrs;>> info: status: 204 mylrs;>> info: profile: "competencies":"http://adlnet.gov/competency/sitting-quietly" "http://adlnet.gov/competency/watching-tv" "current path":"http://adlnet.gov/competency/knitting"
Update Agent Profile
var profhash = adlhashJSON;profile"competencies";profile"current path" = "http://adlnet.gov/competency/juggling"; mylrs;>> info: status: 204 mylrs;>> info: profile: "competencies": "http://adlnet.gov/competency/sitting-quietly" "http://adlnet.gov/competency/watching-tv" "http://adlnet.gov/competency/knitting" "current path": "http://adlnet.gov/competency/juggling"
Get all profiles about a specific Agent
mylrs;>> info: profile ids: "competencies"
Get profiles about an Agent since a certain time
var sincehere = ; var newprofile = "another" : "profile";var newprofileid = "another:profile"; mylrs;>> info: status: 204 // get all idsmylrs;>> info: profile ids: "competencies" "another:profile" // get ids of Activity Profiles saved since date...mylrs;>> info: profile ids: "another:profile"
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.
Release History
- 0.2.0 - Initial commit and release to npm
- 0.2.1 - Update of Readme
License
Copyright (c) 2013 ADL
Licensed under the Apache, 2.0 licenses.