Source: get-uv-index.js

/**
 * @function getUVIndex
 * @summary
 * Use this function to get the UV index for a particular zip code.
 * If no zipCode is supplied in the options object, then the Ad Parameters GeoInfo is used.
 * @description
 * Options Object:
 * * zipCode:
 *    * Type: number
 *    * Requirements: Optional
 *    * Description: The zip code to get the UV index for.
 *
 * * timeout:
 *    * Type: number
 *    * Requirements: Optional
 *    * Description: Amount of time (in seconds) to wait for the UV index API to return results before timing out. (default 3 seconds)
 *
 * Events:
 * Success: Listen for the 'UV_INDEX_RESULTS' event using window.$b.on('UV_INDEX_RESULTS', callback) to run callback function when results are returned.
 * Error: Listen for the 'UV_INDEX_ERROR' event using window.$b.on('UV_INDEX_ERROR', callback) to run callback function when an error occurrs.
 *
 * Example Usage:
 * ```
 * window.$b.snippets.getUVIndex()
 * window.$b.on('UV_INDEX_RESULTS', function (data) {
 *   console.log(data.zipCode)
 *   console.log(data.uvIndex)
 *   console.log(data.uvExpCat)
 *   console.log(data.uvAlert)
 * })
 * ```
 * @param {Object=} opts Object containing all of the params to pass into the function
 * @param {string=} options.zipCode optional zip code to overrride value in Ad Parameters
 * @param {number=} options.timeout (default 3 seconds) maximum seconds to wait for api results
 */

function getUVIndex (options) {
  options = options || {}
  var eventName = 'UV_INDEX_RESULTS'
  var errorEvent = 'UV_INDEX_ERROR'
  var blink = window.$b || {}
  var adParameters = blink.adParameters || {}
  var zipCode = options.zipCode
  var isZipValid = function (zip) {
    return /^\d{5,5}$/.test(zip)
  }
  if (!isZipValid(zipCode)) {
    var message
    if (adParameters.geoInfo && adParameters.geoInfo.postalCode) {
      zipCode = adParameters.geoInfo.postalCode
      if (!isZipValid(zipCode)) {
        message = 'Invalid zip code: ' + zipCode
        console.error(errorEvent, message)
        try {
          window.$b.emit(errorEvent, message)
        } catch (err) {
          console.error(err)
        }
        return Promise.reject(new Error(message))
      }
    } else {
      message = 'Ad Parameter has no geoInfo.postalCode'
      console.error(errorEvent, message)
      try {
        window.$b.emit(errorEvent, message)
      } catch (err) {
        console.error(err)
      }
      return Promise.reject(new Error(message))
    }
  }
  var timeout = options.timeout
  if ((typeof timeout !== 'number') || (timeout <= 0)) {
    timeout = 3000
  } else {
    timeout *= 1000
  }

  var prepRetData = function (data) {
    var zipCode = data[0].ZIP_CODE
    var uvIndex = data[0].UV_INDEX
    var uvAlert = data[0].UV_ALERT
    var uvExpCat
    if (uvIndex < 2) {
      uvExpCat = 'Low'
    } else if (uvIndex >= 3 || uvIndex <= 5) {
      uvExpCat = 'Moderate'
    } else if (uvIndex === 6 || uvIndex === 7) {
      uvExpCat = 'High'
    } else if (uvIndex >= 8 || uvIndex <= 10) {
      uvExpCat = 'Very High'
    } else if (uvIndex >= 11) {
      uvExpCat = 'Extreme'
    }
    return { zipCode: zipCode, uvIndex: uvIndex, uvAlert: uvAlert, uvExpCat: uvExpCat }
  }

  var apiUrl = 'https://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVDAILY/ZIP/' + zipCode + '/json'
  return new Promise(function (resolve, reject) {
    $.ajax({
      url: apiUrl,
      method: 'GET',
      timeout: timeout
    })
      .fail(function (err) {
        var message = apiUrl + ': ' + err.statusText
        console.error(errorEvent, message)
        reject(err)
        window.$b.emit(errorEvent, new Error(message))
      })
      .done(function (data) {
        if (data instanceof Array && data.length > 0) {
          var newData = prepRetData(data)
          try {
            window.$b.emit(eventName, newData)
          } catch (err) {
            console.error(err)
          }
        } else {
          var message = 'data is not of expected type (array) or data.length = 0'
          try {
            console.error(errorEvent, message)
            window.$b.emit(errorEvent, new Error(message))
          } catch (err) {
            console.error(errorEvent, message)
          }
        }
      })
  })
} // End - function getUVIndex

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

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