Module: Turbocable::Codecs

Defined in:
lib/turbocable/codecs.rb,
lib/turbocable/codecs/json.rb,
lib/turbocable/codecs/msgpack.rb

Overview

Registry for payload codecs. Each codec exposes:

.encode(payload) -> String (bytes)
.decode(bytes)   -> Object  (for tests / round-trips)
.content_type    -> String  (WebSocket sub-protocol name, informational)

Built-in codecs:

  • :json — always available, no extra dependencies

  • :msgpack — requires the msgpack gem (~> 1.7); loaded lazily on first use

Examples:

Fetch the JSON codec

codec = Turbocable::Codecs.fetch(:json)
bytes = codec.encode({ text: "hello" })

Fetch the MessagePack codec (requires msgpack gem)

codec = Turbocable::Codecs.fetch(:msgpack)
bytes = codec.encode({ text: "hello" })

Defined Under Namespace

Modules: JSON, MsgPack

Class Method Summary collapse

Class Method Details

.fetch(name) ⇒ Module

Returns the codec module for name.

:msgpack is loaded on first access; it raises LoadError if the msgpack gem is not installed.

Parameters:

  • name (Symbol, String)

Returns:

  • (Module)

    a codec module with .encode, .decode, .content_type

Raises:



43
44
45
46
47
48
49
50
51
# File 'lib/turbocable/codecs.rb', line 43

def self.fetch(name)
  key = name.to_sym
  return REGISTRY[key] if REGISTRY.key?(key)
  return load_lazy_codec!(key) if LAZY_CODECS.include?(key)

  raise ConfigurationError,
    "Unknown codec #{key.inspect}. " \
    "Available: #{registered.map(&:inspect).join(", ")}."
end

.registeredArray<Symbol>

Returns all codec names (eager + lazy).

Returns:

  • (Array<Symbol>)

    all codec names (eager + lazy)



54
55
56
# File 'lib/turbocable/codecs.rb', line 54

def self.registered
  (REGISTRY.keys + LAZY_CODECS).freeze
end