0

I'm using the API for Custom Search Engine for Ruby from Google. This is how I set the API after call Google::APIClient.new:

response = client.execute(
        :api_method => search.cse.list,
        :application_name => 'my_app',
        :application_version => '0.1',
        :parameters => {
            'q' => thekey,
            'num' => 4,
            'start' => 1,
            'key' => 'MYKEY',
            'cx' => 'MYCX'
        }
    )

The request gone, but I can't reach the next page's results. If I run client.execute(response.next_page) obtain the same data of the first API call.

response.next_page

return this:

irb(main):015:0> response.next_page => #"ordine dei geometri", "num"=>4, "start"=>1, "key"=>"MYKEY", "cx"=>"MYCX", "pageToken"=>nil}, @headers={"User-Agent"=>"google-api-ruby-client/0.7.1 Mac OS X/10.9.4\n (gzip)", "Accept-Encoding"=>"gzip", "Content-Type"=>""}, @api_method=#, @authenticated=nil, @authorization=nil, @body

You see that start parameter is set to 1 even if the first response returned

"queries"=>{"nextPage"=>['start' => 5}

Instead:

response.next_page_token

is ever nil.

I tried to search (this not working for me) but documentation is quite strange about Google API Ruby client.

Community
  • 1
  • 1
MonkTools
  • 45
  • 1
  • 4

2 Answers2

1

Here's my template for paging through Google API calls:

page_token = nil
begin
  parameters = {}
  parameters['domain'] = "mydomain.org"
  if page_token.to_s != ''
    parameters['pageToken'] = page_token
  end

  result = $client.execute(
    :api_method => api.groups.list,
    :parameters => parameters)

  if result.status == 200
    groups = result.data
    groups_array.concat(groups.groups)
    page_token = groups.next_page_token
  else
    page_token = nil
  end
end while page_token.to_s != ''

In this example I'm retrieving all the groups of a specific domain.

JRQ
  • 545
  • 4
  • 17
  • Thanks @jrq but your answer don't work with `groups_array.concat(groups.groups)` return me an error, btw my result.data don't respond_to?("concat"). Probably because i use Custom Search Api which return another kind of datas. Thanks anyway. – MonkTools Sep 15 '14 at 19:46
  • I should have been clearer. You will need to substitute your API call, your parameters, and your results in the example I provided. But the structure for dealing with paged results is good. You should be able to see that you need to re-execute for each page. – JRQ Sep 16 '14 at 12:52
  • yeah, i replaced all that stuff obviously. In my prev message i forgot to write "me" after "work with" so the phrase result quite strange, sorry. Anyway, i tried with your page dealer structure, but the problem is the same. `result.page_next_token` is ever nil and `result.page_next` return the original request with `startIndex = 1`. The strange thing is that `result.data['queries']` show a `nextPage` key which contain the right call to make for the next page. – MonkTools Sep 16 '14 at 16:02
0

Ok, some day ago i opened an issue on github repo of google-api-ruby-client and this is the final answer:

Custom search has a non-standard (vs. other Google APIs) way to represent next pages. next_page expects to find a property next_page_token in the response and merge that into the previous query. Since that value isn't present, it just re-executes the previous query.

For custom search you need to handle this your self by looking at the next_page/previous_page roles in queries.

So, in case do it without it :)

MonkTools
  • 45
  • 1
  • 4