Class: BraveSearch::Results

Inherits:
Object
  • Object
show all
Defined in:
lib/brave_search/results.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#queryObject (readonly)

Returns the value of attribute query.



5
6
7
# File 'lib/brave_search/results.rb', line 5

def query
  @query
end

#raw_dataObject (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_contentObject



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

#countObject



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

Returns:

  • (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_resultsObject



24
25
26
# File 'lib/brave_search/results.rb', line 24

def image_results
  @raw_data.dig(:images, :results) || []
end

#infoboxObject



32
33
34
# File 'lib/brave_search/results.rb', line 32

def infobox
  @raw_data.dig(:mixed, :infobox)
end

#locationsObject



28
29
30
# File 'lib/brave_search/results.rb', line 28

def locations
  @raw_data.dig(:mixed, :locations) || []
end

#news_resultsObject



16
17
18
# File 'lib/brave_search/results.rb', line 16

def news_results
  @raw_data.dig(:news, :results) || []
end

#pdf_urlsObject



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

#spellObject



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_hObject

Convert to hash for easy access



55
56
57
# File 'lib/brave_search/results.rb', line 55

def to_h
  @raw_data
end

#video_resultsObject



20
21
22
# File 'lib/brave_search/results.rb', line 20

def video_results
  @raw_data.dig(:videos, :results) || []
end

#web_resultsObject



12
13
14
# File 'lib/brave_search/results.rb', line 12

def web_results
  @raw_data.dig(:web, :results) || []
end