Class: SerpApiSearch
- Inherits:
-
Object
- Object
- SerpApiSearch
- Defined in:
- lib/search/serp_api_search.rb
Overview
Generic HTTP client for serpapi.com
Direct Known Subclasses
AppleStoreSearch, BaiduSearch, BingSearch, DuckduckgoSearch, EbaySearch, GoogleSearch, HomedepotSearch, NaverSearch, WalmartSearch, YahooSearch, YandexSearch, YoutubeSearch
Constant Summary collapse
- VERSION =
"2.2.0"- 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) ⇒ SerpApiSearch
constructor
constructor.
Constructor Details
#initialize(params, engine = nil) ⇒ SerpApiSearch
constructor
Usage
“‘ruby require ’google_search’ search = SerpApiSearch.new(“coffee”, api_key: “secure API key”, engine: “google”) result = search.get_json “‘
39 40 41 42 43 |
# File 'lib/search/serp_api_search.rb', line 39 def initialize(params, engine = nil) @params = params @params[:engine] ||= engine raise SerpApiException.new('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.
24 25 26 |
# File 'lib/search/serp_api_search.rb', line 24 def params @params end |
Class Method Details
.api_key=(api_key) ⇒ Object
api_key
121 122 123 |
# File 'lib/search/serp_api_search.rb', line 121 def self.api_key=(api_key) $serp_api_key = api_key end |
.serp_api_key=(api_key) ⇒ Object
serp_api_key legacy implementation.
115 116 117 |
# File 'lib/search/serp_api_search.rb', line 115 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.
126 127 128 |
# File 'lib/search/serp_api_search.rb', line 126 def api_key @params[:api_key] || @params[:serp_api_key] || $serp_api_key end |
#construct_url(path) ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/search/serp_api_search.rb', line 100 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.
96 97 98 |
# File 'lib/search/serp_api_search.rb', line 96 def engine @params[:engine] end |
#get_account ⇒ Object
Get account information using Account API
91 92 93 |
# File 'lib/search/serp_api_search.rb', line 91 def get_account as_json(get_results('/account')) end |
#get_hash ⇒ Hash
get_html
63 64 65 |
# File 'lib/search/serp_api_search.rb', line 63 def get_hash JSON.parse(get_json, {symbolize_names: true}) end |
#get_hash_with_images ⇒ Object
alias for get_hash
69 70 71 |
# File 'lib/search/serp_api_search.rb', line 69 def get_hash_with_images get_hash end |
#get_html ⇒ String
get_html
55 56 57 58 |
# File 'lib/search/serp_api_search.rb', line 55 def get_html @params[:output] = "html" get_results('/search') end |
#get_json ⇒ Hash
get_json
48 49 50 51 |
# File 'lib/search/serp_api_search.rb', line 48 def get_json @params[:output] = "json" get_results('/search') end |
#get_json_with_images ⇒ Object
alias for get_json
75 76 77 |
# File 'lib/search/serp_api_search.rb', line 75 def get_json_with_images get_json end |
#get_location ⇒ Object
Get location using Location API
80 81 82 |
# File 'lib/search/serp_api_search.rb', line 80 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
85 86 87 88 |
# File 'lib/search/serp_api_search.rb', line 85 def get_search_archive(search_id, format = 'json') raise SerpApiException.new('format must be json or html') if format !~ /^(html|json)$/ as_json(get_results("/searches/#{search_id}.#{format}")) end |