Sha256: 5ddab49f9b6e0dcaf18e9301c11cfa1eb21f8ad34536295949c5961f65ab06e0

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'json'

module RapidsRivers

  # Understands a specifc message
  class Packet
    # The following keys are reserved for system usage:
    VISIT_COUNT = 'system_read_count'
    CONTRIBUTING_SERVICES = 'contributing_services'

    attr_reader :contributing_services
    protected :contributing_services

    def initialize(json_hash)
      @json_hash = json_hash
      @system_read_count = (@json_hash[VISIT_COUNT] || -1) + 1
      @contributing_services = @json_hash[CONTRIBUTING_SERVICES] || []
      @used_keys = [VISIT_COUNT, CONTRIBUTING_SERVICES]
    end

    def used_key(key)
      @used_keys << key.to_s
    end

    def clone_with_name(service_name)
      self.clone.tap { |packet_copy| packet_copy.contributing_services << service_name }
    end

    def to_json
      @used_keys.each { |key| @json_hash[key] = instance_variable_get("@#{key}".to_sym) if instance_variable_get("@#{key}".to_sym) }
      @json_hash.to_json
    end

    def to_s
      "Packet (in JSON): #{self.to_json}"
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rapids_rivers-0.1.7 lib/rapids_rivers/packet.rb