From c34874f12471ec965edde35dd81340ffa7b1601b Mon Sep 17 00:00:00 2001 From: Xavier MALPARTY Date: Tue, 22 Jun 2021 17:55:01 +0700 Subject: [PATCH] [#7] Update client service to match rebase code --- app/services/google/client_service.rb | 29 ++++++++++++++------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/app/services/google/client_service.rb b/app/services/google/client_service.rb index 7d149946..f04693bc 100644 --- a/app/services/google/client_service.rb +++ b/app/services/google/client_service.rb @@ -8,20 +8,20 @@ class ClientService BASE_SEARCH_URL = 'https://www.google.com/search' def initialize(keyword:, lang: 'en') - escaped_keyword = CGI.escape(keyword) - @uri = URI("#{BASE_SEARCH_URL}?q=#{escaped_keyword}&hl=#{lang}&gl=#{lang}") + @escaped_keyword = CGI.escape(keyword) + @uri = URI("#{BASE_SEARCH_URL}?q=#{@escaped_keyword}&hl=#{lang}&gl=#{lang}") end def call - begin - @result = HTTParty.get(@uri, { headers: { 'User-Agent' => USER_AGENT } }) - rescue HTTParty::Error, Timeout::Error, SocketError => e - Rails.logger.error "Error: Query Google with keyword #{@keyword} throw an error: #{e}".colorize(:red) - @result = nil - else - validate_result - end - @result + result = HTTParty.get(@uri, { headers: { 'User-Agent' => USER_AGENT } }) + + return false unless valid_result? result + + result + rescue HTTParty::Error, Timeout::Error, SocketError => e + Rails.logger.error "Error: Query Google with '#{@escaped_keyword}' thrown an error: #{e}".colorize(:red) + + false end private @@ -31,9 +31,10 @@ def call def valid_result?(result) return true if result&.response&.code == '200' - Rails.logger.warn "Warning: Query Google with keyword #{@keyword} return status code #{@result.response.code}" - .colorize(:yellow) - @result = nil + Rails.logger.warn "Warning: Query Google with '#{@escaped_keyword}' return status code #{result.response.code}" + .colorize(:yellow) + + false end end end