Sha256: 54badc08c2d58578bdc527799bcee6aaa7d25e0ff1798670ad5ffd662ad80694

Contents?: true

Size: 1.31 KB

Versions: 15

Compression:

Stored size: 1.31 KB

Contents

require "timber/events/controller_call"
require "timber/events/custom"
require "timber/events/exception"
require "timber/events/http_server_request"
require "timber/events/http_server_response"
require "timber/events/sql_query"
require "timber/events/template_render"

module Timber
  # Namespace for all Timber supported events.
  module Events
    # Protocol for casting objects into a `Timber::Event`.
    #
    # @example Casting a hash
    #   Timber::Events.build({type: :custom_event, message: "My log message", data: {my: "data"}})
    def self.build(obj)
      if obj.is_a?(::Timber::Event)
        obj
      elsif obj.respond_to?(:to_timber_event)
        obj.to_timber_event
      elsif obj.is_a?(Hash) && obj.key?(:message) && obj.length == 2
        event = obj.select { |k,v| k != :message }
        type = event.keys.first
        data = event.values.first

        Events::Custom.new(
          type: type,
          message: obj[:message],
          data: data
        )
      elsif obj.is_a?(Struct) && obj.respond_to?(:message) && obj.respond_to?(:type)
        Events::Custom.new(
          type: obj.type,
          message: obj.message,
          data: obj.respond_to?(:to_h) ? obj.to_h : Timber::Util::Struct.to_hash(obj) # ruby 1.9.3 does not have to_h :(
        )
      else
        nil
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
timber-1.1.14 lib/timber/events.rb
timber-1.1.13 lib/timber/events.rb
timber-1.1.12 lib/timber/events.rb
timber-1.1.11 lib/timber/events.rb
timber-1.1.10 lib/timber/events.rb
timber-1.1.9 lib/timber/events.rb
timber-1.1.8 lib/timber/events.rb
timber-1.1.7 lib/timber/events.rb
timber-1.1.6 lib/timber/events.rb
timber-1.1.5 lib/timber/events.rb
timber-1.1.4 lib/timber/events.rb
timber-1.1.3 lib/timber/events.rb
timber-1.1.2 lib/timber/events.rb
timber-1.1.1 lib/timber/events.rb
timber-1.1.0 lib/timber/events.rb