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 = "