urban-dictionary.js
function get (url, callback) {
  // https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_http_get_options_callback
  http.get(url, (result) => {
    const contentType = result.headers['content-type']
    const statusCode = result.statusCode

    let error

    if (statusCode !== 200) {
      error = new Error(`Unable to send request for definitions. Status code: '${statusCode}'`)
      error.code = 'ERR_REQUEST_SEND'
    } else if (contentType.indexOf('application/json') === -1) {
      error = new Error(`Content retrieved isn't JSON. Content type: '${contentType}'`)
      error.code = 'ERR_RESPONSE_NOT_JSON'
    }

    if (error) {
      // Removes response data to clear up memory.
      result.resume()
      callback(error)
      return
    }

    result.setEncoding('utf8')

    let rawData = ''

    result.on('data', (buffer) => {
      rawData += buffer
    })

    result.on('end', () => {
      let data = null
      let error = null

      try {
        data = JSON.parse(rawData)
      } catch (unusedError) {
        // In case somehow the data got set to not null. This is more of a failsafe.
        data = null
        error = new Error('Failed to parse retrieved Urban Dictionary JSON.')
        error.code = 'ERR_JSON_PARSE'
      }

      callback(error, data)
    })
  })
}
function get (url, callback) {
  // https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_http_get_options_callback
  http.get(url, (result) => {
    const contentType = result.headers['content-type']
    const statusCode = result.statusCode

    let error

    if (statusCode !== 200) {
      error = new Error(`Unable to send request for definitions. Status code: '${statusCode}'`)
      error.code = 'ERR_REQUEST_SEND'
    } else if (contentType.indexOf('application/json') === -1) {
      error = new Error(`Content retrieved isn't JSON. Content type: '${contentType}'`)
      error.code = 'ERR_RESPONSE_NOT_JSON'
    }

    if (error) {
      // Removes response data to clear up memory.
      result.resume()
      callback(error)
      return
    }

    result.setEncoding('utf8')

    let rawData = ''

    result.on('data', (buffer) => {
      rawData += buffer
    })

    result.on('end', () => {
      let data = null
      let error = null

      try {
        data = JSON.parse(rawData)
      } catch (unusedError) {
        // In case somehow the data got set to not null. This is more of a failsafe.
        data = null
        error = new Error('Failed to parse retrieved Urban Dictionary JSON.')
        error.code = 'ERR_JSON_PARSE'
      }

      callback(error, data)
    })
  })
}
function get ( url , callback ) { // https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_http_get_options_callback http . get ( url , ( result ) => { const contentType = result . headers [ 'content-type' ] const statusCode = result . statusCode let error if ( statusCode !== 200 ) { error = new Error ( ` ${ statusCode } ` ) error . code = 'ERR_REQUEST_SEND' } else if ( contentType . indexOf ( 'application/json' ) === - 1 ) { error = new Error ( ` ${ contentType } ` ) error . code = 'ERR_RESPONSE_NOT_JSON' } if ( error ) { // Removes response data to clear up memory. result . resume ( ) callback ( error ) return } result . setEncoding ( 'utf8' ) let rawData = '' result . on ( 'data' , ( buffer ) => { rawData += buffer } ) result . on ( 'end' , ( ) => { let data = null let error = null try { data = JSON . parse ( rawData ) } catch ( unusedError ) { // In case somehow the data got set to not null. This is more of a failsafe. data = null error = new Error ( 'Failed to parse retrieved Urban Dictionary JSON.' ) error . code = 'ERR_JSON_PARSE' } callback ( error , data ) } ) } ) }
Retrieves a JSON parsed object.
@param {string} url The full URL to retrieve from.
@param {function(error, object):void} callback
* argument[0] {error} error
* argument[1] {object} Parsed JSON result.
Retrieves a JSON parsed object .