Module: App::Helpers::Async

Defined in:
app/helpers/async.rb

Instance Method Summary collapse

Instance Method Details

#defer(future) ⇒ Object



6
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
38
39
40
41
42
43
# File 'app/helpers/async.rb', line 6

def defer(future)
  future.on_success do |result|
    env["async.callback"].call([
      200,
      {
        'Content-Type' => 'application/json'
      },
      [json_dump(result)]
    ])
  end

  future.on_failure do |error|
    status = case error
    when Cassandra::Errors::NoHostsAvailable
      503
    when Cassandra::Errors::AuthenticationError
      401
    when Cassandra::Errors::UnauthorizedError
      403
    when Cassandra::Errors::ExecutionError
      504
    when Cassandra::Error
      400
    when
      500
    end

    env["async.callback"].call([
      status,
      {
        'Content-Type' => 'application/json'
      },
      [json_dump(error)]
    ])
  end

  throw :async
end