Class: Praetor::ServiceClient
- Inherits:
-
Object
- Object
- Praetor::ServiceClient
- Defined in:
- lib/praetor/service_client.rb
Instance Attribute Summary collapse
-
#redis ⇒ Object
Returns the value of attribute redis.
-
#service_endpoint ⇒ Object
Returns the value of attribute service_endpoint.
Instance Method Summary collapse
-
#initialize(service_endpoint) ⇒ ServiceClient
constructor
A new instance of ServiceClient.
- #request(path, params = {}) ⇒ Object
-
#stop_service ⇒ Object
Used primarily during testing to stop the run loop of a service.
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 = ENV['REDIS_PROVIDER'] ? { url: ENV['REDIS_PROVIDER'] } : {} @redis = Redis.new end |
Instance Attribute Details
#redis ⇒ Object
Returns the value of attribute redis.
6 7 8 |
# File 'lib/praetor/service_client.rb', line 6 def redis @redis end |
#service_endpoint ⇒ Object
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.} #{parsed_response.data.backtrace.join("\n")}" end end |
#stop_service ⇒ Object
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 |