Class: RasmusClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis, *namespace) ⇒ RasmusClient

Returns a new instance of RasmusClient.



8
9
10
11
12
# File 'lib/rasmus_client.rb', line 8

def initialize(redis, *namespace)
  @redis = redis
  @namespace = namespace
  @timeout = 60
end

Instance Attribute Details

#redisObject

Returns the value of attribute redis.



6
7
8
# File 'lib/rasmus_client.rb', line 6

def redis
  @redis
end

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/rasmus_client.rb', line 6

def timeout
  @timeout
end

Instance Method Details

#execute(cmd, input, *params) ⇒ Object



26
27
28
# File 'lib/rasmus_client.rb', line 26

def execute(cmd, input, *params)
  request(command: 'execute', path: cmd, input: input, params: params)
end

#key(*strings) ⇒ Object



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

def key(*strings)
  [*@namespace, 'rasmus', *strings].compact.map(&:to_s).join ':'
end

#list(path) ⇒ Object



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

def list(path)
  request(command: 'list', path: path)
end

#read(path) ⇒ Object



14
15
16
# File 'lib/rasmus_client.rb', line 14

def read(path)
  request(command: 'read', path: path)
end

#request(args = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/rasmus_client.rb', line 30

def request(args={})
  uuid = SecureRandom.uuid
  @redis.lpush(key('req'), args.merge(uuid: uuid).to_json)
  if raw_response = @redis.brpop(key('resp', uuid), @timeout)
    JSON.parse raw_response.last
  else
    nil
  end
end

#write(path, mode, data) ⇒ Object



18
19
20
# File 'lib/rasmus_client.rb', line 18

def write(path, mode, data)
  request(command: 'write', path: path, input: data, mode: mode)
end