Sha256: 3528c9734481877a7197a94c0446fde82f71ef681a10688c4a4c9a0e1b00c375

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

module KJess
  # Protocl is the base class that all Kestrel requests and responses are
  # developed on. it defines the DSL for creating the Request and Response
  # objects that make up the Protocol.
  #
  class Protocol

    CRLF = "\r\n"

    class << self
      # Internal: The keyword that starts this protocol message
      #
      # name - the keyword to define this portion of the protocol
      #
      # Returns the name
      def keyword( name = nil )
        if name then
          register( name )
          @keyword = name
        end
        @keyword
      end

      # Internal: define or return the arity of this protocol item
      #
      # arity - the number of args this protocol item has
      #
      # Returns the arity
      def arity( a = nil )
        @arity = a if a
        @arity
      end

      # Internal: register this protocol item with its registry
      #
      # name - the name under which to register the protocol
      #
      # Returns nothing
      def register( name )
        registry[name] ||= self
      end
    end

    attr_reader :args
    attr_reader :raw_args

    def initialize( opts = {} )
      @raw_args = opts
      @args = parse_options_to_args( opts ) || []
    end

    # Internal: callback that child classes may use to further parse the
    # initialization arguments
    #
    # Returns Array
    def parse_options_to_args( opts ); end

    # Internal: Convert the object to its protocol serialized format.
    #
    # This may be overridden in child classes
    #
    # Return a String
    def to_protocol
      s = keyword
      s += " #{args.join(' ')}" unless args.empty?
      s += CRLF
    end

    # Internal: return the keyword
    #
    # Returns a String
    def keyword
      self.class.keyword
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kjess-1.0.0 lib/kjess/protocol.rb