Sha256: f69fe872fcda364e6333bfe5e774415492100f66ffaf101560833f815c67f08e

Contents?: true

Size: 874 Bytes

Versions: 3

Compression:

Stored size: 874 Bytes

Contents

require 'redis'

module Headsail
  class Redis
    attr_reader :connection

    def initialize(key_name)
      @key_name = key_name
      setup_connection
      setup_service
      setup_next_id
    end

    def add(data)
      @connection.incr(next_id_key)
      @connection.set(new_key, data)
    end

    private

    def new_key
      "#{@key_name}:#{next_id}"
    end

    def next_id
      @connection.get(next_id_key)
    end

    def next_id_key
      @next_id_key ||= "#{@key_name}:next_id"
    end

    def setup_connection
      @connection = ::Redis.new(url: ENV['REDIS_URL'])
    end

    def setup_service
      return if @connection.exists(@key_name)
      @connection.set(@key_name, true)
      @connection.lpush('apis', @key_name)
    end

    def setup_next_id
      @connection.set(next_id_key, 0) unless @connection.exists(next_id_key)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
headsail-0.1.11 lib/headsail/redis.rb
headsail-0.1.1 lib/headsail/redis.rb
headsail-0.1.0 lib/headsail/redis.rb