class GorgService::Message
Constants
- DEFAULT_SOA_VERSION
Attributes
@return [String] Identifier of the admin who triggered the generation of this message @note In case of autonomous actions, there is no admin_id
@return [String] identifier of the library used to generate this message, for debug and compatibility purpose
@return [String] Content encoding of the payload @note The only content encoding officially supported by Gadz.org SOA is 'deflate'
@return [String] Content type of the payload @note The only content type officially supported by Gadz.org SOA is 'application/json'
@return [String] UUID of the message the this message refer to (reply or log)
@return [DateTime] Message
generation datetime
@return [Hash] Data of the payload as an hash @note for further compatibility, don't assume that data is an hash but check Content-Type
@deprecated In Gadz.org Soa v2.0 errors are not store in messages anymore
@return [Array<Message::ErrorLog>] List of errors associated to this message
@deprecated Use {#routing_key} instead. event is no longer a part of GorgSOA specs @return [String] the name of the event
@deprecated Use {#id} instead. event_id
is no longer a part of GorgSOA specs @return [String] UUID of the message
@return [Hash] Additional headers
@return [String] UUID of the message
@return [String] Name of the exchange used to receive reply
@return [String] Name of the exchange used to receive reply
@return [String] Routing of the message
@return [String] Id of the app which generated the message
@return [String] Id of the app which generated the message
@return [String] Id of the app which generated the message
@return [String] The Gadz.org SOA specs version used to generate this message @note Incomming message without this information should be considered v1.0
@return [Integer] Number of softfails associated to this message
@return [Hash] Mapping of attributes errors in regard of Gadz.org SOA v2
Public Class Methods
@param opts [Hash] Attributes of the message @option opts [String] :id See {#id}. Default : Random UUID4 @option opts [Array<Message::ErrorLog>] :errors See {#errors}. Default : [] @option opts [DateTime] :creation_time See {#creation_time}. Default : DateTime.now @option opts [String] :sender See {#sender}. Default : GorgService.configuration
.application_id @option opts [String] :data See {#data}. Default : nil @option opts [String] :routing_key See {#routing_key}. Default : nil @option opts [String] :reply_to See {#reply_to}. Default : nil @option opts [String] :correlation_id See {#correlation_id}. Default : nil @option opts [String] :content_type See {#content_type}. Default : “application/json” @option opts [String] :content_encoding See {#content_encoding}. Default : “deflate” @option opts [String] :headers See {#headers}. Default : {}
# File lib/gorg_service/message.rb, line 162 def initialize(opts={}) self.id= opts.fetch(:event_id,nil)||opts.fetch(:id,generate_id) self.errors= opts.fetch(:errors,[]) self.creation_time= opts.fetch(:creation_time,DateTime.now) self.sender= opts.fetch(:sender,application_id) self.data= opts.fetch(:data,nil) self.routing_key= opts.fetch(:routing_key,nil)||opts.fetch(:event,nil) self.softfail_count= opts.fetch(:softfail_count,0) self.reply_to= opts.fetch(:reply_to,nil) self.correlation_id= opts.fetch(:correlation_id,nil) self.content_type= opts.fetch(:content_type,"application/json") self.content_encoding= opts.fetch(:content_encoding,"deflate") self.headers= opts.fetch(:headers,{}) self.soa_version= opts.fetch(:soa_version, DEFAULT_SOA_VERSION) end
@param [Hash] delivery_info delivery_info provided by {Bunny} @param [Hash] properties properties provided by {Bunny} @param [Hash] body body provided by {Bunny} @param [Class] formatter_class The formatter to be used to parse the message. Default to {Message::FormatterV1} @return [GorgService::Message] A kind of GorgService::Message
# File lib/gorg_service/message.rb, line 295 def self.parse(delivery_info, properties, body) formatter_class=Message::Formatter.formatter_for_version(properties.to_h[:headers].to_h["soa-version"]||"1") formatter_class.parse(delivery_info, properties, body) end
Public Instance Methods
@deprecated Please use directly the rendering interface of the formatter @param formatter [Message::FormatterV1] The formatter to be used. Default : an instance of Message::FormatterV1
@return [Hash] The un-serialized payload of the RabbitMq message
# File lib/gorg_service/message.rb, line 183 def body Message::Formatter.formatter_for_version(self.soa_version).new(self).body end
# File lib/gorg_service/message.rb, line 125 def event=(value) warn "[DEPRECATION] Message.event is deprecated and will be removed soon. Use id instead (called from #{caller_locations(1,1)[0]})" self.routing_key=value end
# File lib/gorg_service/message.rb, line 139 def event_id=(value) warn "[DEPRECATION] Message.event_id is deprecated and will be removed soon. Use id instead (called from #{caller_locations(1,1)[0]})" self.id=value end
@return [Boolean] Does this message expect a reply ?
# File lib/gorg_service/message.rb, line 212 def expect_reply? !!reply_to end
Log FailError in message body
# File lib/gorg_service/message.rb, line 202 def log_error error e=GorgService::Message::ErrorLog.new( type: error.type.downcase, message: error.message||"", debug: error.error_raised && {internal_error: error.error_raised.inspect} ) errors<<e end
# File lib/gorg_service/message.rb, line 233 def log_message(opts) args= { routing_key: self.reply_routing_key, correlation_id: self.id, soa_version: '2.0' #v1 messages generate v2 logs }.merge(opts) GorgService::LogMessage.new(args) end
@deprecated Please use directly the rendering interface of the formatter @param formatter [Message::FormatterV1] The formatter to be used. Default : an instance of Message::FormatterV1
@return [Hash] The properties of the RabbitMq message
# File lib/gorg_service/message.rb, line 192 def properties(formatter: Message::FormatterV1.new(self)) Message::Formatter.formatter_for_version(self.soa_version).new(self).properties end
@return the response to send
# File lib/gorg_service/message.rb, line 223 def reply_message(opts) args= { routing_key: self.reply_routing_key, correlation_id: self.id, soa_version: self.soa_version }.merge(opts) GorgService::ReplyMessage.new(args) end
@return [Stirng] Routing key to use to reply to this message
# File lib/gorg_service/message.rb, line 217 def reply_routing_key routing_key.sub('request','reply') end
@return [String] Major version of the soa version
# File lib/gorg_service/message.rb, line 69 def soa_version_major (self.soa_version||DEFAULT_SOA_VERSION).split('.')[0] end
@return [String] The serialized (JSON) value of the RabbitMQ payload
# File lib/gorg_service/message.rb, line 197 def to_json self.to_h.to_json end
@return [nil] Type of message (event,log,request,reply). To be overwritten by children classes
# File lib/gorg_service/message.rb, line 98 def type nil end
Validate the message against rules specified in {confluence.gadz.org/display/INFRA/Messages the Gadz.org SOA Message
Specification}
# File lib/gorg_service/message.rb, line 244 def validate self.validation_errors= Hash.new([].freeze) self.validation_errors[:content_type]+=["is not supported by Gadz.org SOA"] unless ['application/json'].include? self.content_type self.validation_errors[:content_encoding]+=["is not supported by Gadz.org SOA"] unless ['deflate','gzip'].include? self.content_encoding # "gzip" is not officially supported by Gadz.org but I don't feel comfortable blocking it :) self.validation_errors[:id]+=["is null"] unless self.id self.validation_errors[:id]+=["is not a UUID"] unless !self.id || /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/.match(self.id) self.validation_errors[:correlation_id]+=["is not a UUID"] unless !self.correlation_id || /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/.match() self.validation_errors[:creation_time]+=["is not DateTime"] unless self.creation_time.is_a? DateTime self.validation_errors[:type]+=["is not in (event, log, request, reply)"] unless %w(event log request reply).include? self.type self.validation_errors[:sender]+=["is null"] unless self.sender self.validation_errors[:soa_version]+=["is null"] unless self.soa_version return self.validation_errors.empty? end
@see validate
@raise [MessageValidationError] Raise an exception containing the errors if the message is invalid
# File lib/gorg_service/message.rb, line 270 def validate! raise MessageValidationError.new(self.errors) unless validate end
@param [String,Hash] A JSON Schema usable by {JSON::Validator} @return [Boolean] True if the message is valid against the provided JSON Schema @raise [DataValidationError] Validation exception containing errors ( See {DataValidationError#errors})
# File lib/gorg_service/message.rb, line 277 def validate_data_with(schema) data_validation_errors=JSON::Validator.fully_validate(schema, self.data) if data_validation_errors.any? raise DataValidationError.new(data_validation_errors) else return true end end
Protected Instance Methods
# File lib/gorg_service/message.rb, line 306 def application_id GorgService.configuration.application_id end
Generate new id
# File lib/gorg_service/message.rb, line 302 def generate_id SecureRandom.uuid() end