'use strict'

const GLOBAL_CRED = 'b66ca6f60fe049d6bedfe3e2ccb28d8c'
const ROLLBAR_URL = 'https://api.rollbar.com/api/1/item/'

function concat (stream, callback) {
  var strings = []
  stream.on('data', function (data) {
    strings.push(data)
  })
  stream.on('end', function () {
    callback(strings.join(''))
  })
}

function request (method, url, payload) {
  const https = require('https')
  const parseUrl = require('url').parse

  return new Promise(function (resolve, reject) {
    let body = JSON.stringify(payload)
    url = parseUrl(url)
    let req = https.request({
      hostname: url.hostname,
      path: url.path,
      method: method
    }, function (res) {
      concat(res, function (data) {
        if (res.statusCode >= 200 && res.statusCode < 300) resolve(data)
        else reject(data)
      })
    })
    req.setHeader('Content-Length', Buffer.byteLength(body))
    req.write(body)
  })
}

module.exports = function (cred, opts) {
  if (!cred) cred = GLOBAL_CRED

  function error (err) {
    const regex = /^\s*at (?:([^(]+(?: \[\w\s+\])?) )?\(?(.+?)(?::(\d+):(\d+)(?:, <js>:(\d+):(\d+))?)?\)?$/
    let frames = (err.stack || '').split('\n').map(function (line) {
      let r = line.match(regex)
      if (!r) return
      return {
        method: r[1],
        filename: r[2],
        lineno: r[3],
        colno: r[4]
      }
    })
    frames.shift() // discard first line
    return request('POST', ROLLBAR_URL, {
      access_token: cred,
      data: {
        code_version: opts.version,
        context: opts.context,
        platform: 'client',
        environment: 'production',
        framework: 'node',
        body: {
          trace: {
            exception: {
              class: err.name,
              message: err.message || err
            },
            frames}
        }
      }
    }).catch(err => require('./errors').log(err))
  }

  return {
    error
  }
}

@@ -125,202 +125,8 @@
 /'%0A%0A
-function concat (stream, callback) %7B%0A  var strings = %5B%5D%0A  stream.on('data', function (data) %7B%0A    strings.push(data)%0A  %7D)%0A  stream.on('end', function () %7B%0A    callback(strings.join(''))%0A  %7D)%0A%7D%0A%0A
 func
@@ -171,21 +171,19 @@
   const 
-https
+got
  = requi
@@ -190,527 +190,89 @@
 re('
-https')%0A  const parseUrl = require('url').parse%0A%0A  return new Promise(function (resolve, reject) %7B%0A    let body = JSON.stringify(payload)%0A    url = parseUrl(url)%0A    let req = https.request(%7B%0A      hostname: url.hostname,%0A      path: url.path,%0A      method: method%0A    %7D, function (res) %7B%0A      concat(res, function (data) %7B%0A        if (res.statusCode %3E= 200 && res.statusCode %3C 300) resolve(data)%0A        else reject(data)%0A      %7D)%0A    %7D)%0A    req.setHeader('Content-Length', Buffer.byteLength(body))%0A    req.write(body
+./got')%0A%0A  return got(url, %7B%0A    method: method,%0A    body: JSON.stringify(payload
 )%0A  

Replace rollbar https client with got client (#67)