Class: BraveSearch::Results
- Inherits:
-
Object
- Object
- BraveSearch::Results
- Defined in:
- lib/brave_search/results.rb
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#raw_data ⇒ Object
readonly
Returns the value of attribute raw_data.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #all_text_content ⇒ Object
- #count ⇒ Object
-
#deconstruct_keys(keys) ⇒ Object
Support pattern matching (Ruby 3+).
- #download_pdfs(storage: nil, folder: "pdfs", &progress_callback) ⇒ Object
- #empty? ⇒ Boolean
- #export(format:) ⇒ Object
- #export_to_storage(format:, storage:, key:) ⇒ Object
- #image_results ⇒ Object
- #infobox ⇒ Object
-
#initialize(data) ⇒ Results
constructor
A new instance of Results.
- #locations ⇒ Object
- #news_results ⇒ Object
- #pdf_urls ⇒ Object
- #spell ⇒ Object
- #summarize_with(client) ⇒ Object
-
#to_h ⇒ Object
Convert to hash for easy access.
- #video_results ⇒ Object
- #web_results ⇒ Object
Constructor Details
#initialize(data) ⇒ Results
Returns a new instance of Results.
7 8 9 10 |
# File 'lib/brave_search/results.rb', line 7 def initialize(data) @raw_data = data @query = data[:query] end |
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query.
5 6 7 |
# File 'lib/brave_search/results.rb', line 5 def query @query end |
#raw_data ⇒ Object (readonly)
Returns the value of attribute raw_data.
5 6 7 |
# File 'lib/brave_search/results.rb', line 5 def raw_data @raw_data end |
Instance Method Details
#[](key) ⇒ Object
59 60 61 |
# File 'lib/brave_search/results.rb', line 59 def [](key) @raw_data[key] end |
#all_text_content ⇒ Object
86 87 88 89 90 91 |
# File 'lib/brave_search/results.rb', line 86 def all_text_content content = [] content += web_results.flat_map { |r| [r[:title], r[:description]].compact } content += news_results.flat_map { |r| [r[:title], r[:description]].compact } content.join(" ") end |
#count ⇒ Object
44 45 46 |
# File 'lib/brave_search/results.rb', line 44 def count web_results.length + news_results.length + video_results.length + image_results.length end |
#deconstruct_keys(keys) ⇒ Object
Support pattern matching (Ruby 3+)
49 50 51 52 |
# File 'lib/brave_search/results.rb', line 49 def deconstruct_keys(keys) @raw_data.slice(*keys) if keys @raw_data end |
#download_pdfs(storage: nil, folder: "pdfs", &progress_callback) ⇒ Object
67 68 69 70 |
# File 'lib/brave_search/results.rb', line 67 def download_pdfs(storage: nil, folder: "pdfs", &progress_callback) downloader = PdfDownloader.new(storage: storage) downloader.batch_download(pdf_urls, folder: folder, &progress_callback) end |
#empty? ⇒ Boolean
40 41 42 |
# File 'lib/brave_search/results.rb', line 40 def empty? web_results.empty? && news_results.empty? && video_results.empty? && image_results.empty? end |
#export(format:) ⇒ Object
72 73 74 75 |
# File 'lib/brave_search/results.rb', line 72 def export(format:) exporter = Exporter.for(format) exporter.export(self) end |
#export_to_storage(format:, storage:, key:) ⇒ Object
77 78 79 80 |
# File 'lib/brave_search/results.rb', line 77 def export_to_storage(format:, storage:, key:) exporter = Exporter.for(format) exporter.export_to_storage(self, storage: storage, key: key) end |
#image_results ⇒ Object
24 25 26 |
# File 'lib/brave_search/results.rb', line 24 def image_results @raw_data.dig(:images, :results) || [] end |
#infobox ⇒ Object
32 33 34 |
# File 'lib/brave_search/results.rb', line 32 def infobox @raw_data.dig(:mixed, :infobox) end |
#locations ⇒ Object
28 29 30 |
# File 'lib/brave_search/results.rb', line 28 def locations @raw_data.dig(:mixed, :locations) || [] end |
#news_results ⇒ Object
16 17 18 |
# File 'lib/brave_search/results.rb', line 16 def news_results @raw_data.dig(:news, :results) || [] end |
#pdf_urls ⇒ Object
63 64 65 |
# File 'lib/brave_search/results.rb', line 63 def pdf_urls web_results.filter_map { |result| result[:url] if result[:url]&.end_with?(".pdf") } end |
#spell ⇒ Object
36 37 38 |
# File 'lib/brave_search/results.rb', line 36 def spell @raw_data[:spell] end |
#summarize_with(client) ⇒ Object
82 83 84 |
# File 'lib/brave_search/results.rb', line 82 def summarize_with(client) client.summarizer.search_and_summarize(q: @query || "search results") end |
#to_h ⇒ Object
Convert to hash for easy access
55 56 57 |
# File 'lib/brave_search/results.rb', line 55 def to_h @raw_data end |
#video_results ⇒ Object
20 21 22 |
# File 'lib/brave_search/results.rb', line 20 def video_results @raw_data.dig(:videos, :results) || [] end |
#web_results ⇒ Object
12 13 14 |
# File 'lib/brave_search/results.rb', line 12 def web_results @raw_data.dig(:web, :results) || [] end |