×

Ruby client library

How to use the Image Search API with Ruby

Use Ruby client library to integrate the Image 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
autoCorrect = true #autoCorrectspelling
safeSearch = false #filter results for adult content

response = Unirest.get "https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/ImageSearchAPI?q=#{q}&pageNumber=#{pageNumber}&;pageSize=#{pageSize}&;autoCorrect=#{autoCorrect}&;safeSearch=#{safeSearch}",
  headers:{
    "X-RapidAPI-Key": Your_X_RapidAPI_Key
  }

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

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

    #Get the image
    imageUrl = image["url"]
    imageHeight = image["height"]
    imageWidth = image["width"]

    #Get the image thumbail
    thumbnail = image["thumbnail"]
    thumbnailHeight = image["thumbnailHeight"]
    thumbnailWidth = image["thumbnailWidth"]

    #An example: Output the webpage url, title and published date:
    puts "imageUrl: #{imageUrl}. imageHeight: #{imageHeight}. imageWidth: #{imageWidth}"

end