Sha256: 608ff6452495c9f61f19a09257f0d4d3503e48ebc52a476a6877fba54e46ba2e

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

module Pina
  module Utils
    module Pagination
      def next_page
        paginate(_meta['pagination']['next'])
      end

      def first_page
        resource.all
      end

      def previous_page
        paginate(_meta['pagination']['previous'])
      end

      def last_page
        paginate(_meta['pagination']['last'])
      end

      def paginate(page_url)
        params = extract_params(page_url)
        return unless params&.fetch('page')

        resource.all(params)
      end

      def collect_items
        resource = self

        [].tap do |collection|
          loop do
            collection.push(*resource.items)
            resource = resource.next_page

            break unless resource
          end
        end
      end

      private

      def resource
        Object.const_get("Pina::#{self.class.to_s.split('::').last}")
      end

      def extract_params(url)
        return unless url

        index = url.index('?')
        return unless index

        params_to_hash(url[index + 1..-1])
      end

      def params_to_hash(params)
        {}.tap do |params_hash|
          params.split('&').each do |element|
            param = element.split('=')
            params_hash[param.first] = param.last
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pina-0.15.0 lib/pina/utils/pagination.rb