module Hyperion::DispatchDsl

This is a DSL of sorts that gives the `request` block a nice way to dispatch the result based on status, HTTP code, etc.

Public Instance Methods

__set_escape_continuation__(k) click to toggle source
# File lib/hyperion/result_handling/dispatch_dsl.rb, line 10
def __set_escape_continuation__(k)
  @escape = k
end
when(condition, &action) click to toggle source
# File lib/hyperion/result_handling/dispatch_dsl.rb, line 14
def when(condition, &action)
  pred = as_predicate(condition)
  is_match = Util.nil_if_error { Proc.loose_call(pred, [self]) }
  if is_match
    return_value = action.call(self)
    @escape.call(return_value)  # non-local exit
  else
    nil
  end
end

Private Instance Methods

as_predicate(condition) click to toggle source
# File lib/hyperion/result_handling/dispatch_dsl.rb, line 27
def as_predicate(condition)
  if condition.is_a?(HyperionStatus)
    status_checker(condition)

  elsif condition.is_a?(ClientErrorCode)
    client_error_code_checker(condition)

  elsif condition.is_a?(Integer)
    http_code_checker(condition)

  elsif condition.is_a?(Range)
    range_checker(condition)

  elsif condition.callable?
    condition

  else
    fail "Not a valid condition: #{condition.inspect}"
  end
end
client_error_code_checker(code) click to toggle source
# File lib/hyperion/result_handling/dispatch_dsl.rb, line 52
def client_error_code_checker(code)
  proc do |r|
    r.status == HyperionStatus::CLIENT_ERROR &&
        r.body.errors.detect(:code, code)
  end
end
http_code_checker(code) click to toggle source
# File lib/hyperion/result_handling/dispatch_dsl.rb, line 59
def http_code_checker(code)
  proc { |r| r.code == code }
end
range_checker(range) click to toggle source
# File lib/hyperion/result_handling/dispatch_dsl.rb, line 63
def range_checker(range)
  proc { |r| range.include?(r.code) }
end
status_checker(status) click to toggle source
# File lib/hyperion/result_handling/dispatch_dsl.rb, line 48
def status_checker(status)
  proc { |r| r.status == status }
end