×

Custom Web Search API – Ruby client library

How to use the Custom Web Search API with Ruby

Use our Ruby client library to integrate the ContextualWeb Custom Web Search API into your Ruby application.

require "unirest" # install from: http://unirest.io/ruby.html

#Replace the following string value with your valid X-RapidAPI-Key.
Your_X_RapidAPI_Key = "XXXXXXXXXXXXXXXXXXX"

#The query parameters: (update according to your search query)
q = "Taylor%20Swift"; #the search query
pageNumber = 1 #the number of requested page
pageSize = 10 #the size of a page
searchEngineId = "XXXX" #the id of the custom search engine 

response = Unirest.get "https://custom-search.p.rapidapi.com/api/search/CustomWebSearchAPIV2?q=#{q}&pageNumber=#{pageNumber}&searchEngineId=#{searchEngineId}",
  headers:{
    "X-RapidAPI-Key" => Your_X_RapidAPI_Key
  }

#Get the numer of items returned
totalCount = response.body["totalCount"]

#Get the list of most frequent searches related to the input search query
relatedSearch = response.body["relatedSearch"]

#Go over each resulting item
response.body["value"].each do |webPage|

    #Get the web page metadata  
    url = webPage["url"]
    title = webPage["title"]
    description = webPage["description"]
    keywords = webPage["keywords"]
    provider = webPage["provider"]["name"]
    datePublished = webPage["datePublished"]

    #Get the web page image (if exists)
    imageUrl = webPage["image"]["url"]
    imageHeight = webPage["image"]["height"]
    imageWidth = webPage["image"]["width"]

    thumbnail = webPage["image"]["thumbnail"]
    thumbnailHeight = webPage["image"]["thumbnailHeight"]
    thumbnailWidth = webPage["image"]["thumbnailWidth"]

    #An example: Output the webpage url, title and published date:
    puts "Url: #{url}. Title: #{title}. Published Date: #{datePublished}"

end