Class: Praetor::ServiceClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_endpoint) ⇒ ServiceClient

Returns a new instance of ServiceClient.



8
9
10
11
12
13
# File 'lib/praetor/service_client.rb', line 8

def initialize(service_endpoint)
  @service_endpoint = service_endpoint

  client_options = ENV['REDIS_PROVIDER'] ? { url: ENV['REDIS_PROVIDER'] } : {}
  @redis = Redis.new client_options
end

Instance Attribute Details

#redisObject

Returns the value of attribute redis.



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

def redis
  @redis
end

#service_endpointObject

Returns the value of attribute service_endpoint.



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

def service_endpoint
  @service_endpoint
end

Instance Method Details

#request(path, params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/praetor/service_client.rb', line 15

def request(path, params = {})
  path_parts = path.split '#'
  req = {
    'key'        => Rubyflake.generate.to_s(32),
    'controller' => "#{path_parts.first.camelize}Controller",
    'action'     => path_parts.last,
    'params'     => params
  }

  redis.lpush service_endpoint, JSON.generate(req)
  _, response = redis.brpop req['key']
  parsed_response = Hashie::Mash.new JSON.parse(response)

  if parsed_response.status == 'ok'
    parsed_response.data
  else
    raise "Service error: #{parsed_response.data.message} #{parsed_response.data.backtrace.join("\n")}"
  end
end

#stop_serviceObject

Used primarily during testing to stop the run loop of a service



36
37
38
# File 'lib/praetor/service_client.rb', line 36

def stop_service
  redis.lpush service_endpoint, 'stop'
end