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.



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

def error
  @error
end

Class Method Details

.active_enviro_descObject



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

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



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

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.


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

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

.env_based_mailer!(io) ⇒ Object



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

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)


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

def error?
  !!error
end

.error_messageObject



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

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

.errors_n_warnings(io) ⇒ Object



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

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

.general_messages(io) ⇒ Object



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

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



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

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

.live?(io) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.log_err_n_warn(io) ⇒ Object



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

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)


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

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

.notify?(io) ⇒ Boolean

Returns:

  • (Boolean)


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

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 if     io.session.config.run_mode_remote?

  true
end

.org_nameObject



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

def org_name
  self::ORG
end

.trace_countObject



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

def trace_count
  self::TRACE_COUNT
end