Class: Eco::API::Common::Loaders::Workflow::Mailer

Inherits:
Eco::API::Common::Loaders::Workflow show all
Defined in:
lib/eco/api/common/loaders/config/workflow/mailer.rb

Direct Known Subclasses

Eco::API::Custom::Mailer

Constant Summary collapse

ORG =
'your organization'.freeze
TRACE_COUNT =
3
EP_EMAIL =
/@.*?ecoportal\.co(!:m|\.nz)$/
ALT_TO =
%w[
  oscar@ecoportal.co.nz
  travis@ecoportal.co.nz
].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Eco::API::Common::Loaders::Workflow

workflow

Class Attribute Details

.errorObject

Returns the value of attribute error.



41
42
43
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 41

def error
  @error
end

Class Method Details

.active_enviro_descObject



88
89
90
91
92
93
94
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 88

def active_enviro_desc
  space_desc = config.active_enviro_space.to_s.strip
  space_desc = ''                          if space_desc == 'default'
  space_desc = " - space: '#{space_desc}'" unless space_desc.empty?

  "#{active_enviro}#{space_desc}"
end

.alternative_emails(io) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 65

def alternative_emails(io)
  email_to   = io.session.config.mailer.to
  email_to   = [email_to].flatten.compact

  alt_emails = email_to.grep(EP_EMAIL)
  alt_emails = ALT_TO if alt_emails.empty?
  alt_emails
end

.base_subjectObject

@todo: add whether it is a dry-run and if we are on certain enviro.

  • The target env could probably be added as a :general message.


84
85
86
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 84

def base_subject
  "#{org_name} (#{active_enviro_desc}) at #{Time.now.iso8601}"
end

.env_based_mailer!(io) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 57

def env_based_mailer!(io)
  return if live?(io) || !io.session.config.dry_run?

  # redirect mailing to eP Staff when not in `live` env.
  # or when in `dry-run`
  io.session.config.mailer.to = alternative_emails(io)
end

.error?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 96

def error?
  !!error
end

.error_messageObject



123
124
125
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 123

def error_message
  error&.patch_full_message(trace_count: 3)
end

.errors_n_warnings(io) ⇒ Object



112
113
114
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 112

def errors_n_warnings(io)
  [error_message, log_err_n_warn(io)].join("\n")
end

.general_messages(io) ⇒ Object



105
106
107
108
109
110
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 105

def general_messages(io)
  gen_msg = io.logger.cache.logs(level: %i[general])
  return if gen_msg.empty?

  "GENERAL MESSAGES:\n#{gen_msg.join}\n"
end

.host(io) ⇒ Object



78
79
80
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 78

def host(io)
  io.session.api.client.host
end

.live?(io) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 74

def live?(io)
  host(io).downcase.start_with?('live.')
end

.log_err_n_warn(io) ⇒ Object



116
117
118
119
120
121
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 116

def log_err_n_warn(io)
  warn_errors = io.logger.cache.logs(level: %i[error warn])
  return if warn_errors.empty?

  "ALL WARNINGS & ERRORS:\n#{warn_errors.join}\n"
end

.maybe_error_pages_or_tree_updates?(io) ⇒ Boolean

Note:

at some stage, tree cases should have their own case type

Returns:

  • (Boolean)


101
102
103
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 101

def maybe_error_pages_or_tree_updates?(io)
  some_other_case?(io) && error?
end

.notify?(io) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
55
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 47

def notify?(io)
  return false if     io.options.dig(:workflow, :no_email)
  return false unless io.session.mailer?
  return true  if     io.options.dig(:workflow, :notify)
  return false if     io.session.config.dry_run?
  return false unless io.session.config.run_mode_remote?

  true
end

.org_nameObject



43
44
45
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 43

def org_name
  self::ORG
end

.trace_countObject



127
128
129
# File 'lib/eco/api/common/loaders/config/workflow/mailer.rb', line 127

def trace_count
  self::TRACE_COUNT
end