Class: OnsOnRails::Publisher
- Inherits:
-
Object
- Object
- OnsOnRails::Publisher
- Defined in:
- lib/ons_on_rails/publisher.rb
Class Method Summary collapse
-
.deliveries ⇒ Object
Keeps an array of all the messages published through the Publishers::Test backend.
Instance Method Summary collapse
-
#initialize(backend, options) ⇒ Publisher
constructor
Create a Publisher.
-
#publish(data, options = {}) ⇒ void
Publish a message.
Constructor Details
#initialize(backend, options) ⇒ Publisher
Create a Publisher.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/ons_on_rails/publisher.rb', line 12 def initialize(backend, ) required_keys = %i(access_key secret_key producer_id topic tag) required_keys.each { |required_key| .fetch(required_key) } @default_topic = .fetch(:topic) @default_tag = .fetch(:tag) @backend_klass = OnsOnRails::Publishers.const_get(backend.to_s.camelize) @backend = @backend_klass.new(.slice(:access_key, :secret_key, :producer_id)) end |
Class Method Details
.deliveries ⇒ Object
Keeps an array of all the messages published through the Publishers::Test backend. Most useful for unit and functional testing.
48 49 50 |
# File 'lib/ons_on_rails/publisher.rb', line 48 def self.deliveries @deliveries ||= [] end |
Instance Method Details
#publish(data, options = {}) ⇒ void
This method returns an undefined value.
Publish a message.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ons_on_rails/publisher.rb', line 32 def publish(data, = {}) topic = .fetch(:topic, @default_topic) tag = .fetch(:tag, @default_tag) format = .fetch(:format, 'json').to_sym body = case format when :json then data.to_json when :raw then data.to_s else raise "unsupported message format #{format}" end @backend.publish(topic, tag, body, .fetch(:key, '')) end |