Class: Artsy::EventService::Publisher

Inherits:
Object
  • Object
show all
Includes:
RabbitMQConnection
Defined in:
lib/artsy-eventservice/artsy/event_service/publisher.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RabbitMQConnection

#bunny_params, #config, #connect_to_rabbit, #create_conn, #no_tls_params, #rabbitmq_url, #tls_params

Class Method Details

.configObject



26
27
28
# File 'lib/artsy-eventservice/artsy/event_service/publisher.rb', line 26

def self.config
  Artsy::EventService.config
end

.publish(topic:, event:) ⇒ Object



7
8
9
# File 'lib/artsy-eventservice/artsy/event_service/publisher.rb', line 7

def self.publish(topic:, event:)
  new.post_event(topic: topic, event: event)
end

Instance Method Details

#post_event(topic:, event:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/artsy-eventservice/artsy/event_service/publisher.rb', line 11

def post_event(topic:, event:)
  raise 'Event missing topic or verb.' if event.verb.to_s.empty? || topic.to_s.empty?
  connect_to_rabbit do |conn|
    channel = conn.create_channel
    exchange = channel.topic(topic, durable: true)
    exchange.publish(
      event.json,
      routing_key: event.verb,
      persistent: true,
      content_type: 'application/json',
      app_id: config.app_name
    )
  end
end