Skip to content

Instantly share code, notes, and snippets.

@johnmjackson
Last active September 15, 2015 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmjackson/47280df7f1ad6202c0c7 to your computer and use it in GitHub Desktop.
Save johnmjackson/47280df7f1ad6202c0c7 to your computer and use it in GitHub Desktop.
Using boolean fields in a Flow XO SDK service.
'use strict';
var config = {
name: 'Send a Notification',
slug: 'send_notification',
type: 'action',
kind: 'task',
scripts: {
run: require('./run')
},
fields: {
input: [{
key: 'message',
label: 'Message',
type: 'textarea',
required: true
}, {
// To use the boolean field type, simply set the 'type'
// to 'boolean'.
key: 'notify',
label: 'Notify?',
type: 'boolean',
description: 'Whether this message should trigger a user notification.'
}],
output: []
}
};
module.exports = function(service) {
service.registerMethod(config);
};
'use strict';
module.exports = function(options, done) {
var inputErr = this.validateScriptInput(options.input, {
message: {
presence: true
},
// We use the custom validator 'fxoBoolean' to
// make sure that we can parse a boolean from
// the input that the user gave.
// For example, 'yes', 'no', '1', '0', 'true', 'false'
notify: {
fxoBoolean: true
}
});
// If there's any kind of error, return it.
if(inputErr) {
return done(inputErr);
}
// Otherwise, we have a valid boolean object,
// which looks like this:
//
// { type: 'boolean',
// input: 'yes',
// valid: true,
// parsed: true }
//
// So the actual value we need is contained
// in the 'parsed' property. This will contain
// either true or false.
//
// Swap the object for the actual value now.
if(inputData.notify) {
inputData.notify = inputData.notify.parsed;
}
// Now we can make the actual request, and we
// know for sure we've got a bool to send.
var opt = {
endpoint: '/notification',
token: options.credentials.auth_token,
method: 'POST',
json: options.input
};
this.request(opt, done);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment