Class: SlackBotServer::RedisQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_bot_server/redis_queue.rb

Overview

An implementation of the quue interface that uses Redis as a data conduit.

Instance Method Summary collapse

Constructor Details

#initialize(redis: nil, key: 'slack_bot_server:queue') ⇒ RedisQueue

Creates a new queue

Parameters:

  • redis (Redis) (defaults to: nil)

    an instance of the ruby Redis client. If nil, one will be created using the default hostname and port

  • key (String) (defaults to: 'slack_bot_server:queue')

    the key to store the queue against



10
11
12
13
14
15
16
17
18
# File 'lib/slack_bot_server/redis_queue.rb', line 10

def initialize(redis: nil, key: 'slack_bot_server:queue')
  @key = key
  @redis = if redis
    redis
  else
    require 'redis'
    Redis.new
  end
end

Instance Method Details

#clearnil

Clears the queue

Returns:

  • (nil)


40
41
42
# File 'lib/slack_bot_server/redis_queue.rb', line 40

def clear
  @redis.del @key
end

#popObject

Pop a value from the front of the queue

Returns:

  • (Object)

    the object on the queue, reconstituted from its JSON string



29
30
31
32
33
34
35
36
# File 'lib/slack_bot_server/redis_queue.rb', line 29

def pop
  json_value = @redis.lpop @key
  if json_value
    MultiJson.load(json_value, symbolize_keys: true)
  else
    nil
  end
end

#push(value) ⇒ Object

Push a value onto the back of the queue.

Parameters:

  • value (Object)

    this will be turned into JSON when stored



22
23
24
# File 'lib/slack_bot_server/redis_queue.rb', line 22

def push(value)
  @redis.rpush @key, MultiJson.dump(value)
end