Class: Johac::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ Response

Returns a new instance of Response.



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

def initialize(result)
  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)
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

#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:



117
118
119
# File 'lib/johac/response.rb', line 117

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

#codeObject

HTTP Status code

Returns:

  • response status code



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

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}


54
55
56
# File 'lib/johac/response.rb', line 54

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

#each(&block) ⇒ Object

Enumerate over response body object and return a new Response with a modified body

Parameters:

  • block (Block)

    to invoke

See Also:

  • {Enumerable}


63
64
65
66
67
68
69
# File 'lib/johac/response.rb', line 63

def each(&block)
  if response?
    body.each(&block)
  else
    self
  end
end

#error?Boolean

Determine if the request failed

Returns:

  • (Boolean)

    true if response failed (http or expcetion)



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

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



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/johac/response.rb', line 104

def flat_map(&block)
  if response?
    begin
      yield self
    rescue => e
      Response.new(e)
    end
  else
    self
  end
end

#map_object(&block) {|body| ... } ⇒ Object

Map body hash to another value using a block

Parameters:

  • block (Block)

    mapping function block

Yields:

Returns:

  • result of block



43
44
45
# File 'lib/johac/response.rb', line 43

def map_object(&block)
  yield body
end

#objectObject

Returns OpenStruct object of hash.

Returns:

  • OpenStruct object of hash



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

def object
  OpenStruct.new(body)
end

#on_error(&block) ⇒ Object

Invoke a block of code if the response fails, with the exception as the paramter.

Parameters:

  • block (Block)

    to invoke



75
76
77
78
79
80
# File 'lib/johac/response.rb', line 75

def on_error(&block)
  if error?
    yield exception
  end
  self
end

#on_success(&block) ⇒ Object

Invoke a block of code if the response succeeds, with the content as a parameter

Parameters:

  • block (Block)

    to invoke



86
87
88
89
90
91
# File 'lib/johac/response.rb', line 86

def on_success(&block)
  unless error?
    yield body
  end
  self
end