Class: Krakow::FrameType Abstract

Inherits:
Object
  • Object
show all
Extended by:
Utils::Lazy::ClassMethods
Includes:
Utils::Lazy, Utils::Lazy::InstanceMethods
Defined in:
lib/krakow/frame_type.rb,
lib/krakow/frame_type/error.rb,
lib/krakow/frame_type/message.rb,
lib/krakow/frame_type/response.rb

Overview

This class is abstract.

Received message

Direct Known Subclasses

Error, Message, Response

Defined Under Namespace

Classes: Error, Message, Response

Constant Summary

FRAME_TYPE_MAP =

Registered frame types

[
  FrameType::Response,
  FrameType::Error,
  FrameType::Message
]
SIZE_BYTES =

Size bytes

4

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Hash) arguments (readonly) Originally defined in module Utils::Lazy::InstanceMethods

Returns argument hash

Returns:

  • (Hash)

    argument hash

Class Method Details

+ (nil) attribute(name, type, options = {}) Originally defined in module Utils::Lazy::ClassMethods

Add new attributes to class

Parameters:

  • name (String)
  • type (Class, Array<Class>)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :required (true, false)

    must be provided on initialization

  • :default (Object, Proc)

    default value

Returns:

  • (nil)

+ (Array<Hash>) attributes(*args) Originally defined in module Utils::Lazy::ClassMethods

Return attributes

Parameters:

  • args (Symbol)

    :required or :optional

Returns:

  • (Array<Hash>)

+ (FrameType) build(args = {})

Build proper FrameType instance based on args

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :type (FrameType)

    class of frame

  • :data (String)
  • :size (Integer)

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/krakow/frame_type.rb', line 41

def build(args={})
  klass = FRAME_TYPE_MAP[args[:type].to_i]
  if(klass == FrameType::Response)
    klass.new(:response => args[:data])
  elsif(klass == FrameType::Error)
    klass.new(:error => args[:data])
  elsif(klass == FrameType::Message)
    unpacked = args[:data].unpack("Q>s>a16a#{args[:size]}")
    klass.new(
      Hash[*([:timestamp, :attempts, :message_id, :message].zip(unpacked).flatten)]
    )
  else
    raise TypeError.new "Unknown frame type received: #{args[:type].inspect} - #{klass.inspect}"
  end
end

+ (Hash) decode(bytes)

Information about incoming frame

Parameters:

  • bytes (String)

Returns:

  • (Hash)


30
31
32
33
# File 'lib/krakow/frame_type.rb', line 30

def decode(bytes)
  size, type = bytes.unpack('l>l>')
  {:size => size - SIZE_BYTES, :type => type}
end

+ (TrueClass) set_attributes(attrs) Originally defined in module Utils::Lazy::ClassMethods

TODO:

need deep dup here

Directly set attribute hash

Parameters:

  • attrs (Hash)

Returns:

  • (TrueClass)

Instance Method Details

- (String) content

Content of message

Returns:

  • (String)


61
62
63
# File 'lib/krakow/frame_type.rb', line 61

def content
  raise NotImplementedError.new 'Content method not properly defined!'
end

- (Object) initialize(args = {}) Also known as: super_init Originally defined in module Utils::Lazy::InstanceMethods

Create new instance

Parameters:

  • args (Hash) (defaults to: {})

Returns:

  • (Object)

- (String) inspect Originally defined in module Utils::Lazy::InstanceMethods

Returns:

  • (String)

- (Logger?) log(*args) Originally defined in module Utils::Logging

Log message

Parameters:

  • args (Array, nil)

Returns:

  • (Logger, nil)

- (String) to_s Originally defined in module Utils::Lazy::InstanceMethods

Returns:

  • (String)