Sha256: c01cb1ac0c135624f4ec54498e184b18653dc714bd1127559012aa24432c61e7

Contents?: true

Size: 1.07 KB

Versions: 6

Compression:

Stored size: 1.07 KB

Contents

# encoding: utf-8

require_relative 'constants'

module Github

  # A module that adds http get request to response pagination
  module PagedRequest
    include Github::Constants

    FIRST_PAGE = 1 # Default request page if none provided

    PER_PAGE   = 30 # Default number of items as specified by API

    NOT_FOUND  = -1 # Either page or per_page parameter not present

    # Check if current api instance has default per_page param set,
    # otherwise use global default.
    #
    def default_page_size
      current_api.per_page ? current_api.per_page : PER_PAGE
    end

    def default_page
      current_api.page ? current_api.page : FIRST_PAGE
    end

    # Perform http get request with pagination parameters
    #
    def page_request(path, params={})
      if params[PARAM_PER_PAGE] == NOT_FOUND
        params[PARAM_PER_PAGE] = default_page_size
      end
      if params[PARAM_PAGE] && params[PARAM_PAGE] == NOT_FOUND
        params[PARAM_PAGE] = default_page
      end

      current_api.get_request(path, ParamsHash.new(params))
    end

  end # PagedRequest
end # Github

Version data entries

6 entries across 6 versions & 3 rubygems

Version Path
github_api2-1.0.1 lib/github_api2/paged_request.rb
github_api2-1.0.0 lib/github_api2/paged_request.rb
github_api-0.19.0 lib/github_api/paged_request.rb
lingfennan-github_api-0.18.2 lib/github_api/paged_request.rb
github_api-0.18.2 lib/github_api/paged_request.rb
github_api-0.18.1 lib/github_api/paged_request.rb