Sha256: a4a9cef0b2b209672672f57ccb35d4e3b7c464b5f6104dc37cf7b97688e47819

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require_relative "../support"

module Moleculer
  module Packets
    ##
    # @abstract Subclass for packet types.
    class Base
      include Support
      ##
      # The protocol version
      attr_reader :ver

      ##
      # The sender of the packet
      attr_reader :sender

      def self.packet_name
        name.split("::").last.upcase
      end

      ##
      # @param data [Hash] the raw packet data
      # @options data [String] :ver the protocol version, defaults to `'3'`
      # @options  data [String] :sender the packet sender, defaults to `Moleculer#node_id`
      def initialize(data = {})
        @ver    = HashUtil.fetch(data, :ver, "3")
        @sender = HashUtil.fetch(data, :sender, Moleculer.config.node_id)
      end

      ##
      # The publishing topic for the packet. This is used to publish packets to the moleculer network. Override as
      # needed.
      #
      # @return [String] the pub/sub topic to publish to
      def topic
        "MOL.#{self.class.packet_name}"
      end

      def as_json
        {
          ver:    ver,
          sender: sender,
        }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
moleculer-0.2.0 lib/moleculer/packets/base.rb
moleculer-0.1.1 lib/moleculer/packets/base.rb
moleculer-0.1.0 lib/moleculer/packets/base.rb