module Ecertic::Utils

Constants

COLOR_CODES

Public Class Methods

encode_files(files) click to toggle source
# File lib/ecertic/utils.rb, line 11
def self.encode_files(files)
  files.map do |file|
    {
      pdf_filename: File.basename(file.path),
      pdf_content: Base64.strict_encode64(file.read),
    }
  end
end
log_debug(message, data = {}) click to toggle source
# File lib/ecertic/utils.rb, line 32
def self.log_debug(message, data = {})
  return unless should_log_for_level(Ecertic::LEVEL_DEBUG)

  log_internal(message, data, color: :orange, level: Ecertic::LEVEL_DEBUG, logger: Ecertic.logger, out: $stdout)
end
log_error(message, data = {}) click to toggle source
# File lib/ecertic/utils.rb, line 20
def self.log_error(message, data = {})
  return unless should_log_for_level(Ecertic::LEVEL_ERROR)

  log_internal(message, data, color: :red, level: Ecertic::LEVEL_ERROR, logger: Ecertic.logger, out: $stderr)
end
log_info(message, data = {}) click to toggle source
# File lib/ecertic/utils.rb, line 26
def self.log_info(message, data = {})
  return unless should_log_for_level(Ecertic::LEVEL_INFO)

  log_internal(message, data, color: :cyan, level: Ecertic::LEVEL_INFO, logger: Ecertic.logger, out: $stdout)
end
should_log_for_level(level) click to toggle source
# File lib/ecertic/utils.rb, line 38
def self.should_log_for_level(level)
  Ecertic.logger || Ecertic.log_level && Ecertic.log_level <= level
end
validate_mandatory_attributes(attributes, required) click to toggle source
# File lib/ecertic/utils.rb, line 5
def self.validate_mandatory_attributes(attributes, required)
  required.each do |name|
    attributes&.key?(name) || raise(ArgumentError, ":#{name} is required")
  end
end

Private Class Methods

colorize(val, color, isatty) click to toggle source
# File lib/ecertic/utils.rb, line 51
def self.colorize(val, color, isatty)
  return val unless isatty

  mode = 0
  foreground = 30 + COLOR_CODES.fetch(color)
  background = 40 + COLOR_CODES.fetch(:default)

  "\033[#{mode};#{foreground};#{background}m#{val}\033[0m"
end
level_name(level) click to toggle source
# File lib/ecertic/utils.rb, line 62
def self.level_name(level)
  case level
  when LEVEL_DEBUG then "debug"
  when LEVEL_ERROR then "error"
  when LEVEL_INFO  then "info"
  else level
  end
end
log_internal(message, data = {}, color: nil, level: nil, logger: nil, out: nil) click to toggle source
# File lib/ecertic/utils.rb, line 72
def self.log_internal(message, data = {}, color: nil, level: nil, logger: nil, out: nil)
  data_str = data.reject { |_k, v| v.nil? }.map do |(k, v)|
    format("%<key>s=%<value>s", key: colorize(k, color, logger.nil? && !out.nil? && out.isatty), value: v)
  end.join(" ")

  if !logger.nil?
    logger.log(level, format("message=%<message>s %<data_str>s", message: message, data_str: data_str))
  elsif out.isatty
    out.puts format("%<level>s %<message>s %<data_str>s",
                    level: colorize(level_name(level).ljust(5).upcase, color, out.isatty),
                    message: message,
                    data_str: data_str)
  else
    out.puts format("message=%<message>s level=%<level>s %<data_str>s",
                    message: message,
                    level: level_name(level),
                    data_str: data_str)
  end
end