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

- old
+ new

@@ -13,20 +13,21 @@ end def each return enum_for(:each) unless block_given? - offset = 0 - chunk_size = 100 + @filters[:offset] = 0 unless @filters.key?(:offset) + @filters[:limit] = 100 unless @filters.key?(:limit) + finished = false while (!finished) do - results = get_model_collection(offset, chunk_size) + results = get_model_collection() results.each { |item| yield item } - offset += results.length - finished = results.length < chunk_size + @filters[:offset] += results.length + finished = results.length < @filters[:limit] end end def count RestClient.get(url, params: @filters.merge(view: 'count')) { |response,request,result| @@ -62,16 +63,21 @@ def range(offset = 0, limit = 100) accumulated = [] finished = false chunk_size = 100 - if limit < chunk_size - chunk_size = limit - end - while (!finished && accumulated.length < limit) do - results = get_model_collection(offset + accumulated.length, chunk_size) + @filters[:offset] = offset + accumulated.length + + # 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() 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 @@ -134,13 +140,11 @@ end } model end - def get_model_collection(offset = 0, limit = 100) + def get_model_collection 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)