Class: Carioca::Services::Finisher

Inherits:
Object
  • Object
show all
Defined in:
lib/carioca/services/finisher.rb

Overview

Exiter namespace

Constant Summary collapse

DEFAULT_FINISHERS_SPECS =
{
  # global
  not_root: { code: 40, key: 'finisher.messages.not_root' },
  options_incompatibility: { code: 410, key: 'finisher.messages.options_incompatibility' },
  missing_service_dependency: { code: 430, key: 'finisher.messages.missing_service_dependency' },
  config_required: { code: 420, key: 'finisher.messages.config_required' },
  setup_error: { code: 520, key: 'finisher.messages.setup_error' },
  setup_success: { code: 0, key: 'finisher.messages.setup_success' },
  sanitycheck_error: { code: 510, key: 'finisher.messages.sanitycheck_error' },
  sanitycheck_success: { code: 0, key: 'finisher.messages.sanitycheck_success' },
  configuration_error: { code: 501, key: 'finisher.messages.configuration_error' },
  success_exit: { code: 0, key: 'finisher.messages.success_exit' },
  quiet_exit: { code: 0 },
  error_exit: { code: 50, key: 'finisher.messages.error_exit' },
  # events
  interrupt: { code: 330, key: 'finisher.messages.interrupt' },
  # request & API
  not_found: { code: 404, key: 'finisher.messages.not_found' },
  already_exist: { code: 408, key: 'finisher.messages.already_exist' },
  # daemon & API
  status_ok: { code: 200, key: 'finisher.messages.status_ok' },
  created: { code: 201, key: 'finisher.messages.created' },
  accepted: { code: 202, key: 'finisher.messages.accepted' },
  bad_request: { code: 400, key: 'finisher.messages.bad_request' },
  status_ko: { code: 500, key: 'finisher.messages.status_ko' },
  no_content: { code: 204, key: 'finisher.messages.no_content' },
  not_implemented: { code: 210, key: 'finisher.messages.not_implemented' },
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeFinisher

Returns a new instance of Finisher.



56
57
58
59
60
61
62
63
# File 'lib/carioca/services/finisher.rb', line 56

def initialize
  registry = Carioca::Registry.get
  @output = registry.get_service name: :output
  @i18n = registry.get_service name: :i18n
  @configuration = registry.get_service name: :configuration
  @exit_map = {}.merge DEFAULT_FINISHERS_SPECS
  @exit_map.merge! @configuration.settings.exit_cases if @configuration.settings.exit_cases
end

Instance Method Details

#do_exit!(exit_case: :quiet_exit, more: nil) ⇒ Object

exiter

Parameters:

  • [Symbol] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/carioca/services/finisher.rb', line 75

def do_exit!(exit_case: :quiet_exit, more: nil)
  mess = ''
  mess = @i18n.t(@exit_map[exit_case][:key]) if @exit_map[exit_case].include? :key
  mess << ' : ' unless mess.empty? || !more
  mess << more.to_s if more
  if (@exit_map[exit_case][:code]).zero?
    @output.success mess unless mess.empty?
    exit 0
  else
    @output.fatal mess unless mess.empty?
    exit @exit_map[exit_case][:code]
  end
end

#do_return(return_case: :status_ok, more: nil) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/carioca/services/finisher.rb', line 89

def do_return(return_case: :status_ok, more: nil)
  data = @exit_map[return_case].clone
  if data.include? :key
    data[:message] = @i18n.t(data[:key])
    data.delete :key
  end
  data[:more] = more if more
  data
end

#secure_alternative(message:, data:, return_case: :accepted) ⇒ Object

Raises:



126
127
128
# File 'lib/carioca/services/finisher.rb', line 126

def secure_alternative(message:, data:, return_case: :accepted)
  raise AlternativeCatcher.new(message, return_case:, data:)
end

#secure_api_return(data: nil, return_case: nil, structured: false, json: true, status: true) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/carioca/services/finisher.rb', line 103

def secure_api_return(data: nil, return_case: nil, structured: false, json: true, status: true)
  result = {}
  begin
    data = yield if block_given?
    result = structured ? do_return(return_case:).merge({ data: }) : data
  rescue StandardError => e
    result = if e.respond_to?(:error_case)
               do_return(return_case: e.error_case, more: e.message)
             elsif e.respond_to?(:return_case)
               structured ? do_return(return_case: e.return_case, more: e.message).merge({ data: e.data }) : e.data
             else
               do_return(return_case: :status_ko, more: "#{e.class} : #{e.message}")
             end
  end
  if status && structured && json
    { status: result[:code], data: JSON.pretty_generate(JSON.parse(result.to_json)) }
  elsif json
    JSON.pretty_generate(JSON.parse(result.to_json)) if json
  else
    result
  end
end

#secure_execute!(exit_case: :success_exit) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/carioca/services/finisher.rb', line 130

def secure_execute!(exit_case: :success_exit)
  begin
    more = yield
    key = exit_case
  rescue StandardError => e
    key = e.respond_to?(:error_case) ? e.error_case : :error_exit
    more = e.respond_to?(:error_case) ? e.message : "#{e.class} : #{e.message}"
  end
  do_exit! exit_case: key, more:
end

#secure_raise(message: 'unknown error', error_case: :status_ko) ⇒ Object

Raises:



99
100
101
# File 'lib/carioca/services/finisher.rb', line 99

def secure_raise(message: 'unknown error', error_case: :status_ko)
  raise SpecificError.new(message, error_case:)
end

#terminate(return_case: nil, exit_case: nil, more: nil) ⇒ Object



65
66
67
68
69
70
# File 'lib/carioca/services/finisher.rb', line 65

def terminate(return_case: nil, exit_case: nil, more: nil)
  raise 'Case must be a return or an exit' if return_case && exit_case

  do_exit!(exit_case:, more:) if exit_case
  do_return(return_case:, more:) if return_case
end