Class: Timber::Contexts::Release

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

Overview

Note:

To automatically set this context, see Release.from_env.

The release context tracks application releases / versions / deploys.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Release

Returns a new instance of Release.



38
39
40
41
42
# File 'lib/timber/contexts/release.rb', line 38

def initialize(attributes)
  @commit_hash = attributes[:commit_hash]
  @created_at = attributes[:created_at]
  @version = attributes[:version]
end

Instance Attribute Details

#commit_hashObject (readonly)

Returns the value of attribute commit_hash.



36
37
38
# File 'lib/timber/contexts/release.rb', line 36

def commit_hash
  @commit_hash
end

#created_atObject (readonly)

Returns the value of attribute created_at.



36
37
38
# File 'lib/timber/contexts/release.rb', line 36

def created_at
  @created_at
end

#versionObject (readonly)

Returns the value of attribute version.



36
37
38
# File 'lib/timber/contexts/release.rb', line 36

def version
  @version
end

Class Method Details

.from_envObject

Builds a release context based on environment variables. Simply add the ‘RELEASE_COMMIT`, `RELEASE_CREATED_AT`, or the `RELEASE_VERSION` env vars to get this context automatially. All are optional, but at least one must be present.

If you’re on Heroku, simply enable dyno metadata to get this automatically: devcenter.heroku.com/articles/dyno-metadata



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/timber/contexts/release.rb', line 21

def from_env
  commit_hash = ENV['RELEASE_COMMIT'] || ENV['HEROKU_SLUG_COMMIT']
  created_at = ENV['RELEASE_CREATED_AT'] || ENV['HEROKU_RELEASE_CREATED_AT']
  version = ENV['RELEASE_VERSION'] || ENV['HEROKU_RELEASE_VERSION']

  if commit_hash || created_at || version
    Timber::Config.instance.debug { "Release env vars detected, adding to context" }
    new(commit_hash: commit_hash, created_at: created_at, version: version)
  else
    Timber::Config.instance.debug { "Release env vars _not_ detected" }
    nil
  end
end

Instance Method Details

#as_json(_options = {}) ⇒ Object

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



45
46
47
# File 'lib/timber/contexts/release.rb', line 45

def as_json(_options = {})
  {commit_hash: commit_hash, created_at: created_at, version: version}
end