Sha256: f43ac2edd1f57228450ebcd11f7e28bf3b953af51bfb714b99c499c74dd9cf92

Contents?: true

Size: 1.6 KB

Versions: 20

Compression:

Stored size: 1.6 KB

Contents

require 'will_paginate/collection'

module Px::Service::Client
  ##
  # This class implements the methods necessary to be compatible with WillPaginate and Enumerable
  class ListResponse
    include WillPaginate::CollectionMethods
    include Enumerable

    attr_reader :response, :per_page

    DEFAULT_PER_PAGE = 20

    def initialize(page_size, response, results_key, options = {})
      @response = response
      @results_key = results_key
      @options = options
      @per_page = page_size || DEFAULT_PER_PAGE
    end

    ##
    # Get the current page
    def current_page
      response["current_page"]
    end

    def offset
      (current_page - 1) * per_page
    end

    def total_entries
      response["total_items"]
    end
    alias_method :total, :total_entries

    def total_pages
      response["total_pages"]
    end

    def results
      response[@results_key]
    end

    def raw_results
      response[@results_key]
    end

    ##
    # Support Enumerable
    def each(&block)
      results.each(&block)
    end

    ##
    # Allow comparisons with arrays e.g. in Rspec to succeed
    def ==(other)
      if other.class == self.class
        other.results == self.results
      elsif other.class <= Array
        other == self.results
      else
        false
      end
    end
    alias_method :eql?, :==

    def empty?
      results.empty?
    end

    def method_missing(method_name, *arguments, &block)
      results.send(method_name, *arguments, &block)
    end

    def respond_to_missing?(method_name, include_private = false)
      results.respond_to?(method_name, include_private)
    end

  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
px-service-client-2.0.12 lib/px/service/client/list_response.rb
px-service-client-2.0.11 lib/px/service/client/list_response.rb
px-service-client-2.0.10 lib/px/service/client/list_response.rb
px-service-client-2.0.8 lib/px/service/client/list_response.rb
px-service-client-2.0.7 lib/px/service/client/list_response.rb
px-service-client-2.0.6 lib/px/service/client/list_response.rb
px-service-client-1.2.3 lib/px/service/client/list_response.rb
px-service-client-2.0.5 lib/px/service/client/list_response.rb
px-service-client-2.0.4 lib/px/service/client/list_response.rb
px-service-client-2.0.3 lib/px/service/client/list_response.rb
px-service-client-2.0.2 lib/px/service/client/list_response.rb
px-service-client-2.0.1 lib/px/service/client/list_response.rb
px-service-client-2.0.0 lib/px/service/client/list_response.rb
px-service-client-1.2.2 lib/px/service/client/list_response.rb
px-service-client-1.2.1 lib/px/service/client/list_response.rb
px-service-client-1.2.0 lib/px/service/client/list_response.rb
px-service-client-1.1.0 lib/px/service/client/list_response.rb
px-service-client-1.0.6 lib/px/service/client/list_response.rb
px-service-client-1.0.4 lib/px/service/client/list_response.rb
px-service-client-1.0.1 lib/px/service/client/list_response.rb