lib/lm_rest/api_client.rb
# Handles making multiple requests to the API if pagination is necessary.
# Pagination is transparent, and simplifies requests that result in more
# than ITEMS_SIZE_LIMIT being returned.
#
# If you need to walk through resources page-by-page manullay, use the
# request() method with the 'offset' and 'size' params
#
Handles making multiple requests to the API if pagination is necessary.
Pagination is transparent, and simplifies requests that result in more
than ITEMS_SIZE_LIMIT being returned.

If you need to walk through resources page-by-page manullay, use the
request() method with the 'offset' and 'size' params
Handles making multiple requests to the API if pagination is necessary . Pagination is transparent and simplifies requests that result in more than ITEMS_SIZE_LIMIT being returned . If you need to walk through resources page - by - page manullay use the request () method with the ' offset ' and ' size ' params
def paginate(uri, params)
      if (params[:size] == 0 || params[:size].nil? || params[:size] > ITEMS_SIZE_LIMIT)
        user_size = params[:size]
        params[:size] = ITEMS_SIZE_LIMIT
        params[:offset] ||= 0
        body = request(:get, uri.call(params), nil)
        item_collector = body['items']
        total = body['total']
        user_size ||= total
        if user_size > total
          user_size = total
        end
        pages_remaining = ((user_size - ITEMS_SIZE_LIMIT).to_f/ITEMS_SIZE_LIMIT).ceil
        pages_remaining.times do |page|
          params[:offset] += ITEMS_SIZE_LIMIT
          if page == pages_remaining - 1
            params[:size]  = user_size%ITEMS_SIZE_LIMIT
          else
            params[:size] = ITEMS_SIZE_LIMIT
          end
          body = request(:get, uri.call(params), nil)
          item_collector += body['items']
        end
        body['items'] = item_collector
        body
      else
        request(:get, uri.call(params), nil)
      end
    end
def paginate ( uri , params ) if ( params [ :size ] == 0 || params [ :size ] . nil? || params [ :size ] > ITEMS_SIZE_LIMIT ) user_size = params [ :size ] params [ :size ] = ITEMS_SIZE_LIMIT params [ :offset ] ||= 0 body = request ( :get , uri . call ( params ) , nil ) item_collector = body [ 'items' ] total = body [ 'total' ] user_size ||= total if user_size > total user_size = total end pages_remaining = ( ( user_size - ITEMS_SIZE_LIMIT ) . to_f / ITEMS_SIZE_LIMIT ) . ceil pages_remaining . times do | page | params [ :offset ] += ITEMS_SIZE_LIMIT if page == pages_remaining - 1 params [ :size ] = user_size % ITEMS_SIZE_LIMIT else params [ :size ] = ITEMS_SIZE_LIMIT end body = request ( :get , uri . call ( params ) , nil ) item_collector += body [ 'items' ] end body [ 'items' ] = item_collector body else request ( :get , uri . call ( params ) , nil ) end end
Handles making multiple requests to the API if pagination is necessary.
Handles making multiple requests to the API if pagination is necessary .
# Hooray for pagination logic! # save user-entered size in a param for use later # set our size param to the max # Set our offset to grab the first page of results  # make the initial request # pull the actual items out of the request body and into our # item_collector while we build up the items list # The API sends the total number of objects back in the first request. # We need this to determine how many more pages to pull # If user didn't pass size param, set it to total # This just means you'll get all items if not specifying a size # If the user passed a size larger than what's available, set it to # total to retrieve all items # calculate the remaining number of items (after first request) # then use that to figure out how many more times we need to call # request() to get all the items, then do it # Increment the offset by the limit to get the next page # if this is the last page, get the remainder # else, get a whole page # make a subsequent request with modified params # add these items to our item_collector # No pagination required, just request the page
uri
<not_specific>
params
<not_specific>