Sha256: c3d14a6e28214d1f1836898e625722aa768bc8348d67c4c28f3590b2817cf09c

Contents?: true

Size: 1.52 KB

Versions: 6

Compression:

Stored size: 1.52 KB

Contents

require 'api_pack/support/helper'

module ApiPack
  class PaginationMetaGenerator
    include Support::Helper

    def initialize(request:, total_pages:)
      @url = "#{request.base_url}#{request.path}"
      @page = request.params[:page].to_i
      @per_page = request.params[:per_page].to_i
      @total_pages = total_pages
    end

    def call
      {
        links: links,
        meta: {
          current_page: current_page,
          total_pages: @total_pages
        }
      }
    end

    private

    attr_accessor :url, :hash

    attr_reader :per_page, :page, :total_pages

    def links
      links = {
        self: generate_url,
        first: generate_url(number_page: 1),
        last: generate_url(number_page: total_pages)
      }

      links[:next] = generate_url(number_page: next_number(page)) if current_page != total_pages
      links[:prev] = generate_url(number_page: page - 1) if page > 1

      links
    end

    def generate_url(number_page: 0)
      return url if number_page.zero?

      [url, url_params(number_page)].join("?")
    end

    def url_params(number_page)
      url_params = {}
      url_params[:per_page] = include_per_page
      url_params[:page] = number_page

      to_query_api(url_params)
    end

    def include_per_page
      return ApiPack.default_per_page if per_page.zero?

      per_page
    end

    def next_number(number_page)
      return number_page + 2 if number_page.zero?

      number_page + 1
    end

    def current_page
      return 1 if page.zero?

      page
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
api_pack-1.3.1 lib/api_pack/pagination_meta_generator.rb
api_pack-1.2.1 lib/api_pack/pagination_meta_generator.rb
api_pack-1.1.4 lib/api_pack/pagination_meta_generator.rb
api_pack-1.1.3 lib/api_pack/pagination_meta_generator.rb
api_pack-1.1.2 lib/api_pack/pagination_meta_generator.rb
api_pack-1.1.1 lib/api_pack/pagination_meta_generator.rb