Sha256: e81846299626c732fb343ac3c150feeaa079a1c03f7c815ff4331aae0bb27079

Contents?: true

Size: 969 Bytes

Versions: 7

Compression:

Stored size: 969 Bytes

Contents

module Kinescope
  class PaginationResource
    include Enumerable

    attr_reader :action, :args, :resource
    attr_accessor :page, :per_page, :total

    def initialize(action, resource, *args)
      @action = action
      @resource = resource
      @args = args

      @collection = []
      @page = nil
      @per_page = nil
      @total = nil
    end

    def [](index)
      @collection[index]
    end

    def each(&block)
      handle_response if total.nil?
      return to_enum(:each) unless block_given?

      @collection.each { |element| yield(element) }

      self
    end

    private

    def handle_response
      invoker = ResourceKit::ActionInvoker.new(action, resource, *args)
      @collection = invoker.handle_response

      meta = Kinescope::MetaMapping.extract_single(invoker.response.body, :read)
      self.page = meta.pagination.page
      self.per_page = meta.pagination.per_page
      self.total = meta.pagination.total
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
kinescope-rb-0.5.1 lib/kinescope/pagination_resource.rb
kinescope-rb-0.5.0 lib/kinescope/pagination_resource.rb
kinescope-rb-0.4.0 lib/kinescope/pagination_resource.rb
kinescope-rb-0.3.0 lib/kinescope/pagination_resource.rb
kinescope-rb-0.2.1 lib/kinescope/pagination_resource.rb
kinescope-rb-0.2.0 lib/kinescope/pagination_resource.rb
kinescope-rb-0.1.0 lib/kinescope/pagination_resource.rb