import json

import redis


class RedisManager(object):
    @staticmethod
    def get_redis_connection(vcap_env, heroku_redis_url):
        if vcap_env:
            redis_env = json.loads(vcap_env)['rediscloud'][0]['credentials']
            return redis.Redis(host=redis_env['hostname'],
                               port=int(redis_env['port']),
                               password=redis_env['password'])
        elif heroku_redis_url:
            return redis.Redis.from_url(heroku_redis_url)
        else:
            return redis.Redis(host='localhost', port=6379, db=0)
