Source: cast-user-vote.js

/**
 * @function castUserVote
 * @summary
 * Sends a user vote to the server, then emits a POLL_RESULTS event with the results.
 * @description
 * Example of how to set up a poll:
 * 1.  Create a poll
 *     https://pterodactylus.tremorvideodsp.com/poll/new
 * 2.  View the poll JSON by going to the pterodactylus Poll List page and clicking the JSON button
 * 3.  Copy the poll JSON and set it to a javascript variable:
 * ```
 * var poll = {
 *   "prefix": "2018/08",
 *   "puid": "OMB1s5",
 *   "cu": "CU00002",
 *   "title": "Are you hungry or just sad?",
 *   "quantity": 2,
 *   "answers": [
 *     {
 *       "auid": "ANuH2k",
 *       "text": "Hungry.",
 *       "votes": 0
 *     },
 *     {
 *       "auid": "LeuAM9",
 *       "text": "Just sad.",
 *       "votes": 0
 *     }
 *   ],
 *   "createdDate": "2018-08-29T21:46:49.590Z"
 * }
 * ```
 * 4.  Set the answer chosen and call the function:
 * ```
 *  var answerChosen = 0
 *  window.$b.snippets.castUserVote({
 *    cu: poll.cu,
 *    puid: poll.puid,
 *    auid: poll.answers[answerChosen]
 *  })
 * ```
 *
 * Example of how to receive results:
 * ```
 * window.$b.on('POLL_RESULTS', function (data) {
 *   data.answers.forEach(function (answer) {
 *     console.log(answer.votes + ' votes for: "' + answer.text + '"')
 *    })
 * })
 * ```
 * Required parameters:
 * @param {Object} options
 * @param {string} options.auid Answer Unique ID (6 alpha-numeric, - or _)
 * @param {string} options.puid Poll Unique ID (6 alpha-numeric, - or _)
 * @param {string} options.cu Creative Unit (CU and 1-5 digits, ex. CU12345)
 *
 * Optional parameter:
 * @param {string=} options.host (default "https://pterodactylus.tremorvideodsp.com")
 * @param {number=} timeout (default 3 seconds) maximum seconds to wait for api results
 *
 * Note: if the server does not send back a response in 30 seconds, an error is logged to the console.
 */
function castUserVote (options) {
  var isIdValid = function (id) {
    if (!id) return false
    return true
  }
  var errorEvent = 'POLL_ERROR'
  var message
  if (!isIdValid(options.auid)) {
    message = 'castUserVote: invalid auid answer id: ' + options.auid
    console.error(errorEvent, message)
    window.$b.emit(errorEvent, message)
    return
  }
  if (!isIdValid(options.puid)) {
    message = 'castUserVote: invalid puid poll id: ' + options.puid
    console.error(errorEvent, message)
    window.$b.emit(errorEvent, message)
    return
  }
  var timeout = options.timeout
  if ((typeof timeout !== 'number') || (timeout <= 0)) {
    timeout = 3000
  } else {
    timeout *= 1000
  }
  var host = options.host || 'https://raptor.tremorvideodsp.com'
  var url = host + '/ws/poll/PUID/AUID'
  url = url
    .replace('AUID', options.auid.auid)
    .replace('PUID', options.puid)
  window.$b.emitInteract(
    { context: 'Poll', action: options.auid.auid }
  )
  return $.ajax({
    url: url,
    method: 'GET',
    timeout: timeout
  })
    .done(function (data) {
      console.log('POLL_RESULTS: ', data)
      window.$b.emit('POLL_RESULTS', data)
    })
    .fail(function (err) {
      console.error(errorEvent, err)
      window.$b.emit(errorEvent, err)
    })
}

window.$b = window.$b || {}
window.$b.snippets = window.$b.snippets || {}
var snippets = window.$b.snippets
snippets.castUserVote = castUserVote

if (window.module) {
  window.module.exports = castUserVote
}