diff --git a/app/services/google/parser_service.rb b/app/services/google/parser_service.rb index 0c0e018f..a5e9fafd 100644 --- a/app/services/google/parser_service.rb +++ b/app/services/google/parser_service.rb @@ -20,20 +20,22 @@ def initialize(html_response:) @document.css('#footcnt a').add_class('footer-links') end - # Write parsed data directly in the keyword object argument - # and return back the keyword object - def parse_into!(keyword) - keyword.ads_top_count = ads_top_count - keyword.ads_page_count = ads_page_count - keyword.ads_top_url = ads_top_url - keyword.ads_page_url = ads_page_url - keyword.non_ads_result_count = non_ads_result_count - keyword.total_link_count = total_link_count - keyword.html = @html - - keyword + # Parse html data and return a hash with the results + def call + { + ads_top_count: ads_top_count, + ads_page_count: ads_page_count, + ads_top_url: ads_top_url, + ads_page_url: ads_page_url, + non_ads_result_count: non_ads_result_count, + non_ads_url: non_ads_url, + total_link_count: total_link_count, + html: @html + } end + private + def ads_top_count @document.css("##{AD_CONTAINER_ID} .#{ADWORDS_CLASS}").count end diff --git a/spec/services/google/parser_service_spec.rb b/spec/services/google/parser_service_spec.rb index a24c0bd6..79a33e6b 100644 --- a/spec/services/google/parser_service_spec.rb +++ b/spec/services/google/parser_service_spec.rb @@ -7,7 +7,7 @@ it 'counts exactly 1 top ad', vcr: 'google_search_top_ads_1' do result = Google::ClientService.new(keyword: 'squarespace').call - expect(described_class.new(html_response: result).ads_top_count).to eq(1) + expect(described_class.new(html_response: result).call[:ads_top_count]).to eq(1) end end @@ -15,38 +15,38 @@ it 'counts exactly 3 top ads', vcr: 'google_search_top_ads_6' do result = Google::ClientService.new(keyword: 'vpn').call - expect(described_class.new(html_response: result).ads_top_count).to eq(3) + expect(described_class.new(html_response: result).call[: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(keyword: 'vpn').call - expect(described_class.new(html_response: result).ads_page_count).to eq(6) + expect(described_class.new(html_response: result).call[: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(keyword: 'vpn').call - 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/') + expect(described_class.new(html_response: result).call[: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(keyword: 'vpn').call - expect(described_class.new(html_response: result).non_ads_result_count).to eq(14) + expect(described_class.new(html_response: result).call[:non_ads_result_count]).to eq(14) end it 'gets 14 results', vcr: 'google_search_top_ads_6' do result = Google::ClientService.new(keyword: 'vpn').call - expect(described_class.new(html_response: result).non_ads_url.count).to eq(14) + expect(described_class.new(html_response: result).call[: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(keyword: 'vpn').call - expect(described_class.new(html_response: result).total_link_count).to eq(113) + expect(described_class.new(html_response: result).call[:total_link_count]).to eq(113) end end end