class GorgService::Message::FormatterV1

Constants

DEFAULT_HEADERS
JSON_SCHEMA_V1

Public Class Methods

parse(delivery_info, properties, body) click to toggle source
# File lib/gorg_service/message/formatters.rb, line 172
def self.parse(delivery_info, properties, body)
  begin
    json_body=JSON.parse(body)
    JSON::Validator.validate!(JSON_SCHEMA_V1, json_body)

    msg=GorgService::Message.new(
        routing_key: delivery_info[:routing_key],
        reply_to: properties[:reply_to],
        correlation_id: properties[:correlation_id],
        sender_id: properties[:app_id],
        content_type: properties[:content_type],
        content_encoding: properties[:content_encoding],
        headers: properties[:headers],
        type: properties[:type],

        softfail_count: properties[:headers].to_h.delete('softfail-count'),

        id: json_body["event_uuid"],
        event_id: json_body["event_uuid"],
        event: json_body["event_name"],
        data: convert_keys_to_sym(json_body["data"]),
        creation_time: json_body["event_creation_time"] && DateTime.parse(json_body["event_creation_time"]),
        sender: json_body["event_sender_id"],
        errors: json_body["errors"]&&json_body["errors"].map{|e| GorgService::Message::ErrorLog.parse(e)},
    )
    msg
  rescue JSON::ParserError => e
    raise GorgService::Consumer::HardfailError.new("Unprocessable message : Unable to parse JSON message body", e)
  rescue JSON::Schema::ValidationError => e
    raise GorgService::Consumer::HardfailError.new("Invalid JSON : This message does not respect Gadz.org JSON Schema",e,{})
  end
end

Public Instance Methods

body() click to toggle source
# File lib/gorg_service/message/formatters.rb, line 153
def body
  b={
      event_uuid: message.id,
      event_name: message.routing_key,
      event_sender_id: message.sender,
      event_creation_time: message.creation_time.iso8601,
      data: message.data,
  }
  if message.errors.any?
    b[:errors_count]=message.errors.count
    b[:errors]=message.errors.map{|e| e.to_h}
  end
  b
end
payload() click to toggle source
# File lib/gorg_service/message/formatters.rb, line 168
def payload
  body.to_json
end
properties() click to toggle source
# File lib/gorg_service/message/formatters.rb, line 137
def properties
  {
      routing_key: message.routing_key,
      reply_to: message.reply_to,
      correlation_id: message.correlation_id,
      content_type: message.content_type,
      content_encoding: message.content_encoding,
      headers: DEFAULT_HEADERS.merge(message.headers.to_h).merge(
          'softfail-count' => message.softfail_count,
      ),
      app_id: message.sender_id,
      type: message.type,
      message_id: message.id,
  }
end