 Redis = require 'redis'
 
 module.exports = (robot) ->
-  # Configure redis the same way that redis-brain does.
-  info = Url.parse process.env.REDISTOGO_URL or
-    process.env.REDISCLOUD_URL or
-    process.env.BOXEN_REDIS_URL or
-    'redis://localhost:6379'
-  client = Redis.createClient(info.port, info.hostname)
-  client.auth info.auth.split(":")[1] if info.auth
-
-  buffer = new PantsBuffer(client)
+  buffer = new PantsBuffer()
 
   # We listen to everything.
   # Everything.

    @storage.zadd [@key(), stamp, body], (err, response) =>
       return console.log "WARNING: Failed to store: '#{who}: #{what}' @ #{stmp}: #{err}" if err

  trim: ->
    days = 3
    end = Date.now() - (
      days *  # Days
      24   *  # Hours

    console.log "=> Trimming #{@key()} 0 #{end} (Now: #{Date.now()})"
    @storage.zremrangebyscore @key(), 0, end, (err, response) =>
      return console.log "WARNING: Failed to trim '#{@key()}': #{err}" if err

  # Constructor for `PantsBuffer`
  #
  # Options:
  #   prefix: The redis prefix to use for the ringbuffer.
  #   adys:   The number of days to keep logs in the buffer
  constructor: (@options={}) ->
    # Connect to redis
    info = Url.parse process.env.REDISTOGO_URL or
      process.env.REDISCLOUD_URL or
      process.env.BOXEN_REDIS_URL or
      'redis://localhost:6379'
    @storage = Redis.createClient(info.port, info.hostname)
    @storage.auth info.auth.split(":")[1] if info.auth

    # Defaults for options
    @options.prefix ?= "ayp:"
    @options.days   ?= 3
