Class: Timber::Contexts::Organization

Inherits:
Timber::Context
  • Object
show all
Defined in:
lib/timber/contexts/organization.rb

Overview

The organization context tracks the organization of the currently authenticated user.

You will want to add this context at the time you determine the organization a user belongs to, typically in the authentication flow.

Example:

organization_context = Timber::Contexts::Organization.new(id: "abc1234", name: "Timber Inc")
logger.with_context(organization_context) do
  # Logging will automatically include this context
  logger.info("This is a log message")
end

Constant Summary collapse

ID_MAX_BYTES =
256.freeze
NAME_MAX_BYTES =
256.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Organization

Returns a new instance of Organization.



29
30
31
32
33
# File 'lib/timber/contexts/organization.rb', line 29

def initialize(attributes)
  normalizer = Util::AttributeNormalizer.new(attributes)
  @id = normalizer.fetch(:id, :string, :limit => ID_MAX_BYTES)
  @name = normalizer.fetch(:name, :string, :limit => NAME_MAX_BYTES)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



27
28
29
# File 'lib/timber/contexts/organization.rb', line 27

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



27
28
29
# File 'lib/timber/contexts/organization.rb', line 27

def name
  @name
end

Instance Method Details

#as_json(_options = {}) ⇒ Object



43
44
45
# File 'lib/timber/contexts/organization.rb', line 43

def as_json(_options = {})
  to_hash
end

#to_hashObject

Builds a hash representation containing simple objects, suitable for serialization (JSON).



36
37
38
39
40
41
# File 'lib/timber/contexts/organization.rb', line 36

def to_hash
  @to_hash ||= Util::NonNilHashBuilder.build do |h|
    h.add(:id, id)
    h.add(:name, name)
  end
end