lib/recurly/pager.rb in recurly-3.28.0 vs lib/recurly/pager.rb in recurly-4.0.0

- old
+ new

@@ -4,29 +4,29 @@ attr_reader :data, :next def initialize(client:, path:, options: {}) @client = client @path = path - @options = map_array_params(options) + @options = options rewind! end # Performs a request with the pager `limit` set to 1 and only returns the first # result in the response. def first # Modify the @next url to set the :limit to 1 original_next = @next @next = @path - fetch_next!(@options.merge(limit: 1)) + fetch_next!(@options.merge(params: @options.fetch(:params, {}).merge({ limit: 1 }))) # Restore the @next url to the original @next = original_next @data.first end # Makes a HEAD request to the API to determine how many total records exist. def count - resource = @client.send(:head, self.next, **@options) + resource = @client.send(:head, self.next, @options) resource.get_response.total_records end # Enumerates each "page" from the server. # This method yields a given block with the array of items @@ -101,11 +101,11 @@ def page_enumerator Enumerator.new do |yielder| loop do # Pass in @options when requesting the first page (@data.empty?) - next_options = @data.empty? ? @options : {} + next_options = @data.empty? ? @options : @options.merge(params: {}) fetch_next!(next_options) yielder << data unless has_more? rewind! break @@ -114,11 +114,11 @@ end end def fetch_next!(options) path = extract_path(self.next) - page = @client.send(:get, path, **options) + page = @client.send(:get, path, options) @data = page.data.map { |d| JSONParser.from_json(d) } @has_more = page.has_more @next = page.next end @@ -129,18 +129,8 @@ # Returns just the path and parameters so we can safely reuse the connection def extract_path(uri_or_path) uri = URI(uri_or_path) uri.kind_of?(URI::HTTP) ? uri.request_uri : uri_or_path - end - - # Converts array parameters to CSV strings to maintain consistency with - # how the server expects the request to be formatted while providing the - # developer with an array type to maintain developer happiness! - def map_array_params(params) - @options = params.map do |key, param| - new_param = param.is_a?(Array) ? param.join(",") : param - [key, new_param] - end.to_h end end end