Class: Johac::Response
- Inherits:
-
Object
- Object
- Johac::Response
- Includes:
- Enumerable
- Defined in:
- lib/johac/response.rb
Instance Attribute Summary collapse
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
-
#body ⇒ Object
HTTP Response body as a JSON parsed object.
-
#code ⇒ Object
HTTP Status code.
-
#dig(*args) ⇒ Object
Dig for a item in the body.
-
#each(&block) ⇒ Object
Enumerate over response body opject.
-
#error? ⇒ Boolean
Determine if the request failed.
-
#initialize(result) ⇒ Response
constructor
A new instance of Response.
-
#map_object(&block) {|body| ... } ⇒ Object
Map body hash to another value using a block.
-
#object ⇒ Object
OpenStruct object of hash.
-
#on_error(&block) ⇒ Object
Invoke a block of code if the response fails, with the exception as the paramter.
-
#on_success(&block) ⇒ Object
Invoke a block of code if the response succeeds, with the content as a parameter.
-
#status ⇒ Object
HTTP Status code.
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
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
5 6 7 |
# File 'lib/johac/response.rb', line 5 def exception @exception end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
4 5 6 |
# File 'lib/johac/response.rb', line 4 def response @response end |
Instance Method Details
#body ⇒ Object
HTTP Response body as a JSON parsed object
41 42 43 |
# File 'lib/johac/response.rb', line 41 def body response_body || exception_body || {} end |
#code ⇒ Object
HTTP 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
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
75 76 77 |
# File 'lib/johac/response.rb', line 75 def each(&block) body.each(&block) end |
#error? ⇒ Boolean
Determine if the request failed
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
55 56 57 |
# File 'lib/johac/response.rb', line 55 def map_object(&block) yield body end |
#object ⇒ Object
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.
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
94 95 96 97 98 99 |
# File 'lib/johac/response.rb', line 94 def on_success(&block) unless error? yield body end self end |
#status ⇒ Object
HTTP Status code
27 28 29 |
# File 'lib/johac/response.rb', line 27 def status response_status || exception_status || 0 end |