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[
  [email protected]
  [email protected]
].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



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

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.


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

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)


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

def error?
  !!error
end

.error_messageObject



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

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

.errors_n_warnings(io) ⇒ Object



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

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

.general_messages(io) ⇒ Object



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

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

Note:

this initiates a connection to the API (undesired)



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

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



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

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)


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

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



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

def trace_count
  self::TRACE_COUNT
end