Class: Johac::Response
- Inherits:
-
Object
- Object
- Johac::Response
- Includes:
- Enumerable
- Defined in:
- lib/johac/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #and_then(&block) ⇒ Object
-
#code ⇒ Object
HTTP Status code.
-
#dig(*args) ⇒ Object
Dig for a item in the body.
-
#each(&block) ⇒ Object
Enumerate over response body object and return a new Response with a modified body.
-
#error? ⇒ Boolean
Determine if the request failed.
-
#flat_map(&block) ⇒ Object
Chain another request to the successful response.
-
#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.
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
4 5 6 |
# File 'lib/johac/response.rb', line 4 def body @body end |
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
4 5 6 |
# File 'lib/johac/response.rb', line 4 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 |
#status ⇒ Object (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
117 118 119 |
# File 'lib/johac/response.rb', line 117 def and_then(&block) flat_map(&block) end |
#code ⇒ Object
HTTP 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
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
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
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) }
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
43 44 45 |
# File 'lib/johac/response.rb', line 43 def map_object(&block) yield body end |
#object ⇒ Object
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.
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
86 87 88 89 90 91 |
# File 'lib/johac/response.rb', line 86 def on_success(&block) unless error? yield body end self end |