BraveSearch Ruby Gem
Ruby client for the Brave Search API with Rails 8 integration, Ruby 3+ pattern matching, and async support.
Features
- ๐ Multiple Search Types: Web, news, video, image, suggest, and spellcheck
- โก Async Support: Concurrent searches using Ruby 3+ Fiber.schedule
- ๐ฏ Pattern Matching: Ruby 3+ pattern matching support for results
- ๐ Rails 8 Integration: Automatic configuration with Rails credentials
- ๐งช Comprehensive Testing: RSpec tests with WebMock
- ๐ Modern Ruby: Requires Ruby 3.0+ for modern features
Installation
Add this line to your application's Gemfile:
gem 'brave_search'
And then execute:
$ bundle install
Usage
Basic Usage
client = BraveSearch::Client.new(api_key: 'your_api_key')
results = client.search(q: 'ruby programming', count: 10)
# Results wrapper with convenience methods
puts results.web_results.first[:title]
puts "Found #{results.count} total results"
Multiple Search Types
# Different search types
web_results = client.search(q: 'ruby programming')
news_results = client.news_search(q: 'ruby news')
video_results = client.video_search(q: 'ruby tutorials')
image_results = client.image_search(q: 'ruby logo')
# Suggestions and spellcheck
suggestions = client.suggest(q: 'ruby prog')
spelling = client.spellcheck(q: 'rubyy')
Ruby 3+ Pattern Matching
results = client.search(q: 'ruby programming')
case results
in { web: { results: [first, *rest] }, query: { original: String => query } }
puts "Found #{rest.length + 1} results for: '#{query}'"
puts "Top result: #{first[:title]}"
in { web: { results: [] } }
puts "No results found"
else
puts "Unexpected response"
end
Async Support
require 'async'
async_client = BraveSearch::AsyncClient.new
# Single async search
Async do
result = async_client.search(q: 'ruby programming').wait
puts result.web_results.first[:title]
end
# Concurrent searches
Async do
results = async_client.concurrent_search([
'ruby programming',
'rails framework',
'async ruby'
]).wait
results.each { |r| puts "Found #{r.count} results" }
end
S3-Compatible Storage & PDF Downloads
# Configure storage
BraveSearch.configure do |config|
config.storage_provider = :hetzner
config.storage_bucket = "research-papers"
config.storage_endpoint = "https://fsn1.your-objectstorage.com"
end
# Search and download PDFs automatically
client = BraveSearch::Client.new
downloaded = client.search_and_download_pdfs(
q: "machine learning papers filetype:pdf",
count: 5,
folder: "ml-papers/#{Date.today}"
) do |completed, total|
puts "Downloaded #{completed}/#{total} PDFs"
end
# Manual PDF extraction and download
results = client.search(q: "research papers filetype:pdf")
pdf_urls = results.pdf_urls
puts "Found #{pdf_urls.length} PDFs"
# Download to specific storage
storage = BraveSearch::Storage.for(:digitalocean,
bucket: "documents",
endpoint: "https://nyc3.digitaloceanspaces.com"
)
results.download_pdfs(storage: storage, folder: "research") do |completed, total|
puts "Progress: #{completed}/#{total}"
end
Supported Storage Providers
-
AWS S3:
:aws
-
Hetzner Object Storage:
:hetzner
-
DigitalOcean Spaces:
:digitalocean
-
Any S3-compatible:
:s3
Rails Integration
- Install the initializer:
rails generate brave_search:install
- Add your API key to Rails credentials:
rails credentials:edit
Add:
brave_api_key: your_api_key_here
- Use in your Rails app:
client = BraveSearch::Client.new
results = client.search(q: 'ruby on rails')
Configuration
BraveSearch.configure do |config|
config.api_key = 'your_api_key'
config.timeout = 30
config.retry_attempts = 3
end
Development
After checking out the repo, run bundle install
to install dependencies. Then, run rake spec
to run the tests.
License
The gem is available as open source under the terms of the MIT License.