Sha256: 817f9d50349eed6af74651a6609a25ed1830dca13ccba1820f5baba7b9f61806

Contents?: true

Size: 836 Bytes

Versions: 2

Compression:

Stored size: 836 Bytes

Contents

# frozen_string_literal: true

module Toycol
  class Client
    extend Helper

    @port = 9292
    @host = "localhost"
    CHUNK_SIZE = 1024 * 16

    class << self
      attr_writer :port, :host

      def execute!(request_message, &block)
        socket = TCPSocket.new(@host, @port)
        socket.write(request_message)
        logger "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|
          logger "Received response message:\n\n"
          puts message
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
toycol-1.0.0 lib/toycol/client.rb
toycol-0.3.1 lib/toycol/client.rb