Class: SerpApiClient
- Inherits:
-
Object
- Object
- SerpApiClient
- Defined in:
- lib/serp_api_client.rb
Overview
Generic HTTP client for serpapi.com
Direct Known Subclasses
BaiduSearchResults, BingSearchResults, EbaySearchResults, GoogleSearchResults, YahooSearchResults, YandexSearchResults
Constant Summary collapse
- VERSION =
"1.3.2"
- BACKEND =
"serpapi.com"
Instance Attribute Summary collapse
-
#params ⇒ Object
Returns the value of attribute params.
Class Method Summary collapse
-
.api_key=(api_key) ⇒ Object
api_key.
-
.serp_api_key=(api_key) ⇒ Object
serp_api_key legacy implementation.
Instance Method Summary collapse
-
#api_key ⇒ String
Api_key for this search.
- #construct_url(path) ⇒ Object
-
#engine ⇒ String
Current search engine.
-
#get_account ⇒ Object
Get account information using Account API.
-
#get_hash ⇒ Hash
get_html.
- #get_hash_with_images ⇒ Object deprecated Deprecated.
-
#get_html ⇒ String
get_html.
-
#get_json ⇒ Hash
get_json.
- #get_json_with_images ⇒ Object deprecated Deprecated.
-
#get_location ⇒ Object
Get location using Location API.
-
#get_search_archive(search_id, format = 'json') ⇒ Object
Retrieve search result from the Search Archive API.
-
#initialize(params, engine = nil) ⇒ SerpApiClient
constructor
constructor.
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 “‘
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
#params ⇒ Object
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
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.
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_key ⇒ String
Returns 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 |
#engine ⇒ String
Returns current search engine.
91 92 93 |
# File 'lib/serp_api_client.rb', line 91 def engine @params[:engine] end |
#get_account ⇒ Object
Get account information using Account API
86 87 88 |
# File 'lib/serp_api_client.rb', line 86 def get_account as_json(get_results('/account')) end |
#get_hash ⇒ Hash
get_html
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_images ⇒ Object
alias for get_hash
64 65 66 |
# File 'lib/serp_api_client.rb', line 64 def get_hash_with_images get_hash end |
#get_html ⇒ String
get_html
50 51 52 53 |
# File 'lib/serp_api_client.rb', line 50 def get_html @params[:output] = "html" get_results('/search') end |
#get_json ⇒ Hash
get_json
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_images ⇒ Object
alias for get_json
70 71 72 |
# File 'lib/serp_api_client.rb', line 70 def get_json_with_images get_json end |
#get_location ⇒ Object
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 |