lib/cinc/client.rb in cinc-0.1.0 vs lib/cinc/client.rb in cinc-0.2.0

- old
+ new

@@ -2,12 +2,17 @@ require 'json' module Cinc class Client - def initialize(server = 'localhost', port = 9001) - @socket = TCPSocket.new(server, port) + def initialize(host: 'localhost', port: 9001) + @host = host + @port = port + end + + def connect + @socket = Socket.tcp(@host, @port, connect_timeout: 10) rescue StandardError => e raise ConnectionError.new(e.message) end def get_airbases @@ -18,10 +23,12 @@ private def process_command(command) @socket.puts command.to_json - return JSON.parse @socket.gets + response = @socket.gets + raise ConnectionError.new("Connection terminated by server") unless response + JSON.parse(response) rescue StandardError => e raise ConnectionError.new(e.message) end class Command