New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#6 As a user, I can query a single keyword and get its Google search raw response #36
Changes from all commits
b402a7f
dbf2e9d
89a9645
27fbc17
ccb4c85
b180494
3c0b3d3
f430136
18c3631
a2d7d94
36900b1
9de1276
5184e73
8b3ffc0
11f09cd
f60ba32
2441302
1af4e64
0236a3a
5a52417
67171f8
242c5db
c74da8a
d289a9b
603ac8e
b2f1d52
d233c30
8ace4ab
f409671
a50ea52
73dac87
dbaa2dd
3b993d8
4f4805c
f9d9607
36c0d94
500bda9
44ef5d2
ab621cc
5d8ec49
dd4b0f8
0e68db9
902742d
93704d4
4c5bed3
777e61b
8dbecbc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.