Class: RubyCode::SearchProviders::MultiProvider
- Inherits:
-
Object
- Object
- RubyCode::SearchProviders::MultiProvider
- Defined in:
- lib/rubycode/search_providers/multi_provider.rb
Overview
Multi-provider search with automatic fallback
Instance Attribute Summary collapse
-
#last_used_provider ⇒ Object
readonly
Returns the value of attribute last_used_provider.
Instance Method Summary collapse
-
#add_provider(provider) ⇒ Object
Add a provider to the list.
-
#initialize(providers: []) ⇒ MultiProvider
constructor
A new instance of MultiProvider.
-
#provider_names ⇒ Object
Get list of available providers.
- #search(query, max_results: 5) ⇒ Object
Constructor Details
#initialize(providers: []) ⇒ MultiProvider
Returns a new instance of MultiProvider.
9 10 11 12 |
# File 'lib/rubycode/search_providers/multi_provider.rb', line 9 def initialize(providers: []) @providers = providers @last_used_provider = nil end |
Instance Attribute Details
#last_used_provider ⇒ Object (readonly)
Returns the value of attribute last_used_provider.
7 8 9 |
# File 'lib/rubycode/search_providers/multi_provider.rb', line 7 def last_used_provider @last_used_provider end |
Instance Method Details
#add_provider(provider) ⇒ Object
Add a provider to the list
37 38 39 |
# File 'lib/rubycode/search_providers/multi_provider.rb', line 37 def add_provider(provider) @providers << provider end |
#provider_names ⇒ Object
Get list of available providers
42 43 44 |
# File 'lib/rubycode/search_providers/multi_provider.rb', line 42 def provider_names @providers.map { |p| p.class.name.split("::").last } end |
#search(query, max_results: 5) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rubycode/search_providers/multi_provider.rb', line 14 def search(query, max_results: 5) last_error = nil @providers.each do |provider| results = provider.search(query, max_results: max_results) if results && !results.empty? @last_used_provider = provider.class.name.split("::").last return results end rescue StandardError => e last_error = e # Continue to next provider end # If all providers failed, raise the last error raise last_error if last_error # If no error but no results, return empty array @last_used_provider = nil [] end |