Skip to content

Commit

Permalink
[#7] Rename GoogleService namespace in Google
Browse files Browse the repository at this point in the history
Rebase from google-search-raw
  • Loading branch information
malparty committed Jun 30, 2021
1 parent a4e09d0 commit 9ed9447
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
29 changes: 14 additions & 15 deletions app/services/google/client_service.rb
Expand Up @@ -7,21 +7,21 @@ 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}")
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
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
end

private
Expand All @@ -31,10 +31,9 @@ def call
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}"
Rails.logger.warn "Warning: Query Google with keyword #{@keyword} return status code #{@result.response.code}"
.colorize(:yellow)

false
@result = nil
end
end
end
8 changes: 8 additions & 0 deletions spec/services/google_service/client_service_spec.rb
Expand Up @@ -5,13 +5,21 @@
RSpec.describe Google::ClientService, type: :service do
context 'when querying a simple keyword' do
it 'returns an HTTParty Response', vcr: 'google_search' do
<<<<<<< HEAD
result = described_class.query(FFaker::Lorem.word)
=======
result = described_class.new(FFaker::Lorem.word).call
>>>>>>> d03ca6f ([#7] Rename GoogleService namespace in Google)

expect(result).to be_an_instance_of(HTTParty::Response)
end

it 'queries Google Search', vcr: 'google_search' do
<<<<<<< HEAD
path = described_class.query(FFaker::Lorem.word).request.path
=======
path = described_class.new(FFaker::Lorem.word).call.request.path
>>>>>>> d03ca6f ([#7] Rename GoogleService namespace in Google)

expect(path.to_s).to start_with('https://www.google.com/search')
end
Expand Down
52 changes: 52 additions & 0 deletions spec/services/google_service/parser_service_spec.rb
@@ -0,0 +1,52 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Google::ParserService, type: :service do
context 'when parsing a page having 1 top ad' do
it 'counts exactly 1 top ad', vcr: 'google_search_top_ads_1' do
result = Google::ClientService.new('squarespace').call

expect(described_class.new(result).ads_top_count).to eq(1)
end
end

context 'when parsing a page having 3 top ads, 3 bottom ads and 14 non ad links' do
it 'counts exactly 3 top ads', vcr: 'google_search_top_ads_6' do
result = Google::ClientService.new('vpn').call

expect(described_class.new(result).ads_top_count).to eq(3)
end

it 'counts exactly 6 ads in total', vcr: 'google_search_top_ads_6' do
result = Google::ClientService.new('vpn').call

expect(described_class.new(result).ads_page_count).to eq(6)
end

it 'finds exactly the 3 top ads urls', vcr: 'google_search_top_ads_6' do
result = Google::ClientService.new('vpn').call

expect(described_class.new(result).ads_top_url).to contain_exactly('https://cloud.google.com/free', 'https://www.expressvpn.com/', 'https://www.top10vpn.com/best-vpn-for-vietnam/')
end

it 'counts exactly 14 non ad results', vcr: 'google_search_top_ads_6' do
result = Google::ClientService.new('vpn').call

expect(described_class.new(result).non_ads_result_count).to eq(14)
end

it 'gets 14 results', vcr: 'google_search_top_ads_6' do
result = Google::ClientService.new('vpn').call

expect(described_class.new(result).non_ads_url.count).to eq(14)
end

it 'gets exactly 113 links', vcr: 'google_search_top_ads_6' do
# Counted from cassette html raw code
result = Google::ClientService.new('vpn').call

expect(described_class.new(result).total_link_count).to eq(113)
end
end
end

0 comments on commit 9ed9447

Please sign in to comment.