class Airbrake::Promise

Represents a simplified promise object (similar to promises found in JavaScript), which allows chaining callbacks that are executed when the promise is either resolved or rejected.

@see developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise @see github.com/ruby-concurrency/concurrent-ruby/blob/master/lib/concurrent/promise.rb @since v1.7.0

Public Class Methods

new() click to toggle source
# File lib/airbrake-ruby/promise.rb, line 10
def initialize
  @on_resolved = []
  @on_rejected = []
  @value = {}
  @mutex = Mutex.new
end

Public Instance Methods

reject(reason = 'rejected') click to toggle source

@example

Airbrake::Promise.new.reject('Something went wrong')

@param reason [String] @return [self]

# File lib/airbrake-ruby/promise.rb, line 80
def reject(reason = 'rejected')
  @mutex.synchronize do
    @value['error'] = reason
    @on_rejected.each { |callback| callback.call(reason) }
  end

  self
end
rejected?() click to toggle source

@return [Boolean]

# File lib/airbrake-ruby/promise.rb, line 90
def rejected?
  @value.key?('error')
end
rescue() { |value| ... } click to toggle source

Attaches a callback to be executed when the promise is rejected.

@example

Airbrake::Promise.new.rescue { |error| raise error }

@yield [error] The error message from the API @yieldparam error [String] @return [self]

# File lib/airbrake-ruby/promise.rb, line 48
def rescue(&block)
  @mutex.synchronize do
    if @value.key?('error')
      yield(@value['error'])
      return self
    end

    @on_rejected << block
  end

  self
end
resolve(reason = 'resolved') click to toggle source

@example

Airbrake::Promise.new.resolve('id' => '123')

@param reason [Object] @return [self]

# File lib/airbrake-ruby/promise.rb, line 66
def resolve(reason = 'resolved')
  @mutex.synchronize do
    @value['ok'] = reason
    @on_resolved.each { |callback| callback.call(reason) }
  end

  self
end
resolved?() click to toggle source

@return [Boolean]

# File lib/airbrake-ruby/promise.rb, line 95
def resolved?
  @value.key?('ok')
end
then() { |value| ... } click to toggle source

Attaches a callback to be executed when the promise is resolved.

@example

Airbrake::Promise.new.then { |response| puts response }
#=> {"id"=>"00054415-8201-e9c6-65d6-fc4d231d2871",
#    "url"=>"http://localhost/locate/00054415-8201-e9c6-65d6-fc4d231d2871"}

@yield [response] @yieldparam response [Hash<String,String>] Contains the `id` & `url` keys @return [self]

# File lib/airbrake-ruby/promise.rb, line 27
def then(&block)
  @mutex.synchronize do
    if @value.key?('ok')
      yield(@value['ok'])
      return self
    end

    @on_resolved << block
  end

  self
end
value() click to toggle source

@return [Hash<String,String>] either successful response containing the

+id+ key or unsuccessful response containing the +error+ key

@note This is a non-blocking call! @todo Get rid of this method and use an accessor. The resolved guard is

needed for compatibility but it shouldn't exist in the future
# File lib/airbrake-ruby/promise.rb, line 104
def value
  return @value['ok'] if resolved?

  @value
end