class Pheromone::Messaging::MessageFormatter

Constants

SUPPORTED_MESSAGE_FORMATS

Public Class Methods

new(message, encoder, format) click to toggle source
# File lib/pheromone/messaging/message_formatter.rb, line 8
def initialize(message, encoder, format)
  @message = message
  @encoder = encoder
  @message_format = format
end

Public Instance Methods

format() click to toggle source
# 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

Private Instance Methods

deep_transform_values!(object) { |object| ... } click to toggle source

recursively applies a block to a hash

# File lib/pheromone/messaging/message_formatter.rb, line 45
def deep_transform_values!(object, &block)
  case object
    when Array
      object.map! { |element| deep_transform_values!(element, &block) }
    when Hash
      object.each do |key, value|
        object[key] = deep_transform_values!(value, &block)
      end
    else
      yield object
  end
end
message_format() click to toggle source
# File lib/pheromone/messaging/message_formatter.rb, line 29
def message_format
  @message_format || Pheromone.config.message_format
end
message_with_time_conversion() click to toggle source

recursively converts time to the timezone set in configuration

# File lib/pheromone/messaging/message_formatter.rb, line 34
def message_with_time_conversion
  deep_transform_values!(@message) do |value|
    if value.is_a? Time
      value.in_time_zone(Pheromone.config.timezone)
    else
      value
    end
  end
end