Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the Google Custom Search API with Ruby google-api-client

As part of a people search project I'm currently participating in, I need to write a ruby script that can send search queries to the Google Custom Search API and store the search results for processing. I found the Ruby google-api-client gem (http://code.google.com/p/google-api-ruby-client/) and installed it, but, despite having thoroughly read the documentation, I am at a loss as to how to execute a Custom Search API call. This is my first attempt at using Google APIs and I'm finding the process a bit overwhelming, is there anyone out there with any experience that could provide some sample code for me to study? Thanks

like image 986
Richard Stokes Avatar asked Oct 24 '22 09:10

Richard Stokes


1 Answers

While I haven't tested this, something like this should work:

require 'google/api_client'
# Creates an instance of the client.
client = Google::APIClient.new
# Authorization setup goes here.
# Fetch the discovery document and obtain a reference to the API we care about.
search = client.discovered_api('customsearch')
# Make an API call using a reference to a discovered method.
response = client.execute(
  search.cse.list, 'q' => 'your query'
)
status, headers, body = response

Note that I've omitted all the setup code for authentication, which you can find in the docs for the Ruby client.

like image 119
Bob Aman Avatar answered Oct 27 '22 09:10

Bob Aman