From dd22e1bd2862d81681cde1e9d6bba1a351938ff5 Mon Sep 17 00:00:00 2001 From: Matt Lindsey Date: Sat, 30 Nov 2024 09:07:15 -0500 Subject: [PATCH] Include links in google_search results --- app/services/toolbox/google_search.rb | 13 ++++++++++++- test/services/toolbox/google_search_test.rb | 19 ++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/services/toolbox/google_search.rb b/app/services/toolbox/google_search.rb index 61fd7dd8..a37867c6 100644 --- a/app/services/toolbox/google_search.rb +++ b/app/services/toolbox/google_search.rb @@ -11,7 +11,18 @@ def google_search(query_s:) encoded_query = URI.encode_www_form_component(query_s) response_body = get("https://www.google.com/search?q=#{encoded_query}").get_body doc = Nokogiri::HTML(response_body) - results = doc.css("div.BNeawe").map(&:text).join("\n") + + results = doc.css("div.BNeawe").map do |div| + div.children.map do |node| + if node.name == "a" + anchor_text = node.text.strip + href = node["href"] + "#{anchor_text} (#{href})" + else + node.text.strip + end + end.join(" ") + end.join("\n") { message_to_user: "Web query: #{query_s}", diff --git a/test/services/toolbox/google_search_test.rb b/test/services/toolbox/google_search_test.rb index 547c39bb..795a30f5 100644 --- a/test/services/toolbox/google_search_test.rb +++ b/test/services/toolbox/google_search_test.rb @@ -9,9 +9,22 @@ class Toolbox::GoogleSearchTest < ActiveSupport::TestCase test "google_search returns the expected result" do expected_result = { message_to_user: "Web query: Sandi Metz POODR title", - query_results: "Practical Object-Oriented Design in Ruby by Sandi Metz" + query_results: "Practical Object-Oriented Design in Ruby by Sandi Metz. Learn more (https://www.poodr.com) for more details." } - body = "
#{expected_result[:query_results]}
" + html_content = <<-HTML + + +
+ Practical Object-Oriented Design in Ruby by Sandi Metz.#{' '} + Learn more for more details. +
+
+ Not relevant content. + Ignore this. +
+ + + HTML stub_request(:get, /www.google.com/) .with( @@ -21,7 +34,7 @@ class Toolbox::GoogleSearchTest < ActiveSupport::TestCase "User-Agent"=>"Ruby" } ) - .to_return(status: 200, body: body, headers: {}) + .to_return(status: 200, body: html_content, headers: {}) result = @google_search.google_search(query_s: "Sandi Metz POODR title") assert_equal expected_result, result