require 'socket' require 'json' module Cinc class Client 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 command = Command.new('get_airbases', {}) return process_command(command) end private def process_command(command) @socket.puts command.to_json 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 attr_accessor :name, :arguments def initialize(name, arguments) @name = name @arguments = arguments end def to_json { name: name, arguments: arguments }.to_json end end end end