Class: Logplex::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/logplex/publisher.rb

Constant Summary collapse

PUBLISH_ERRORS =
[Excon::Errors::InternalServerError,
Excon::Errors::Unauthorized,
Timeout::Error].freeze

Instance Method Summary collapse

Constructor Details

#initialize(logplex_url = nil) ⇒ Publisher

Returns a new instance of Publisher.



12
13
14
15
# File 'lib/logplex/publisher.rb', line 12

def initialize(logplex_url = nil)
  @logplex_url = logplex_url || Logplex.configuration.logplex_url
  @token = URI(@logplex_url).password || Logplex.configuration.app_name
end

Instance Method Details

#publish(messages, opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/logplex/publisher.rb', line 17

def publish(messages, opts={})
  message_list = messages.dup
  unless messages.is_a? Array
    message_list = [message_list]
  end
  message_list.map! { |m| Message.new(m, { app_name: @token }.merge(opts)) }
  message_list.each(&:validate)
  if message_list.inject(true) { |accum, m| m.valid? }
    begin
      Timeout.timeout(Logplex.configuration.publish_timeout) do
        api_post(message_list.map(&:syslog_frame).join(''), message_list.length)
        true
      end
    rescue *PUBLISH_ERRORS
      false
    end
  end
end