lib/restful_model_collection.rb in inbox-2.0.0 vs lib/restful_model_collection.rb in inbox-2.0.1

- old
+ new

@@ -13,21 +13,20 @@ end def each return enum_for(:each) unless block_given? - @filters[:offset] = 0 unless @filters.key?(:offset) - @filters[:limit] = 100 unless @filters.key?(:limit) - + offset = 0 + chunk_size = 100 finished = false while (!finished) do - results = get_model_collection() + results = get_model_collection(offset, chunk_size) results.each { |item| yield item } - @filters[:offset] += results.length - finished = results.length < @filters[:limit] + offset += results.length + finished = results.length < chunk_size end end def count RestClient.get(url, params: @filters.merge(view: 'count')) { |response,request,result| @@ -63,21 +62,16 @@ def range(offset = 0, limit = 100) accumulated = [] finished = false chunk_size = 100 - while (!finished && accumulated.length < limit) do - @filters[:offset] = offset + accumulated.length + if limit < chunk_size + chunk_size = limit + end - # if the total items we want, minus how many we already have, is fewer than we plan to grab... - remaining = limit - accumulated.length - if remaining < chunk_size - chunk_size = remaining - end - @filters[:limit] = chunk_size - - results = get_model_collection() + while (!finished && accumulated.length < limit) do + results = get_model_collection(offset + accumulated.length, chunk_size) accumulated = accumulated.concat(results) # we're done if we have more than 'limit' items, or if we asked for 50 and got less than 50... finished = accumulated.length >= limit || results.length == 0 || (results.length % chunk_size != 0) end @@ -140,11 +134,13 @@ end } model end - def get_model_collection + def get_model_collection(offset = 0, limit = 100) filters = @filters.clone + filters[:offset] = offset + filters[:limit] = limit models = [] RestClient.get(url, :params => filters){ |response,request,result| items = Inbox.interpret_response(result, response, {:expected_class => Array}) models = inflate_collection(items)