Sha256: f2da5159fd24bc09a281c8691c444295d08ee0f8d9aaf994f909bdb250fc31e6

Contents?: true

Size: 1.05 KB

Versions: 6

Compression:

Stored size: 1.05 KB

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 body
          response.body.to_s
        end

        def each
          [*result].each do |doc|
            yield doc
          end
        end

        def status
          response.status.code
        end

        def success?
          response.success?
        end

        def print_pretty
          if success?
            each(&:print_pretty)
          else
            puts "Request failed."
          end
        end

      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ecoportal-api-0.9.7 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.9.6 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.9.5 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.9.4 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.9.3 lib/ecoportal/api/common/wrapped_response.rb
ecoportal-api-0.9.2 lib/ecoportal/api/common/wrapped_response.rb