Sha256: 7abdad5aa9deec18873d58c7afbd470b8529d2a5a411fd2ff6f8bca59eec121e

Contents?: true

Size: 1.17 KB

Versions: 31

Compression:

Stored size: 1.17 KB

Contents

require 'forwardable'
require 'routemaster/responses/hateoas_response'

module Routemaster
  module Responses
    # Yields all resources listed in a collection endpoint in a non-greedy,
    # non-recursive manner.
    #
    # Each yielded resource is a future; synchronous requests are performed for
    # each page.
    #
    # NB: the first named collection in the _links section of the payload will
    # be enumerated. Any other named collections will simply be ignored.
    class HateoasEnumerableResponse < HateoasResponse
      include Enumerable

      def each(&block)
        each_page do |items|
          items.each(&block)
        end
      end

      def each_page
        current_page = self
        loop do
          yield _page_items(current_page)
          break unless current_page.has?(:next)
          current_page = current_page.next.index
        end
      end

      private

      def _resource_name
        _links.find { |k,v|
          !%w[curies self].include?(k) && v.kind_of?(Array)
        }.first
      end

      def _page_items(page)
        page.body._links.fetch(_resource_name).map do |link|
          @client.fget(link.href)
        end
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
routemaster-drain-3.7.1 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.7.0 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.6.8 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.6.7 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.6.6 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.6.5 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.6.4 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.6.3 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.6.2 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.6.1 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.6.0 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.5.1 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.5.0 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.4.0 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.3.0 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.2.0 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.1.0 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.0.3 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.0.2 lib/routemaster/responses/hateoas_enumerable_response.rb
routemaster-drain-3.0.1 lib/routemaster/responses/hateoas_enumerable_response.rb