Class: SerpApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/serp_api_client.rb

Overview

Generic HTTP client for serpapi.com

Constant Summary collapse

VERSION =
"1.3.2"
BACKEND =
"serpapi.com"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, engine = nil) ⇒ SerpApiClient

constructor

Usage


“‘ruby require ’google_search_results’ search = SerpApiClient.new(“coffee”, api_key: “secure API key”, engine: “google”) result = search.get_json “‘

Parameters:

  • params (Hash)

    contains requested parameter

  • engine (String) (defaults to: nil)

    google|baidu|google|bing|ebay|yandex (optional or can be set in params)



34
35
36
37
38
# File 'lib/serp_api_client.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

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



19
20
21
# File 'lib/serp_api_client.rb', line 19

def params
  @params
end

Class Method Details

.api_key=(api_key) ⇒ Object

api_key

Parameters:



116
117
118
# File 'lib/serp_api_client.rb', line 116

def self.api_key=(api_key)
  $serp_api_key = api_key
end

.serp_api_key=(api_key) ⇒ Object

serp_api_key legacy implementation.

Parameters:



110
111
112
# File 'lib/serp_api_client.rb', line 110

def self.serp_api_key=(api_key)
  self.api_key = api_key
end

Instance Method Details

#api_keyString

Returns api_key for this search.

Returns:

  • (String)

    api_key for this search



121
122
123
# File 'lib/serp_api_client.rb', line 121

def api_key
  @params[:api_key] || @params[:serp_api_key] || $serp_api_key
end

#construct_url(path) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/serp_api_client.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

#engineString

Returns current search engine.

Returns:

  • (String)

    current search engine



91
92
93
# File 'lib/serp_api_client.rb', line 91

def engine
  @params[:engine]
end

#get_accountObject

Get account information using Account API



86
87
88
# File 'lib/serp_api_client.rb', line 86

def 
  as_json(get_results('/account'))
end

#get_hashHash

get_html

Returns:

  • (Hash)

    search result Ruby hash where keys are Symbol



58
59
60
# File 'lib/serp_api_client.rb', line 58

def get_hash
  JSON.parse(get_json, {symbolize_names: true})
end

#get_hash_with_imagesObject

Deprecated.

alias for get_hash



64
65
66
# File 'lib/serp_api_client.rb', line 64

def get_hash_with_images
  get_hash
end

#get_htmlString

get_html

Returns:

  • (String)

    raw html



50
51
52
53
# File 'lib/serp_api_client.rb', line 50

def get_html
  @params[:output] = "html"
  get_results('/search')
end

#get_jsonHash

get_json

Returns:

  • (Hash)

    search result “json like” where keys are String



43
44
45
46
# File 'lib/serp_api_client.rb', line 43

def get_json
  @params[:output] = "json"
  get_results('/search')
end

#get_json_with_imagesObject

Deprecated.

alias for get_json



70
71
72
# File 'lib/serp_api_client.rb', line 70

def get_json_with_images
  get_json
end

#get_locationObject

Get location using Location API



75
76
77
# File 'lib/serp_api_client.rb', line 75

def get_location
  as_json(get_results('/locations.json'))
end

#get_search_archive(search_id, format = 'json') ⇒ Object

Retrieve search result from the Search Archive API



80
81
82
83
# File 'lib/serp_api_client.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