Sha256: 01297b902a28e48e7bed898290a66ee21b2cb38e9c2a6648b96784df1beb7fa9

Contents?: true

Size: 1.14 KB

Versions: 15

Compression:

Stored size: 1.14 KB

Contents

require "timber/events/controller_call"
require "timber/events/custom"
require "timber/events/exception"
require "timber/events/http_request"
require "timber/events/http_response"
require "timber/events/sql_query"
require "timber/events/template_render"

module Timber
  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.key?(:type) && obj.key?(:data)
        Events::Custom.new(
          type: obj[:type],
          message: obj[:message],
          data: obj[: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?(:hash) ? obj.hash : obj.to_h # ruby 1.9.3 does not have to_h
        )
      else
        nil
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
timber-1.0.13 lib/timber/events.rb
timber-1.0.12 lib/timber/events.rb
timber-1.0.11 lib/timber/events.rb
timber-1.0.10 lib/timber/events.rb
timber-1.0.9 lib/timber/events.rb
timber-1.0.8 lib/timber/events.rb
timber-1.0.7 lib/timber/events.rb
timber-1.0.6 lib/timber/events.rb
timber-1.0.5 lib/timber/events.rb
timber-1.0.4 lib/timber/events.rb
timber-1.0.3 lib/timber/events.rb
timberio-1.0.3 lib/timber/events.rb
timberio-1.0.2 lib/timber/events.rb
timberio-1.0.1 lib/timber/events.rb
timberio-1.0.0 lib/timber/events.rb