chriswarren / desk

A Ruby wrapper for the Desk.com V2 REST API
MIT License
66 stars 80 forks source link

Only a subset of results is being made available #27

Closed s2t2 closed 10 years ago

s2t2 commented 10 years ago

The following code only returns the first 20 cases.

Desk.cases.results.each do | results |
  puts results.case
end

Is this a pagination issue? Should I be using different code? I'd appreciate any instructions on how to loop through all existing cases. Thanks!

wedy commented 10 years ago

i think by default it only returns 20, try using cases(:count => 100)

s2t2 commented 10 years ago

thanks @wedy, that does return 100 results, but still not all of the available results, and it appears 100 is the largest value that :count will accept.

Desk.cases(:count => 99).results.map{|r| r.case}.count
=> 99
Desk.cases(:count => 100).results.map{|r| r.case}.count
=> 100
Desk.cases(:count => 101).results.map{|r| r.case}.count
=> 100
chriswarren commented 10 years ago

Try adding :page as an options, like Desk.cases(:count => 25, :page => 2). I think Desk is limiting the results to 100, so you'll need to go to the next page to get anything beyond that.

s2t2 commented 10 years ago

thanks @chriswarren. based on your feedback, and in case this is helpful to anyone else in the future, here's one way to loop through all cases:

(1..5000).to_a.each do |page_counter|
  page_case_results = Desk.cases(:count => 100, :page => page_counter).results
  page_case_results.each do |case_result|
    c = case_result.case
    # do some stuff
  end
end

... where 5000 is just some arbitrary large number to serve as a proxy for the number of total pages.

this definitely does the job, although i've run into a separate rate limit error twice now...

GET https://mysubdomain.desk.com/api/v1/cases.json?count=100&page=274: 400: rate_limit_exceeded

i guess i'll just figure out a way around it.

s2t2 commented 10 years ago

for googlers, here's one way to get around the rate-limiting error:

(1..5000).to_a.each do |page_counter| 
  page_case_results = Desk.cases(:count => 100, :page => page_counter).results
  if page_case_results.count > 0
    page_case_results.each do |case_result|
      c = case_result.case
      # do some stuff
    end
  else
    raise "End of File - #{page_counter - 1} pages" 
  end
end