Class: GorgMessageSender

Inherits:
Object
  • Object
show all
Defined in:
lib/gorg_message_sender/configuration.rb,
lib/gorg_message_sender.rb,
lib/gorg_message_sender/version.rb,
lib/gorg_message_sender/json_schema.rb

Overview

Add configuration features to GorgService

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
"1.4.5"
JSON_SCHEMA =
JSON.parse('{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "event_name": {
      "type": "string",
      "pattern": "^[_a-z]+((\\.)?[_a-z]+)*$",
      "description": "Event type. Must match the routing key"
    },
    "event_uuid": {
      "type": "string",
      "description": "The unique identifier of this message as UUID",
      "pattern": "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
    },
    "event_creation_time": {
      "type": "string",
      "description": "Creation time in UTC ISO 8601 format",
      "pattern": "^([\\\+-]?\\\d{4}(?!\\\d{2}\\\b))((-?)((0[1-9]|1[0-2])(\\\3([12]\\\d|0[1-9]|3[01]))?|W([0-4]\\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\\d|[12]\\\d{2}|3([0-5]\\\d|6[1-6])))([T\\\s]((([01]\\\d|2[0-3])((:?)[0-5]\\\d)?|24\\\:?00)([\\\.,]\\\d+(?!:))?)?(\\\17[0-5]\\\d([\\\.,]\\\d+)?)?([zZ]|([\\\+-])([01]\\\d|2[0-3]):?([0-5]\\\d)?)?)?)?$"
    },
    "event_sender_id": {
      "type": "string",
      "description": "Producer that sent the original message"
    }, 
    "data": {
      "type": "object",
      "description": "Data used to process this message"
    },
    "errors_count": {
      "type": "integer",
      "description": "Helper for counting errors"
    },
    "errors": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "error_type": {
            "enum": [  "debug", "info", "warning", "softerror", "harderror" ],
            "description": "Type of error."
          },
          "error_sender": {
            "type": "string",
            "description": "Consummer that sent this error"
          },
          "error_code":{
            "type":"string",
            "description": "Optionnal error code from the consummer"
          },
          "error_uuid":{
            "type":"string",
            "description": "The unique identifier of this error as UUID",
            "pattern": "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"
          },
          "error_message":{
            "type":"string",
            "description": "Error explanation"
          },          
          "timestamp": {
            "type": "string",
            "description": "Time of occuring error in UTC ISO 8601",
            "pattern": "^([\\\+-]?\\\d{4}(?!\\\d{2}\\\b))((-?)((0[1-9]|1[0-2])(\\\3([12]\\\d|0[1-9]|3[01]))?|W([0-4]\\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\\d|[12]\\\d{2}|3([0-5]\\\d|6[1-6])))([T\\\s]((([01]\\\d|2[0-3])((:?)[0-5]\\\d)?|24\\\:?00)([\\\.,]\\\d+(?!:))?)?(\\\17[0-5]\\\d([\\\.,]\\\d+)?)?([zZ]|([\\\+-])([01]\\\d|2[0-3]):?([0-5]\\\d)?)?)?)?$"
          },
          "error_debug": {
            "type": "object",
            "description": "Complementary informations for debugging"
          }
        },
        "additionalProperties": false,
        "required": [
          "error_type",
          "error_sender",
          "timestamp",
          "error_uuid",
          "error_message"
        ]
      }
    }
  },
  "additionalProperties": false,
  "required": [
    "event_name",
    "event_uuid",
    "event_creation_time",
    "event_sender_id",
    "data"
  ]
}')

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: self.class.configuration.host, port: self.class.configuration.port, user: self.class.configuration.user, pass: self.class.configuration.password, exchange_name: self.class.configuration.exchange_name, vhost: self.class.configuration.vhost, app_id: self.class.configuration.application_id, durable_exchange: self.class.configuration.durable_exchange) ⇒ GorgMessageSender

Returns a new instance of GorgMessageSender.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gorg_message_sender.rb', line 9

def initialize(host: self.class.configuration.host,
               port: self.class.configuration.port,
               user: self.class.configuration.user,
               pass:self.class.configuration.password,
               exchange_name:self.class.configuration.exchange_name,
               vhost: self.class.configuration.vhost,
               app_id: self.class.configuration.application_id,
               durable_exchange: self.class.configuration.durable_exchange)
  @r_host=host
  @r_port=port
  @r_user=user
  @r_pass=pass
  @r_exchange=exchange_name
  @r_vhost=vhost
  @app_id=app_id
  @r_durable=durable_exchange
end

Class Attribute Details

.configurationObject



6
7
8
# File 'lib/gorg_message_sender/configuration.rb', line 6

def configuration
  @configuration ||= GorgMessageSender::Configuration.new
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



11
12
13
14
# File 'lib/gorg_message_sender/configuration.rb', line 11

def configure
  @configuration = GorgMessageSender::Configuration.new
  yield(configuration)
end

Instance Method Details

#send_batch_raw(msgs, opts = {}) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/gorg_message_sender.rb', line 59

def send_batch_raw(msgs,opts={})
  self.start(verbose: opts[:verbose])
  p_opts={}
  msgs.each do |msg|
    @x.publish(msg[:content], routing_key: msg[:routing_key] )
  end
end

#send_message(data, routing_key, opts = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/gorg_message_sender.rb', line 40

def send_message(data,routing_key,opts={})
  self.start(verbose: opts[:verbose])
  p_opts={}
  p_opts[:routing_key]= routing_key if routing_key
  msg=self.to_message(data,routing_key,opts)
  @x.publish(msg, p_opts)
  puts " [#] Message sent to exchange '#{@r_exchange}' (#{@r_durable ? "" : "not "}durable) with routing key '#{routing_key}'" if opts[:verbose]
  msg
end

#send_raw(msg, routing_key, opts = {}) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/gorg_message_sender.rb', line 50

def send_raw(msg,routing_key,opts={})
  self.start(verbose: opts[:verbose])
  p_opts={}
  p_opts[:routing_key]= routing_key if routing_key
  @x.publish(msg, p_opts)
  puts " [#] Message sent to exchange '#{@r_exchange}' (#{@r_durable ? "" : "not "}durable) with routing key '#{routing_key}'" if opts[:verbose]
  msg
end

#to_message(data, routing_key, opts = {}) ⇒ Object Also known as: message



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gorg_message_sender.rb', line 27

def to_message(data,routing_key, opts={})
  json_msg={
    "event_uuid" => opts[:event_uuid]||SecureRandom.uuid,
    "event_name" => routing_key,
    "event_creation_time" => (opts[:event_creation_time]&&(opts[:event_creation_time].respond_to?(:iso8601) ? opts[:event_creation_time].iso8601 : opts[:event_creation_time].to_s)) ||DateTime.now.iso8601,
    "event_sender_id" => opts[:event_sender_id]|| @app_id,
    "data"=> data,
  }.to_json
  JSON::Validator.validate!(GorgMessageSender::JSON_SCHEMA,json_msg) unless opts[:skip_validation]
  json_msg
end