Class: BraveSearch::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/brave_search/client.rb

Direct Known Subclasses

AsyncClient

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
# File 'lib/brave_search/client.rb', line 10

def initialize(api_key: nil)
  @api_key = api_key || BraveSearch.config.api_key
  raise AuthenticationError, "API key is required" unless @api_key

  self.class.base_uri BraveSearch.config.base_url
  self.class.default_timeout BraveSearch.config.timeout
end

Instance Method Details

#image_search(q:, count: 10, **options) ⇒ Object



77
78
79
80
81
82
# File 'lib/brave_search/client.rb', line 77

def image_search(q:, count: 10, **options)
  params = build_params(q: q, count: count, **options)
  response = make_request("/images/search", params)
  data = handle_response(response)
  Results.new(data)
end

#news_search(q:, count: 10, **options) ⇒ Object



63
64
65
66
67
68
# File 'lib/brave_search/client.rb', line 63

def news_search(q:, count: 10, **options)
  params = build_params(q: q, count: count, **options)
  response = make_request("/news/search", params)
  data = handle_response(response)
  Results.new(data)
end

#search(q:, count: 10, **options) ⇒ Object



18
19
20
21
22
23
# File 'lib/brave_search/client.rb', line 18

def search(q:, count: 10, **options)
  params = build_params(q: q, count: count, **options)
  response = make_request("/web/search", params)
  data = handle_response(response)
  Results.new(data)
end

#search_and_download_pdfs(q:, count: 10, storage: nil, folder: "pdfs", &progress_callback) ⇒ Object



25
26
27
28
# File 'lib/brave_search/client.rb', line 25

def search_and_download_pdfs(q:, count: 10, storage: nil, folder: "pdfs", &progress_callback)
  results = search(q: q, count: count)
  results.download_pdfs(storage: storage, folder: folder, &progress_callback)
end

#search_and_download_pdfs_async(q:, storage_config: nil, folder: "pdfs", **options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/brave_search/client.rb', line 52

def search_and_download_pdfs_async(q:, storage_config: nil, folder: "pdfs", **options)
  return unless defined?(ActiveJob)

  Jobs::PdfDownloadJob.perform_later(
    query: q,
    storage_config: storage_config,
    folder: folder,
    **options
  )
end

#search_and_export(q:, format:, storage: nil, key: nil, count: 10, **options) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/brave_search/client.rb', line 30

def search_and_export(q:, format:, storage: nil, key: nil, count: 10, **options)
  results = search(q: q, count: count, **options)

  if storage && key
    results.export_to_storage(format: format, storage: storage, key: key)
  else
    results.export(format: format)
  end
end

#search_and_export_async(q:, format:, storage_config: nil, key: nil, **options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/brave_search/client.rb', line 40

def search_and_export_async(q:, format:, storage_config: nil, key: nil, **options)
  return unless defined?(ActiveJob)

  Jobs::ExportJob.perform_later(
    query: q,
    format: format,
    storage_config: storage_config,
    key: key,
    **options
  )
end

#spellcheck(q:, **options) ⇒ Object



90
91
92
93
94
# File 'lib/brave_search/client.rb', line 90

def spellcheck(q:, **options)
  params = build_params(q: q, **options)
  response = make_request("/spellcheck", params)
  handle_response(response)
end

#suggest(q:, **options) ⇒ Object



84
85
86
87
88
# File 'lib/brave_search/client.rb', line 84

def suggest(q:, **options)
  params = build_params(q: q, **options)
  response = make_request("/suggest/search", params)
  handle_response(response)
end

#summarizerObject



96
97
98
# File 'lib/brave_search/client.rb', line 96

def summarizer
  @summarizer ||= Summarizer.new(self)
end

#video_search(q:, count: 10, **options) ⇒ Object



70
71
72
73
74
75
# File 'lib/brave_search/client.rb', line 70

def video_search(q:, count: 10, **options)
  params = build_params(q: q, count: count, **options)
  response = make_request("/videos/search", params)
  data = handle_response(response)
  Results.new(data)
end