require 'socket' require 'json' module Cinc class Client def initialize(server = 'localhost', port = 9001) @socket = TCPSocket.new(server, port) 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 return JSON.parse @socket.gets 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