lib/jsom/pagination/links.rb in jsom-pagination-0.1.1 vs lib/jsom/pagination/links.rb in jsom-pagination-0.1.2

- old
+ new

@@ -1,7 +1,9 @@ # frozen_string_literal: true +require 'rack' + module JSOM module Pagination class Links DEFAULT_PAGE_SIZE = 20 @@ -28,16 +30,26 @@ private attr_reader :page, :total_pages, :url def initialize(page:, url:, total_pages:) - @url = url + @url = parse_url(url) @page = page @total_pages = total_pages generate_links end + def parse_url(url) + uri = URI.parse(URI.unescape(url)) + url_params = Rack::Utils.parse_nested_query( + uri.query + ).delete_if { |key, _value| key == 'page' } + uri.query = to_query(url_params) + uri.query = nil if uri.query.empty? + uri + end + def generate_links if page.number > 1 @first = generate_url(1) @prev = generate_url(page.number - 1) end @@ -48,26 +60,31 @@ @next = generate_url(page.number + 1) @last = generate_url(total_pages) end def generate_url(page_number) - [url, url_params(page_number)].reject(&:empty?).join('?') + separator = url.to_s.include?('?') ? '&' : '?' + [url.to_s, url_params(page_number)].reject(&:empty?).join(separator) end def url_params(page_number) url_params = {} url_params[:page] = {} if include_per_page? || include_page?(page_number) url_params[:page][:number] = page_number if include_page?(page_number) url_params[:page][:size] = page.size if include_per_page? to_query(url_params) end - def to_query(obj, namespace = nil) + def to_query(obj, namespace = '') query = obj.collect do |key, value| if value.is_a?(Hash) - to_query(value, namespace ? "#{namespace}[#{key}]" : key) + to_query(value, namespace.empty? ? key : "#{namespace}[#{key}]") else - ["#{namespace}[#{key}]", value].join('=') + if namespace.empty? + [key, value].join('=') + else + ["#{namespace}[#{key}]", value].join('=') + end end end.compact query.join('&') end