Class: Endoscope::Transport
- Inherits:
-
Object
- Object
- Endoscope::Transport
- Defined in:
- lib/endoscope/transport.rb
Constant Summary collapse
- ConnectionError =
Class.new(RuntimeError)
Instance Attribute Summary collapse
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#redis_opts ⇒ Object
readonly
Returns the value of attribute redis_opts.
Instance Method Summary collapse
-
#initialize(opts) ⇒ Transport
constructor
A new instance of Transport.
- #listen_to_responses ⇒ Object
- #publish_response(command, dyno_name, result) ⇒ Object
- #send_command(command_id, command, dyno_selector) ⇒ Object
- #wait_for_commands(dyno_name) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Transport
Returns a new instance of Transport.
11 12 13 14 |
# File 'lib/endoscope/transport.rb', line 11 def initialize(opts) @namespace = opts.delete(:namespace) || "endoscope" @redis_opts = opts end |
Instance Attribute Details
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
10 11 12 |
# File 'lib/endoscope/transport.rb', line 10 def namespace @namespace end |
#redis_opts ⇒ Object (readonly)
Returns the value of attribute redis_opts.
10 11 12 |
# File 'lib/endoscope/transport.rb', line 10 def redis_opts @redis_opts end |
Instance Method Details
#listen_to_responses ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/endoscope/transport.rb', line 47 def listen_to_responses connection.subscribe(responses_channel) do |on| on. do |_channel_name, | response = JSON.parse() yield(response) end end end |
#publish_response(command, dyno_name, result) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/endoscope/transport.rb', line 38 def publish_response(command, dyno_name, result) connection.publish(responses_channel, JSON.generate( id: command.fetch('id'), command: command.fetch('command'), dyno_name: dyno_name, result: result )) end |
#send_command(command_id, command, dyno_selector) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/endoscope/transport.rb', line 29 def send_command(command_id, command, dyno_selector) channel = requests_channel(dyno_selector) connection.publish(channel, JSON.generate( id: command_id, command: command, channel: channel )) end |
#wait_for_commands(dyno_name) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/endoscope/transport.rb', line 16 def wait_for_commands(dyno_name) channels = command_channels(dyno_name) connection.subscribe(*channels) do |on| on. do |_channel, | # puts "##{channel}: #{message}" command = JSON.parse() yield(command) end end rescue Redis::BaseConnectionError => error raise ConnectionError, error., error end |