Sha256: bccef1b42132a96c8dabadff653d706d11b43401ef43611f0f37e20c2b2939b0

Contents?: true

Size: 1.14 KB

Versions: 7

Compression:

Stored size: 1.14 KB

Contents

require 'papertrail/search_result'

# SearchQuery manages pagination.
# Once initialized, call `next_results_page` for a page of results
# And call it again to get the next page, and again and again
module Papertrail
  class SearchQuery
    def self.api_url
      '/api/v1/events/search.json'
    end

    def self.initial_search_limit
      100
    end

    def self.subsequent_search_limit
      1000
    end

    def initialize(connection, query = nil, options = {})
      @connection = connection
      @query      = query
      @options    = options
    end

    attr_accessor :max_id, :subsequent_request

    def next_results_page
      params = @options.dup
      params[:q] = @query if @query
      params[:min_id] = @max_id if @max_id
      params[:limit] ||= default_request_limit

      response = @connection.get(self.class.api_url, params)
      @max_id = response.body['max_id']
      Papertrail::SearchResult.new(response.body)
    end

    def default_request_limit
      if subsequent_request
        self.class.subsequent_search_limit
      else
        @subsequent_request = true
        self.class.initial_search_limit
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
papertrail-0.11.2 lib/papertrail/search_query.rb
papertrail-0.11.1 lib/papertrail/search_query.rb
papertrail-0.11.0 lib/papertrail/search_query.rb
papertrail-0.10.4 lib/papertrail/search_query.rb
papertrail-0.10.3 lib/papertrail/search_query.rb
papertrail-0.10.1 lib/papertrail/search_query.rb
papertrail-0.10.0 lib/papertrail/search_query.rb