Module: TelphinApi::Result

Defined in:
lib/telphin_api/result.rb

Overview

A module that handles method result processing.

It implements method blocks support, result typecasting and raises TelphinApi::Error in case of an error response.

Class Method Summary collapse

Class Method Details

.process(response, type, block) ⇒ Array, Hashie::Mash

The main method result processing.

Parameters:

  • response (Hashie::Mash)

    The server response in mash format.

  • type (Symbol)

    The expected result type (:boolean or :anything).

  • block (Proc)

    A block passed to the API method.

Returns:

  • (Array, Hashie::Mash)

    The processed result.

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/telphin_api/result.rb', line 13

def process(response, type, block)
  result = extract_result(response)
  
  if result.respond_to?(:each)
    # enumerable result receives :map with a block when called with a block
    # or is returned untouched otherwise
    block.nil? ? result : result.map(&block)
  else
    # non-enumerable result is typecasted
    # (and yielded if block_given?)
    result = typecast(result, type)
    block.nil? ? result : block.call(result)
  end
end