Class: Pheromone::Messaging::MessageFormatter

Inherits:
Object
  • Object
show all
Includes:
Pheromone::MethodInvoker
Defined in:
lib/pheromone/messaging/message_formatter.rb

Constant Summary collapse

SUPPORTED_MESSAGE_FORMATS =
[:json, :with_encoding].freeze

Instance Method Summary collapse

Methods included from Pheromone::MethodInvoker

included

Constructor Details

#initialize(message, encoder, format) ⇒ MessageFormatter

Returns a new instance of MessageFormatter.



8
9
10
11
12
# File 'lib/pheromone/messaging/message_formatter.rb', line 8

def initialize(message, encoder, format)
  @message = message
  @encoder = encoder
  @message_format = format
end

Instance Method Details

#formatObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pheromone/messaging/message_formatter.rb', line 14

def format
  if message_format == :json
    message_with_time_conversion.to_json
  elsif message_format == :with_encoding
    call_proc_or_instance_method(
      @encoder,
      message_with_time_conversion.with_indifferent_access
    )
  elsif !SUPPORTED_MESSAGE_FORMATS.include?(Pheromone.config.message_format)
    raise Pheromone::Exceptions::UnsupportedMessageFormat.new
  end
end