krainboltgreene/whiskey

View on GitHub
lib/whiskey/server/connection.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Whiskey
  class Server
    # The Connection class is used to talk to the request. It knows how to
    # read from and write to it's socket.
    class Connection
      extend Forwardable

      attr_reader :socket

      def initialize(socket)
        @socket = socket
      end

      def id
        "#{ip}:#{port}"
      end

      def process
        write run.output
      end

      private

      def run
        Cycle.new(incoming).tap(&:interpret!)
      end

      def incoming
        readpartial(4096)
      end

      def port
        peeraddr[1]
      end

      def ip
        peeraddr[3]
      end

      def write(*args)
        socket.write(*args)
      end

      def readpartial(*args)
        socket.readpartial(*args)
      end

      def peeraddr(*args)
        socket.peeraddr(*args)
      end

      def close
        socket.close
      end
    end
  end
end