Class: Megaphone::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/megaphone/client.rb,
lib/megaphone/client/event.rb,
lib/megaphone/client/errors.rb,
lib/megaphone/client/logger.rb,
lib/megaphone/client/version.rb,
lib/megaphone/client/file_logger.rb,
lib/megaphone/client/fluent_logger.rb

Defined Under Namespace

Classes: Event, FileLogger, FluentLogger, Logger, MegaphoneInvalidEventError, MegaphoneMessageDelayWarning, MegaphoneUnavailableError

Constant Summary collapse

VERSION =
"1.1.1"

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Main entry point for apps using this library. Will default to environment for host and port settings, if not passed. Note that a missing callback_handler will result in a default handler being assigned if the FluentLogger is used.



15
16
17
18
19
20
21
# File 'lib/megaphone/client.rb', line 15

def initialize(config)
  @origin = config.fetch(:origin)
  host = config.fetch(:host, ENV['MEGAPHONE_FLUENT_HOST'])
  port = config.fetch(:port, ENV['MEGAPHONE_FLUENT_PORT'])
  overflow_handler = config.fetch(:overflow_handler, nil)
  @logger = Megaphone::Client::Logger.create(host, port, overflow_handler)
end

Instance Method Details

#closeObject



35
36
37
# File 'lib/megaphone/client.rb', line 35

def close
  logger.close
end

#publish!(topic, subtopic, schema, partition_key, payload) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/megaphone/client.rb', line 23

def publish!(topic, subtopic, schema, partition_key, payload)
  event = Event.new(topic, subtopic, origin, schema, partition_key, payload)
  raise MegaphoneInvalidEventError.new(event.errors.join(', ')) unless event.valid?
  unless logger.post(topic, event.to_hash)
    if transient_error?(logger.last_error)
      raise MegaphoneMessageDelayWarning.new(logger.last_error.message, event.stream_id)
    else
      raise MegaphoneUnavailableError.new(logger.last_error.message, event.stream_id)
    end
  end
end