-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#6] Handle error while query google - not colorized
- Loading branch information
Showing
2 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module GoogleService | ||
class ClientService | ||
require 'httparty' | ||
|
||
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) '\ | ||
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36' | ||
|
||
def initialize(keyword, lang = 'en') | ||
escaped_keyword = CGI.escape(keyword) | ||
@uri = URI("https://auibawsasxasxechjksdc.fr/search?q=#{escaped_keyword}&hl=#{lang}&gl=#{lang}") | ||
end | ||
|
||
# Inspect Http response status code | ||
# Any non 200 response code will be logged | ||
# response is set to nil in order to notify the error | ||
def validate_result | ||
return if @result.response.code == '200' | ||
|
||
Rails.logger.warn "Warning: Query Google with keyword #{@keyword} return status code #{@result.response.code}" | ||
@result = nil | ||
end | ||
|
||
def query_result | ||
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}" | ||
@result = nil | ||
else | ||
validate_result | ||
end | ||
@result | ||
end | ||
end | ||
end |