Sha256: 1da6e913803d39a10281c70c87e12c951cee6c8c6461f7d9133a14e02394a7c9

Contents?: true

Size: 919 Bytes

Versions: 12

Compression:

Stored size: 919 Bytes

Contents

module Ecoportal
  module API
    module Common
      class WrappedResponse
        include Enumerable
        attr_reader :response, :result
        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
        def each
          [*@result].each do |doc|
            yield doc
          end
        end
        def success?
          @response.success?
        end
        def print
          if success?
            @result&.map(&:print)
          else
            puts "Request failed."
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ecoportal-api-0.2.0 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.1.12 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.1.11 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.1.10 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.1.9 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.1.8 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.1.7 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.1.6 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.1.5 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.1.4 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.1.3 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.1.2 lib/ecoportal/api/common/wrapped_response.rb