Class: Ecoportal::API::Common::WrappedResponse
- Inherits:
-
Object
- Object
- Ecoportal::API::Common::WrappedResponse
- Includes:
- Enumerable
- Defined in:
- lib/ecoportal/api/common/wrapped_response.rb
Instance Attribute Summary collapse
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
- #body ⇒ Object
- #each ⇒ Object
-
#initialize(response, klass) ⇒ WrappedResponse
constructor
A new instance of WrappedResponse.
- #print_pretty ⇒ Object
- #status ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(response, klass) ⇒ WrappedResponse
Returns a new instance of WrappedResponse.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 9 def initialize(response, klass) @response = response @klass = klass if @response.success? @result = if @response.body.is_a?(Array) @response.body.map do |doc| @klass.new(doc) end else @klass.new(@response.body) end end end |
Instance Attribute Details
#response ⇒ Object (readonly)
Returns the value of attribute response.
7 8 9 |
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 7 def response @response end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
7 8 9 |
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 7 def result @result end |
Instance Method Details
#body ⇒ Object
25 26 27 |
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 25 def body response.body.to_s end |
#each ⇒ Object
29 30 31 32 33 |
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 29 def each [*result].each do |doc| yield doc end end |
#print_pretty ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 43 def print_pretty if success? each(&:print_pretty) else puts "Request failed." end end |
#status ⇒ Object
35 36 37 |
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 35 def status response.status.code end |
#success? ⇒ Boolean
39 40 41 |
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 39 def success? response.success? end |