Class: ProcessBot::ClientSocket
- Inherits:
-
Object
- Object
- ProcessBot::ClientSocket
- Defined in:
- lib/process_bot/client_socket.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #client ⇒ Object
- #close ⇒ Object
-
#initialize(options:) ⇒ ClientSocket
constructor
A new instance of ClientSocket.
- #logger ⇒ Object
-
#send_command(data) ⇒ Object
rubocop:disable Metrics/AbcSize.
Constructor Details
#initialize(options:) ⇒ ClientSocket
Returns a new instance of ClientSocket.
6 7 8 |
# File 'lib/process_bot/client_socket.rb', line 6 def initialize(options:) @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/process_bot/client_socket.rb', line 4 def @options end |
Instance Method Details
#client ⇒ Object
10 11 12 |
# File 'lib/process_bot/client_socket.rb', line 10 def client @client ||= Socket.tcp("localhost", .fetch(:port).to_i, connect_timeout: 2) end |
#close ⇒ Object
14 15 16 |
# File 'lib/process_bot/client_socket.rb', line 14 def close client.close end |
#logger ⇒ Object
18 19 20 |
# File 'lib/process_bot/client_socket.rb', line 18 def logger @logger ||= ProcessBot::Logger.new(options: ) end |
#send_command(data) ⇒ Object
rubocop:disable Metrics/AbcSize
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/process_bot/client_socket.rb', line 22 def send_command(data) # rubocop:disable Metrics/AbcSize logger.log "Sending: #{data}" client.puts(JSON.generate(data)) response_raw = client.gets # Happens if process is interrupted return :nil if response_raw.nil? response = JSON.parse(response_raw) return :success if response.fetch("type") == "success" if response.fetch("type") == "error" error = RuntimeError.new("Command raised an error: #{response.fetch("message")}") error.set_backtrace(response.fetch("backtrace") + Thread.current.backtrace) raise error end end |