Sawtaytoes
11/24/2018 - 2:46 PM

Initial AJAX Refactor

Refactoring doScaryLightFlash into a more-generic base function that takes its fetch dependency as a composable argument.

const config = require('$config')
const lifxApi = require('./lifxApi')

const lifxEndpoint = (
  lifxApi
  .concat('/v1')
  .concat('/lights')
  .concat(`/${config.getLifxSelector()}`)
  .concat(':random/effects/breathe')
)

const headers = {
  Authorization: `Bearer ${config.getApiToken()}`,
  'Content-Type': 'application/json',
}

const getCycles = () => (
  Math
  .ceil(
    Math
    .random() * 3
  )
)

const getPeriod = () => 1

const createScaryLightFlasher = (
  ajaxFetcher,
) => (
  colorSet,
) => (
  ajaxFetcher(
    lifxEndpoint,
    {
      body: (
        JSON.stringify({
          ...colorSet,
          cycles: getCycles(),
          period: getPeriod(),
        })
      ),
      headers,
      method: 'POST',
    }
  )
)

module.exports = createScaryLightFlasher
const fetch = require('node-fetch')

const createScaryLightFlasher = require('./createScaryLightFlasher')

const doScaryLightFlash = (
  createScaryLightFlasher(
    fetch
  )
)

module.exports = doScaryLightFlash