Skip to content

Commit d30e260

Browse files
author
Ron Dahlgren
committed
Cleanup
1 parent 4dfc15b commit d30e260

3 files changed

Lines changed: 6 additions & 11 deletions

File tree

lib/serpapi/client.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,8 @@ def process_html_response(response, endpoint, params)
259259
end
260260

261261
def validate_json_content!(data, response, endpoint, params)
262-
# Check for API-level error inside the JSON
263262
if data.is_a?(Hash) && data.key?(:error)
264263
raise_http_error(response, data, endpoint, params, explicit_error: data[:error])
265-
# Check for HTTP-level error
266264
elsif response.status != 200
267265
raise_http_error(response, data, endpoint, params)
268266
end
@@ -275,10 +273,10 @@ def raise_http_error(response, data, endpoint, params, explicit_error: nil, deco
275273

276274
raise SerpApiError.new(
277275
"#{msg} from url: https://#{BACKEND}#{endpoint}",
278-
serpapi_error: explicit_error || (data ? data[:error] : nil),
276+
serpapi_error: explicit_error || (data.is_a?(Hash) ? data[:error] : nil),
279277
search_params: params,
280278
response_status: response.status,
281-
search_id: data&.dig(:search_metadata, :id),
279+
search_id: data.is_a?(Hash) ? data&.dig(:search_metadata, :id) : nil,
282280
decoder: decoder
283281
)
284282
end

lib/serpapi/error.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class SerpApiError < StandardError
2121
# @param search_params [Hash, nil] optional hash of the search parameters used
2222
# @param response_status [Integer, nil] optional HTTP or response status code
2323
# @param search_id [String, nil] optional id returned by the service for the search
24-
# @param decoder [String, nil] optional decoder/format used (e.g. "json")
24+
# @param decoder [Symbol, nil] optional decoder/format used (e.g. :json)
2525
def initialize(message = nil,
2626
serpapi_error: nil,
2727
search_params: nil,
@@ -76,7 +76,7 @@ def validate_optional_symbol(value, name = nil)
7676
return nil if value.nil?
7777
raise TypeError, "expected #{name || 'value'} to be a Symbol, got #{value.class}" unless value.is_a?(Symbol)
7878

79-
value.freeze
79+
value
8080
end
8181

8282
def freeze_hash(value)

spec/serpapi/client/client_spec.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
end
7272

7373
it 'get endpoint error' do
74-
expect {
75-
client.send(:get, '/search', :json, {})
74+
expect {
75+
client.send(:get, '/search', :json, {})
7676
}.to raise_error(SerpApi::SerpApiError).with_message(/HTTP request failed with status: 400.* error: Missing query `q` parameter./)
7777
end
7878

@@ -133,9 +133,6 @@
133133
allow(client).to receive(:search).and_raise(SerpApi::SerpApiError)
134134
expect { client.search(q: 'Invalid Query') }.to raise_error(SerpApi::SerpApiError)
135135
end
136-
137-
138-
139136
end
140137

141138
describe 'SerpApi client with persitency disabled' do

0 commit comments

Comments
 (0)