class Codeclimate::Collectors::Messages::Message

Public Class Methods

attribute(name, type, optional: false) click to toggle source

Declare an attribute on a message type. This will declare an attr_accessor, a validation that the attribute is of the specified type, and include it in #attributes. If optional is false a validation for presence is also added.

# File lib/codeclimate/collectors/messages/message.rb, line 17
def self.attribute(name, type, optional: false)
  name = name.to_sym

  attribute_metadata[name] = { type: type, optional: optional }

  attr_accessor name

  validates name, presence: !optional, type: { type: type }
end
attribute_metadata() click to toggle source
# File lib/codeclimate/collectors/messages/message.rb, line 9
def self.attribute_metadata
  @attribute_metadata ||= {}
end
json_type() click to toggle source

Calculate the type string to use in the JSON representation to identify this message type.

# File lib/codeclimate/collectors/messages/message.rb, line 29
def self.json_type
  to_s.gsub(/^.+::(\w+)$/, "\\1")
end
ordering_keys() click to toggle source

Messages can have ordering dependencies, e.g. a collector could emit an Incident message, followed by several IncidentEvent messages. IncidentEvent messages identify their associated incident via their incident_external_id attribute.

Our backend distributes messages amongst parallel workers for ingestion. To ensure messages with an ordering dependency are processed in order by the same worker, we can return an appropriate ordering_keys array for the classes: e.g. Incident returns [:external_id] and IncidentEvent returns [incident_external_id].

Collectors are also responsible for emitting messages in the appropriate order.

# File lib/codeclimate/collectors/messages/message.rb, line 46
def self.ordering_keys
  []
end

Public Instance Methods

==(other) click to toggle source
# File lib/codeclimate/collectors/messages/message.rb, line 50
def ==(other)
  other.class == self.class && other.attributes == attributes
end
as_json(*_opts) click to toggle source

A Hash for serializing the message as JSON. Includes a type key and an attributes key.

# File lib/codeclimate/collectors/messages/message.rb, line 67
def as_json(*_opts)
  {
    type: self.class.json_type,
    attributes: json_attributes,
  }
end
attributes() click to toggle source

Return all the attributes of a message as a Hash

# File lib/codeclimate/collectors/messages/message.rb, line 57
def attributes
  Hash[
    self.class.attribute_metadata.keys.each.map do |name|
      [name, public_send(name)]
    end
  ]
end

Protected Instance Methods

json_attributes() click to toggle source
# File lib/codeclimate/collectors/messages/message.rb, line 76
def json_attributes
  Hash[attributes.map { |name, val| [name, val.as_json] }]
end

Private Instance Methods

validate_not_base_class() click to toggle source
# File lib/codeclimate/collectors/messages/message.rb, line 82
def validate_not_base_class
  if instance_of?(Messages::Message)
    errors.add(:base, "base Message class is abstract")
  end
end