11# Client implementation for SerpApi.com
22#
33module SerpApi
4-
54 # Client for SerpApi.com
65 #
76 class Client
@@ -10,7 +9,7 @@ class Client
109 # Backend service URL
1110 BACKEND = 'serpapi.com' . freeze
1211
13- # HTTP timeout requests
12+ # HTTP timeout requests
1413 attr_reader :timeout ,
1514 # Query parameters
1615 :params
@@ -31,7 +30,7 @@ class Client
3130 # key can be either a symbol or a string.
3231 #
3332 # @param [Hash] params default for the search
34- def initialize ( params = { } , adapter = :net_http )
33+ def initialize ( params = { } , _adapter = :net_http )
3534 # set default read timeout
3635 @timeout = params [ :timeout ] || params [ 'timeout' ] || 120
3736 @timeout . freeze
@@ -128,30 +127,24 @@ def query(params)
128127 # @param [Boolean] symbolize_names if true, convert JSON keys to symbols
129128 # @return decoded payload as JSON / Hash or String
130129 def get ( endpoint , decoder = :json , params = { } , symbolize_names = true )
131- begin
132- payload = @socket . get ( endpoint ) do |req |
133- req . params = query ( params )
134- req . options . timeout = timeout
135- end
136- # read http response
137- data = payload . body
138- # decode payload using JSON native parser
139- if decoder == :json
140- data = JSON . parse ( data , symbolize_names : symbolize_names )
141- if data . class == Hash && data . key? ( 'error' )
142- raise SerpApiException , "get failed with error: #{ data [ 'error' ] } from url: #{ endpoint } , params: #{ params } , decoder: #{ decoder } , http status: #{ payload . status } "
143- end
144- if payload . status != 200
145- raise SerpApiException , "get failed with response status: #{ payload . status } reponse: #{ data } on get url: #{ endpoint } , params: #{ params } , decoder: #{ decoder } "
146- end
130+ payload = @socket . get ( endpoint ) do |req |
131+ req . params = query ( params )
132+ req . options . timeout = timeout
133+ end
134+ # read http response
135+ data = payload . body
136+ # decode payload using JSON native parser
137+ if decoder == :json
138+ data = JSON . parse ( data , symbolize_names : symbolize_names )
139+ if data . instance_of? ( Hash ) && data . key? ( 'error' )
140+ raise SerpApiException , "get failed with error: #{ data [ 'error' ] } from url: #{ endpoint } , params: #{ params } , decoder: #{ decoder } , http status: #{ payload . status } "
147141 end
148- # return raw HTML
149- return data
150- rescue Faraday ::Error => err
151- raise SerpApiException , "fail: get url: #{ endpoint } caused by #{ err . class } : #{ err . message } (params: #{ params } , decoder: #{ decoder } )"
142+ raise SerpApiException , "get failed with response status: #{ payload . status } reponse: #{ data } on get url: #{ endpoint } , params: #{ params } , decoder: #{ decoder } " if payload . status != 200
152143 end
144+ # return raw HTML
145+ data
146+ rescue Faraday ::Error => e
147+ raise SerpApiException , "fail: get url: #{ endpoint } caused by #{ e . class } : #{ e . message } (params: #{ params } , decoder: #{ decoder } )"
153148 end
154-
155149 end
156-
157150end
0 commit comments