@@ -4,7 +4,6 @@ module SerpApi
44 # Client for SerpApi.com
55 #
66 class Client
7-
87 # Backend service URL
98 BACKEND = 'serpapi.com' . freeze
109
@@ -43,7 +42,7 @@ def initialize(params = {})
4342 @persistent . freeze
4443
4544 # delete this client only configuration keys
46- %i( timeout persistent ) . each do |option |
45+ %i[ timeout persistent ] . each do |option |
4746 params . delete ( option ) if params . key? ( option )
4847 end
4948
@@ -56,9 +55,9 @@ def initialize(params = {})
5655 @params . freeze
5756
5857 # create connection socket
59- if persistent?
60- @socket = HTTP . persistent ( "https:// #{ BACKEND } " )
61- end
58+ return unless persistent?
59+
60+ @socket = HTTP . persistent ( "https:// #{ BACKEND } " )
6261 end
6362
6463 # perform a search using SerpApi.com
@@ -119,7 +118,7 @@ def api_key
119118 @params [ :api_key ]
120119 end
121120
122- # close open connection
121+ # close open connection
123122 def close
124123 @socket . close if @socket
125124 end
@@ -148,13 +147,13 @@ def persistent?
148147 # @param [Boolean] symbolize_names if true, convert JSON keys to symbols
149148 # @return decoded response as JSON / Hash or String
150149 def get ( endpoint , decoder = :json , params = { } , symbolize_names = true )
151- # execute get via open socket
152- if persistent?
153- response = @socket . get ( endpoint , params : query ( params ) )
154- else
155- response = HTTP . timeout ( timeout ) . get ( "https://#{ BACKEND } #{ endpoint } " , params : query ( params ) )
156- end
157-
150+ # execute get via open socket
151+ response = if persistent?
152+ @socket . get ( endpoint , params : query ( params ) )
153+ else
154+ HTTP . timeout ( timeout ) . get ( "https://#{ BACKEND } #{ endpoint } " , params : query ( params ) )
155+ end
156+
158157 # decode response using JSON native parser
159158 case decoder
160159 when :json
@@ -163,15 +162,15 @@ def get(endpoint, decoder = :json, params = {}, symbolize_names = true)
163162 if data . instance_of? ( Hash ) && data . key? ( :error )
164163 raise SerpApiError , "HTTP request failed with error: #{ data [ :error ] } from url: https://#{ BACKEND } #{ endpoint } , params: #{ params } , decoder: #{ decoder } , response status: #{ response . status } "
165164 elsif response . status != 200
166- raise SerpApiError , "HTTP request failed with response status: #{ response . status } reponse: #{ data } on get url: https://#{ BACKEND } #{ endpoint } , params: #{ params } , decoder: #{ decoder } "
165+ raise SerpApiError , "HTTP request failed with response status: #{ response . status } reponse: #{ data } on get url: https://#{ BACKEND } #{ endpoint } , params: #{ params } , decoder: #{ decoder } "
167166 end
168167
169168 # discard response body
170169 response . flush if persistent?
171170
172- return data
171+ data
173172 else
174- return response . body
173+ response . body
175174 end
176175 end
177176 end
0 commit comments