Created
July 20, 2016 15:20
-
-
Save Kattoor/3d8e0271728c9ec4133e34f0880a55c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var debugging = false; | |
var requestBinUrl = 'http://requestb.in/1b60sct1'; | |
function setRequestParams(bundle, params) { | |
bundle.request.params = params; | |
return bundle.request; | |
} | |
function genericPre(bundle, what) { | |
var fields = bundle[what + '_fields']; //what = trigger, action or search | |
var params = objectifyValues(fields, what); | |
if (debugging) { | |
bundle.request.url = requestBinUrl; | |
} | |
return setRequestParams(bundle, params); | |
} | |
function objectifyValues(fields, what) { | |
var params = {}; | |
for (var fieldName in fields) { | |
if (fields.hasOwnProperty(fieldName)) { | |
var fieldValue = fields[fieldName]; | |
if (what == 'trigger' || what == 'search') { | |
fieldName = adjustFieldName(fieldName); | |
} | |
if (fieldValue) { | |
params[fieldName] = fieldValue; | |
} | |
} | |
} | |
return params; | |
} | |
function adjustFieldName(fieldName) { | |
if (fieldName.indexOf(']') === fieldName.length - 1) { | |
fieldName = fieldName.substr(0, fieldName.length - 1); | |
} | |
if (fieldName.indexOf('__') > -1) { | |
fieldName = fieldName.replace(/__/g, '.'); | |
} | |
return fieldName; | |
} | |
function prePoll(bundle) { | |
return genericPre(bundle, 'trigger'); | |
} | |
function preWrite(bundle) { | |
return genericPre(bundle, 'action'); | |
} | |
function preSearch(bundle) { | |
return genericPre(bundle, 'search'); | |
} | |
var Zap = { | |
New_Contact_pre_poll: prePoll, | |
New_Account_pre_poll: prePoll, | |
New_Opportunity_pre_poll: prePoll, | |
Create_Contact_pre_write: preWrite, | |
Create_Account_pre_write: preWrite, | |
Create_Opportunity_pre_write: preWrite, | |
Find_Contact_pre_search: preSearch, | |
Find_Account_pre_search: preSearch, | |
Find_Opportunity_pre_search: preSearch | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment