Module: DupSendBlocker

Defined in:
lib/dup_send_blocker.rb,
lib/dup_send_blocker/engine.rb,
lib/dup_send_blocker/version.rb,
app/models/dup_send_blocker/send_log.rb,
app/helpers/dup_send_blocker/application_helper.rb,
app/controllers/dup_send_blocker/application_controller.rb

Defined Under Namespace

Modules: ApplicationHelper Classes: ApplicationController, BlockError, Engine, SendLog

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.perform!(labels = [], send_error_is: :invalid) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dup_send_blocker.rb', line 7

def self.perform!(labels = [], send_error_is: :invalid)
  unless [:valid, :invalid].include?(send_error_is)
    raise ArgumentError.new
  end
  unless block_given?
    raise ArgumentError.new('block is required.')
  end

  send_log = nil
  begin
    send_log = DupSendBlocker::SendLog.write_labels!(labels)
  rescue ::DupSendBlocker::SendLog::DupLabelError => e
    raise BlockError.new(e.message)
  end

  res = nil
  begin
    # 送信処理はblock内で行う
    res = yield
  rescue => e
    if send_error_is == :invalid
      send_log.delete
    else # :valid
      send_log.update_columns({error_message: e.message})
    end

    raise e
  end

  {result: res, send_log: send_log}
end