diff --git a/app/services/google/client_service.rb b/app/services/google/client_service.rb index 17eef11d..92b81b50 100644 --- a/app/services/google/client_service.rb +++ b/app/services/google/client_service.rb @@ -7,7 +7,7 @@ class ClientService BASE_SEARCH_URL2 = 'https://google.com/search' - def initialize(keyword, lang = 'en') + def initialize(keyword:, lang: 'en') escaped_keyword = CGI.escape(keyword) @uri = URI("#{BASE_SEARCH_URL2}?q=#{escaped_keyword}&hl=#{lang}&gl=#{lang}") end diff --git a/spec/services/google/client_service_spec.rb b/spec/services/google/client_service_spec.rb index 44416409..095a624f 100644 --- a/spec/services/google/client_service_spec.rb +++ b/spec/services/google/client_service_spec.rb @@ -5,13 +5,13 @@ 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(FFaker::Lorem.word).call + 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(FFaker::Lorem.word).call.request.path + path = described_class.new(keyword: FFaker::Lorem.word).call.request.path expect(path.to_s).to start_with('https://www.google.com/search') end diff --git a/spec/services/google/parser_service_spec.rb b/spec/services/google/parser_service_spec.rb index 81b435ad..a24c0bd6 100644 --- a/spec/services/google/parser_service_spec.rb +++ b/spec/services/google/parser_service_spec.rb @@ -5,48 +5,48 @@ 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 + result = Google::ClientService.new(keyword: 'squarespace').call - expect(described_class.new(result).ads_top_count).to eq(1) + expect(described_class.new(html_response: 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 + result = Google::ClientService.new(keyword: 'vpn').call - expect(described_class.new(result).ads_top_count).to eq(3) + expect(described_class.new(html_response: 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 + result = Google::ClientService.new(keyword: 'vpn').call - expect(described_class.new(result).ads_page_count).to eq(6) + expect(described_class.new(html_response: 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 + result = Google::ClientService.new(keyword: '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/') + expect(described_class.new(html_response: 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 + result = Google::ClientService.new(keyword: 'vpn').call - expect(described_class.new(result).non_ads_result_count).to eq(14) + expect(described_class.new(html_response: 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 + result = Google::ClientService.new(keyword: 'vpn').call - expect(described_class.new(result).non_ads_url.count).to eq(14) + expect(described_class.new(html_response: 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 + result = Google::ClientService.new(keyword: 'vpn').call - expect(described_class.new(result).total_link_count).to eq(113) + expect(described_class.new(html_response: result).total_link_count).to eq(113) end end end