class SerpApiSearch

Generic HTTP client for serpapi.com

Constants

BACKEND
VERSION

Attributes

params[RW]

Public Class Methods

api_key=(api_key) click to toggle source

api_key @param [String] api_key set user secret API key (copy/paste from serpapi.com/dashboard)

# File lib/search/serp_api_search.rb, line 116
def self.api_key=(api_key)
  $serp_api_key = api_key
end
new(params, engine = nil) click to toggle source

constructor

Usage


“`ruby require 'google_search' search = SerpApiSearch.new({q: “coffee”, api_key: “secure API key”, engine: “google”}) result = search.get_json “`

@param [Hash] params contains requested parameter @param [String] engine google|baidu|google|bing|ebay|yandex (optional or can be set in params)

# File lib/search/serp_api_search.rb, line 34
def initialize(params, engine = nil)
  @params = params
  @params[:engine] ||= engine
  raise 'engine must be defined in params or a second argument' if @params[:engine].nil?
end
serp_api_key=(api_key) click to toggle source

serp_api_key legacy implementation.

@param [String] api_key set user secret API key (copy/paste from serpapi.com/dashboard)

# File lib/search/serp_api_search.rb, line 110
def self.serp_api_key=(api_key)
  self.api_key = api_key
end

Public Instance Methods

api_key() click to toggle source

@return [String] api_key for this search

# File lib/search/serp_api_search.rb, line 121
def api_key
  @params[:api_key] || @params[:serp_api_key] || $serp_api_key
end
construct_url(path) click to toggle source
# File lib/search/serp_api_search.rb, line 95
def construct_url(path)
  @params[:source] = "ruby"
  if !$serp_api_key.nil?
    @params[:api_key] = $serp_api_key
  end

  @params.delete_if { |_, value| value.nil? }

  URI::HTTPS.build(host: BACKEND, path: path, query: URI.encode_www_form(@params))
end
engine() click to toggle source

@return [String] current search engine

# File lib/search/serp_api_search.rb, line 91
def engine
  @params[:engine]
end
get_account() click to toggle source

Get account information using Account API

# File lib/search/serp_api_search.rb, line 86
def get_account
  as_json(get_results('/account'))
end
get_hash() click to toggle source

get_html @return [Hash] search result Ruby hash

where keys are Symbol
# File lib/search/serp_api_search.rb, line 58
def get_hash
  JSON.parse(get_json, {symbolize_names: true})
end
get_hash_with_images() click to toggle source

alias for get_hash @deprecated

# File lib/search/serp_api_search.rb, line 64
def get_hash_with_images
  get_hash
end
get_html() click to toggle source

get_html @return [String] raw html

# File lib/search/serp_api_search.rb, line 50
def get_html
  @params[:output] = "html"
  get_results('/search')
end
get_json() click to toggle source

get_json @return [Hash] search result “json like”

where keys are String
# File lib/search/serp_api_search.rb, line 43
def get_json
  @params[:output] = "json"
  get_results('/search')
end
get_json_with_images() click to toggle source

alias for get_json @deprecated

# File lib/search/serp_api_search.rb, line 70
def get_json_with_images
  get_json
end
get_location() click to toggle source

Get location using Location API

# File lib/search/serp_api_search.rb, line 75
def get_location
  as_json(get_results('/locations.json'))
end
get_search_archive(search_id, format = 'json') click to toggle source

Retrieve search result from the Search Archive API

# File lib/search/serp_api_search.rb, line 80
def get_search_archive(search_id, format = 'json')
  raise 'format must be json or html' if format !~ /^(html|json)$/
  as_json(get_results("/searches/#{search_id}.#{format}"))
end

Private Instance Methods

as_json(data) click to toggle source
# File lib/search/serp_api_search.rb, line 127
def as_json(data)
  JSON.parse(data, symbolize_names: true)
end
check_params(keys = []) click to toggle source
# File lib/search/serp_api_search.rb, line 149
def check_params(keys = [])
  return if @params.keys == [:engine]

  raise 'keys must be a list of String or Symbol' unless keys.class == Array
  missing = []
  keys.each do |key|
    case key.class.to_s
    when 'String'
      if @params[key].nil?
        if @params[key.to_sym].nil?
          missing << key.to_s
        end
      end
    when 'Symbol'
      if @params[key].nil?
        if @params[key.to_s].nil?
          missing << key.to_s
        end
      end
    else
      raise 'keys must contains Symbol or String'
    end
  end
  if !missing.empty?
    raise "missing required keys in params.\n #{missing.join(',')}"
  end
end
get_results(path) click to toggle source
# File lib/search/serp_api_search.rb, line 131
def get_results(path)
  begin
    url = construct_url(path)
    URI(url).open(read_timeout: 600).read
  rescue OpenURI::HTTPError => e
    if error = JSON.load(e.io.read)["error"]
      puts "server returns error for url: #{url}"
      raise error
    else
      puts "fail: fetch url: #{url}"
      raise e
    end
  rescue => e
    puts "fail: fetch url: #{url}"
    raise e
  end
end