Class: Takagi::Serialization::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/takagi/serialization/base.rb

Overview

This class is abstract.

Subclass and implement all methods

Base interface for all content-format serializers

Serializers implement encoding/decoding logic for specific content-formats. Each serializer handles one CoAP content-format (MIME type).

Examples:

Implementing a custom serializer

class XmlSerializer < Takagi::Serialization::Base
  def encode(data)
    # Convert Ruby object to XML bytes
  end

  def decode(bytes)
    # Parse XML bytes to Ruby object
  end

  def content_type
    'application/xml'
  end

  def content_format_code
    41  # CoAP XML content-format
  end
end

Instance Method Summary collapse

Instance Method Details

#binary?Boolean

Check if this serializer can handle binary data

Returns:

  • (Boolean)

    true if binary format



78
79
80
# File 'lib/takagi/serialization/base.rb', line 78

def binary?
  false
end

#content_format_codeInteger

CoAP content-format code

Examples:

serializer.content_format_code  # => 50 (JSON)

Returns:

  • (Integer)

    CoAP content-format code from RFC 7252 §12.3

Raises:

  • (NotImplementedError)


71
72
73
# File 'lib/takagi/serialization/base.rb', line 71

def content_format_code
  raise NotImplementedError, "#{self.class}#content_format_code must be implemented"
end

#content_typeString

MIME type this serializer handles

Examples:

serializer.content_type  # => 'application/json'

Returns:

  • (String)

    MIME type (e.g., ‘application/json’)

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/takagi/serialization/base.rb', line 61

def content_type
  raise NotImplementedError, "#{self.class}#content_type must be implemented"
end

#decode(bytes) ⇒ Object

Decode bytes to Ruby object

Examples:

serializer.decode("\x{...}")  # => { temp: 25 }

Parameters:

  • bytes (String)

    Binary string to decode

Returns:

  • (Object)

    Decoded Ruby object

Raises:



51
52
53
# File 'lib/takagi/serialization/base.rb', line 51

def decode(bytes)
  raise NotImplementedError, "#{self.class}#decode must be implemented"
end

#encode(data) ⇒ String

Encode Ruby object to bytes

Examples:

serializer.encode({ temp: 25 })  # => "\x{...}"

Parameters:

  • data (Object)

    Ruby object to encode

Returns:

  • (String)

    Binary string (ASCII-8BIT encoding)

Raises:



39
40
41
# File 'lib/takagi/serialization/base.rb', line 39

def encode(data)
  raise NotImplementedError, "#{self.class}#encode must be implemented"
end

#nameString

Human-readable name for this serializer

Returns:

  • (String)

    Serializer name



85
86
87
# File 'lib/takagi/serialization/base.rb', line 85

def name
  self.class.name.split('::').last.sub(/Serializer$/, '')
end