node-misfit
v0.1.2
Published
NodeJS library for interfacing with the Misfit Cloud API.
Downloads
7
Readme
node-misfit
Node.js and io.js library for interfacing with the Misfit Cloud API.
Getting Started
Install the library into Node.js or io.js:
npm install node-misfit
var misfit = require('node-misfit');
Then construct a new misfit handler object and go to town...
var misfitHandler = new misfit({
clientId: process.env.MISFIT_CLIENT_ID,
clientSecret: process.env.MISFIT_CLIENT_SECRET,
redirectUri: process.env.MISFIT_REDIRECT_URI
});
var authorizeUrl = misfitHandler.getAuthorizeUrl();
misfitHandler.getProfile(user.accessToken, function(err, profile) {
console.log(profile);
});
API
node-misfit([options
])
node-misfit constructor
Parameters
options
- (Object) Configuration options for the node-misfit object
clientId
(String) Client ID for the Misfit API applicationDefault: ''
clientSecret
(String) Client secret for the Misfit API applicationDefault: ''
redirectUri
(String) URL to redirect the browser to after authorization requestsDefault: ''
scope
(String) Data parameters to which requests should be limitedDefault: 'public,birthday,email,tracking,session,sleeps'
Example
var misfit = require('node-misfit');
var misfitHandler = new misfit({
clientId: process.env.MISFIT_CLIENT_ID,
clientSecret: process.env.MISFIT_CLIENT_SECRET,
redirectUri: process.env.MISFIT_REDIRECT_URI
});
getAuthorizeUrl()
Get the authorization URL for the configured node-misfit object
Misfit - Authorize a 3rd-party app
Example
misfitHandler.getAuthorizeUrl(); // -> https://api.misfitwearables.com/auth/dialog/authorize?response_type=code&client_id=...
getAccessToken(authorizationCode
, [callback
])
Exchange a Misfit API authorization code for an access token
Misfit - Get access token from authorized code
Parameters
authorizationCode
(String) The authorization code to exchange for an access tokencallback
(Function(err, token)) Optional callback to execute on completion of the request
Throws
RequiredParameterError
: ArgumentauthorizationCode
required
Example
misfitHandler.getAccessToken(user.authCode, function(err, token){
console.log(token); // -> 'afwfh384hg84uh348g34g8374hga874gh8a374gha8347gha8347'
});
getProfile(accessToken
, [options
], [callback
])
Get the profile for the specified user.
Parameters
accessToken
(String) - The user's access token to authenticate the requestoptions
(String) Optional set of options to configure the request.
userId
(String) User id of the profile resource owner.Default: 'me'
objectId
(String) Optional id of the specific profile resource to retrieve.
callback
(Function(err, profile)) Optional callback to execute on completion of the request
Throws
RequiredParameterError
: ArgumentaccessToken
required
Example
misfitHandler.getProfile(user.accessToken, function(err, profile){
console.log(profile);
// -> {
// "userId":"51a4189acf12e53f79000001",
// "name":"Misfit",
// "birthday":"1955-07-06",
// "gender":"female",
// "email":"[email protected]"
// }
}
getDevice(accessToken
, [options
], [callback
])
Get the device information for the specified user.
Parameters
accessToken
(String) The user's access token to authenticate the requestoptions
(String) Optional set of options to configure the request.
userId
(String) User id of the device resource owner.Default: 'me'
objectId
(String) Optional id of the specific device resource to retrieve.
callback
(Function(err, profile)) Optional callback to execute on completion of the request
Throws
RequiredParameterError
: ArgumentaccessToken
required
Example
misfitHandler.getDevice(user.accessToken, function(err, device){
console.log(device);
// -> {
// "id":"51a4189acf12e53f80000001",
// "deviceType":"shine",
// "serialNumber":"XXXXXV0011",
// "firmwareVersion":"0.0.50r",
// "batteryLevel":40
// }
}
getGoals(accessToken
, startDate
, endDate
, [options
], [callback
])
Get the goal information for the specified user.
Parameters
accessToken
(String) The user's access token to authenticate the requeststartDate
(String) Start date for the queryFormat: 'YYYY-MM-DD'
endDate
(String) End date for the queryFormat: 'YYYY-MM-DD'
options
(String) Optional set of options to configure the request.
userId
(String) User id of the goal resource owner.Default: 'me'
objectId
(String) Optional id of the specific goal resource to retrieve.
callback
(Function(err, profile)) Optional callback to execute on completion of the request
Throws
InvalidDateError
:startDate
andendDate
must be valid dates in the formatYYYY-MM-DD
InvalidDateRangeError
:startDate
must precedeendDate
and the duration may not exceed 30 daysRequiredParameterError
: ArgumentsaccessToken
,startDate
,endDate
required
Example
misfitHandler.getGoals(user.accessToken, '2013-11-05', '2013-11-08', function(err, goals){
console.log(goals);
// -> {
// "goals":[
// {
// "id":"51a4189acf12e53f81000001",
// "date":"2013-10-05",
// "points":500,
// "targetPoints":1000
// },
// ...
// ]
// }
}
getSummary(accessToken
, startDate
, endDate
, [options
], [callback
])
Get the summary information for the specified user.
Parameters
accessToken
(String) The user's access token to authenticate the requeststartDate
(String) Start date for the queryFormat: 'YYYY-MM-DD'
endDate
(String) End date for the queryFormat: 'YYYY-MM-DD'
options
(String) Optional set of options to configure the request.
userId
(String) User id of the summary resource owner.Default: 'me'
detail
(Boolean) Print a summary for each day instead of rolling up the summary to a single entryDefault: false
callback
(Function(err, profile)) Optional callback to execute on completion of the request
Throws
InvalidDateError
:startDate
andendDate
must be valid dates in the formatYYYY-MM-DD
InvalidDateRangeError
:startDate
must precedeendDate
and the duration may not exceed 30 daysRequiredParameterError
: ArgumentsaccessToken
,startDate
,endDate
required
Example
misfitHandler.getSummary(user.accessToken, '2013-11-05', '2013-11-08', {detail: true}, function(err, summary){
console.log(summary);
// -> {
// "summary": [
// {
// "date": "2013-11-05",
// "points": 394.4,
// "steps": 3650,
// "calories": 1687.4735,
// "activityCalories": 412.3124,
// "distance": 1.18
// },
// {
// "date": "2013-11-06",
// "points": 459.6,
// "steps": 4330,
// "calories": 1707.8484,
// "activityCalories": 412.3124,
// "distance": 1.3982
// },
// ...
// ]
// }
}
getSessions(accessToken
, startDate
, endDate
, [options
], [callback
])
Get the session information for the specified user.
Parameters
accessToken
(String) The user's access token to authenticate the requeststartDate
(String) Start date for the queryFormat: 'YYYY-MM-DD'
endDate
(String) End date for the queryFormat: 'YYYY-MM-DD'
options
(String) Optional set of options to configure the request.
userId
(String) User id of the sessions resource owner.Default: 'me'
objectId
(String) Optional id of the specific sessions resource to retrieve.
callback
(Function(err, profile)) Optional callback to execute on completion of the request
Throws
InvalidDateError
:startDate
andendDate
must be valid dates in the formatYYYY-MM-DD
InvalidDateRangeError
:startDate
must precedeendDate
and the duration may not exceed 30 daysRequiredParameterError
: ArgumentsaccessToken
,startDate
,endDate
required
Example
misfitHandler.getSessions(user.accessToken, '2013-11-05', '2013-11-08', function(err, sessions){
console.log(sessions);
//-> {
// "sessions":[
// {
// "id":"51a4189acf12e53f82000001",
// "activityType":"Cycling",
// "startTime":"2014-05-19T10:26:54-04:00",
// "duration":900,
// "points":210.8,
// "steps":1406,
// "calories":25.7325,
// "distance":0.5125
// },
// ...
// ]
// }
getSleeps(accessToken
, startDate
, endDate
, [options
], [callback
])
Get the sleep information for the specified user.
Parameters
accessToken
(String) The user's access token to authenticate the requeststartDate
(String) Start date for the queryFormat: 'YYYY-MM-DD'
endDate
(String) End date for the queryFormat: 'YYYY-MM-DD'
options
(String) Optional set of options to configure the request.
userId
(String) User id of the sleep resource owner.Default: 'me'
objectId
(String) Optional id of the specific sleep resource to retrieve.
callback
(Function(err, profile)) Optional callback to execute on completion of the request
Throws
InvalidDateError
:startDate
andendDate
must be valid dates in the formatYYYY-MM-DD
InvalidDateRangeError
:startDate
must precedeendDate
and the duration may not exceed 30 daysRequiredParameterError
: ArgumentsaccessToken
,startDate
,endDate
required
Example
misfitHandler.getSleeps(user.accessToken, '2013-11-05', '2013-11-08', function(err, sleeps){
console.log(sleeps);
//-> {
// "sleeps":[
// {
// "id":"51a4189acf12e53f80000003",
// "autoDetected": false,
// "startTime":"2014-05-19T23:26:54+07:00",
// "duration": 0,
// "sleepDetails":[
// {
// "datetime":"2014-05-19T23:26:54+07:00",
// "value":2
// },
// {
// "datetime":"2014-05-19T23:59:22+07:00",
// "value":1
// },
// ...
// ]
// },
// ...
// ]
// }
handleSubscription(data
, [callback
])
Handle subscription request from Misfit Cloud API
Parameters
data
(Object) Subscription Amazon SNS message from Misfit Cloud APIcallback
(Function(err, sleeps)) Optional callback to execute on completion of the request
Throws
RequiredParameterError
: Argumentdata
required
Exceptions
InvalidDateError([message
])
Error indicating an invalid date has been specified.
message
- (String) Optional custom error message.
InvalidDateRangeError([message
])
Error indicating an invalid date range has been supplied.
message
- (String) Optional custom error message.
MisfitError(error
)
Error indicating an issue with authentication or authorization while accessing a Misfit resource.
error
- (Object) Originating error object parsed from API call.
RequiredParameterError(parameters
)
Error indicating that required parameters for a function has not been provided.
parameters
- (Array) Array of missing parameters.
Testing
npm test
Release notes
0.1.2
- Fixed invalid error object handling
- Addition of
MisfitError
and integration into Misfit Cloud API calls - Support for io.js
- Mock API support with associated unit testing
0.1.1
- Support for
detail
option ingetSummary
- Completed README with examples
- Full JsDoc information for both public and private methods
0.1.0
- Initial Release
License
(The MIT License)
Copyright (c) 2015 Armin Tamzarian
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.