Class: Johac::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/johac/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result, chain = []) ⇒ Response

Returns a new instance of Response.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/johac/response.rb', line 6

def initialize(result, chain=[])
  if result.kind_of?(Faraday::Response)
    @response = result
  else
    @exception = result
  end

  @body = result.body if result.respond_to?(:body)
  @status = result.status if result.respond_to?(:status)
  @chain = chain
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



4
5
6
# File 'lib/johac/response.rb', line 4

def body
  @body
end

#chainObject (readonly)

Returns the value of attribute chain.



4
5
6
# File 'lib/johac/response.rb', line 4

def chain
  @chain
end

#exceptionObject (readonly)

Returns the value of attribute exception.



4
5
6
# File 'lib/johac/response.rb', line 4

def exception
  @exception
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/johac/response.rb', line 4

def response
  @response
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/johac/response.rb', line 4

def status
  @status
end

Instance Method Details

#and_then(&block) ⇒ Object

See Also:



98
99
100
# File 'lib/johac/response.rb', line 98

def and_then(&block)
  flat_map(&block)
end

#codeObject

HTTP Status code

Returns:

  • response status code



28
29
30
# File 'lib/johac/response.rb', line 28

def code
  status
end

#dig(*args) ⇒ Object

Dig for a item in the body

Parameters:

  • args (Varargs)

    path of value

Returns:

  • value of key path

See Also:

  • Johac::Response.{Hash{Hash.dig}


68
69
70
# File 'lib/johac/response.rb', line 68

def dig(*args)
  body.kind_of?(Hash) ? body.dig(*args) : nil
end

#error?Boolean

Determine if the request failed

Returns:

  • (Boolean)

    true if response failed (http or expcetion)



21
22
23
# File 'lib/johac/response.rb', line 21

def error?
  status.nil? || status >= 400
end

#flat_map(&block) ⇒ Object

Chain another request to the successful response. The expected output of the block is another Johac::Response object.

This enables request chaining, where an error in the chain will prevent further processing and return an error response

response = client.request1(param)

.flat_map { |r| client.request2(r.object.value) }
.flat_map { |r| client.request3(r.object.value) }

Parameters:

  • block (Block)

    to invoke



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/johac/response.rb', line 83

def flat_map(&block)
  if response?
    begin
      result = yield self, chain
      result.set_chain(chain + [self])
      result
    rescue => e
      Response.new(e, chain + [self])
    end
  else
    self
  end
end

#map(&block) ⇒ Object

Map response body if successful

Parameters:

  • block (Block)

    mapping function block

Returns:

  • result of block



42
43
44
45
46
47
48
# File 'lib/johac/response.rb', line 42

def map(&block)
  unless error?
    yield body
  else
    nil
  end
end

#map_error(&block) ⇒ Object

Map the exception if present

Parameters:

  • block (Block)

    to invoke



53
54
55
56
57
58
59
# File 'lib/johac/response.rb', line 53

def map_error(&block)
  if error?
    yield exception
  else
    nil
  end
end

#objectObject

Returns OpenStruct object of hash, or empty struct if error.

Returns:

  • OpenStruct object of hash, or empty struct if error



33
34
35
# File 'lib/johac/response.rb', line 33

def object
  error? ? OpenStruct.new : OpenStruct.new(body)
end