Class: Barrister::RedisContainer

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

Instance Method Summary collapse

Constructor Details

#initialize(json_path, handlers, options = {}) ⇒ RedisContainer

Returns a new instance of RedisContainer.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/barrister-redis.rb', line 38

def initialize(json_path, handlers, options={})
  options = {
    database_url: 'redis://localhost:6379',
    list_name: json_path.split('/')[-1].split('.')[0]
  }.merge(options)

  @list_name = options[:list_name]

  # establish connection to Redis
  @client = ::Redis.connect url: options[:database_url]

  # initialize service
  contract = Barrister::contract_from_file(json_path)
  @server  = Barrister::Server.new(contract)

  # in case we are passed a single handler
  handlers = handlers.kind_of?(Array) ? handlers : [handlers]

  # register each provided handler
  handlers.each do |handler|
    iface_name = handler.class.to_s.split('::').last
    @server.add_handler iface_name, handler
  end
end

Instance Method Details

#startObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/barrister-redis.rb', line 63

def start
  while true
    # pop last element off our list in a blocking fashion
    channel, request = @client.brpop(@list_name)

    parsed = JSON.parse request

    # reverse the message we were sent
    response = {
      'message' => @server.handle(parsed['message'])
    }

    # 'respond' by inserting our reply at the head of a 'reply'-list
    @client.lpush(parsed['reply_to'], JSON.generate(response))
  end
end