Class: Barrister::RedisTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/barrister-redis.rb

Instance Method Summary collapse

Constructor Details

#initialize(list_name, options = {}) ⇒ RedisTransport

Returns a new instance of RedisTransport.



9
10
11
12
13
14
15
16
# File 'lib/barrister-redis.rb', line 9

def initialize(list_name, options={})
  options = {
    database_url: 'redis://localhost:6379'
  }.merge(options)

  @list_name = list_name
  @client = ::Redis.connect url: options[:database_url]
end

Instance Method Details

#request(message) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/barrister-redis.rb', line 18

def request(message)
  # reply-to tells the server where we'll be listening
  request = {
    'reply_to' => 'reply-' + SecureRandom.uuid,
    'message'  => message
  }

  # insert our request at the head of the list
  @client.lpush(@list_name, JSON.generate(request))

  # pop last element off our list in a blocking fashion
  channel, response = @client.brpop(request['reply_to'], timeout=30)

  JSON.parse(response)['message']
end