Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from malparty/feature/google-search-raw
#6 As a user, I can query a single keyword and get its Google search raw response
- Loading branch information
Showing
10 changed files
with
312 additions
and
43 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
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,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module Google | ||
class ClientService | ||
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' | ||
|
||
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}") | ||
end | ||
|
||
def call | ||
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 | ||
|
||
# Inspect Http response status code | ||
# Any non 200 response code will be logged | ||
def valid_result?(result) | ||
return true if result&.response&.code == '200' | ||
|
||
Rails.logger.warn "Warning: Query Google with '#{@escaped_keyword}' return status code #{result.response.code}" | ||
.colorize(:yellow) | ||
|
||
false | ||
end | ||
end | ||
end |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe Google::ClientService, type: :service do | ||
context 'when querying a simple keyword' do | ||
it 'returns an HTTParty Response', vcr: 'google_search' do | ||
result = described_class.new(keyword: FFaker::Lorem.word).call | ||
|
||
expect(result).to be_an_instance_of(HTTParty::Response) | ||
end | ||
|
||
it 'queries Google Search', vcr: 'google_search' do | ||
path = described_class.new(keyword: FFaker::Lorem.word).call.request.path | ||
|
||
expect(path.to_s).to start_with(described_class::BASE_SEARCH_URL) | ||
end | ||
end | ||
|
||
context 'when google returns an HTTP error' do | ||
it 'returns false', vcr: 'google_warn' do | ||
result = described_class.new(keyword: FFaker::Lorem.word).call | ||
|
||
expect(result).to eq(false) | ||
end | ||
|
||
it 'logs a warning with the escaped keyword', vcr: 'google_warn' do | ||
allow(Rails.logger).to receive(:warn) | ||
|
||
word = FFaker::Lorem.word | ||
described_class.new(keyword: word).call | ||
|
||
expect(Rails.logger).to have_received(:warn).with(/#{CGI.escape(word)}/) | ||
end | ||
end | ||
end |
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