Skip to content

Commit

Permalink
Include links in google_search results
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlindsey committed Nov 30, 2024
1 parent e30905f commit dd22e1b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
13 changes: 12 additions & 1 deletion app/services/toolbox/google_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
19 changes: 16 additions & 3 deletions test/services/toolbox/google_search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<!doctype html><html><body><div class=\"BNeawe vvjwJb AP7Wnd\">#{expected_result[:query_results]}</div></body></html>"
html_content = <<-HTML
<html>
<body>
<div class="BNeawe">
Practical Object-Oriented Design in Ruby by Sandi Metz.#{' '}
<a href="https://www.poodr.com\">Learn more</a> for more details.
</div>
<div class="OtherClass">
Not relevant content.
<a href="https://www.notrelevant.com">Ignore this</a>.
</div>
</body>
</html>
HTML

stub_request(:get, /www.google.com/)
.with(
Expand All @@ -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
Expand Down

0 comments on commit dd22e1b

Please sign in to comment.