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.



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

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

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



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

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

Instance Method Details

#bodyObject

HTTP Response body as a JSON parsed object

Returns:

  • Hash/Array of the JSON parsed body, or empty hash



41
42
43
# File 'lib/johac/response.rb', line 41

def body
  response_body || exception_body || {}
end

#codeObject

HTTP Status code

Returns:

  • response status code



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

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}
  • Johac::Response.{Array{Array.dig}


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

def dig(*args)
  body.dig(*args)
end

#each(&block) ⇒ Object

Enumerate over response body opject

Parameters:

  • block (Block)

    to invoke

See Also:

  • {Enumerable}


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

def each(&block)
  body.each(&block)
end

#error?Boolean

Determine if the request failed

Returns:

  • (Boolean)

    true if response failed (http or expcetion)



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

def error?
  exception != nil || status >= 400
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



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

def map_object(&block)
  yield body
end

#objectObject

Returns OpenStruct object of hash.

Returns:

  • OpenStruct object of hash



46
47
48
# File 'lib/johac/response.rb', line 46

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



83
84
85
86
87
88
# File 'lib/johac/response.rb', line 83

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



94
95
96
97
98
99
# File 'lib/johac/response.rb', line 94

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

#statusObject

HTTP Status code

Returns:

  • response status code



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

def status
  response_status || exception_status || 0
end