Sha256: b8051a5b78c0370da2937eb549d7c1c97ee8cb7982d7ca18f9f7a8876ff0c868

Contents?: true

Size: 824 Bytes

Versions: 5

Compression:

Stored size: 824 Bytes

Contents

# frozen_string_literal: true

require "socket"

module Toycol
  class Client
    @port = 9292
    CHUNK_SIZE = 1024 * 16

    class << self
      attr_writer :port

      def execute!(request_message, &block)
        socket = TCPSocket.new("localhost", @port)
        socket.write(request_message)
        puts "[Toycol] Sent request message: #{request_message}\n---"

        response_message = []
        response_message << socket.readpartial(CHUNK_SIZE) until socket.eof?
        response_message = response_message.join

        block ||= default_proc
        block.call(response_message)
      ensure
        socket.close
      end

      private

      def default_proc
        proc do |message|
          puts "[Toycol] Received response message:\n\n"
          puts message
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
toycol-0.3.0 lib/toycol/client.rb
toycol-0.2.2 lib/toycol/client.rb
toycol-0.2.1 lib/toycol/client.rb
toycol-0.2.0 lib/toycol/client.rb
toycol-0.1.0 lib/toycol/client.rb