Sha256: 6f4e84d7dbc2a5bd7ce7ff833779f1998c614b6b0d3ef9fc77212859562f0221

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

# A fake engine that simply makes up it's results, used in testing. 
#
# Used in BentoSearch's own automated tests, but exposed publically because
# can be useful to use in your tests for software that uses BentoSearch too. 
#
# The nature of the fake results can be controlled by config variables:
#
# [:total_items]  total_items to report
# [:sort_definitions] hash for #sort_definitions
# [:link]             link to give to each item in results
# [:error]            set to an error value hash and results returned
#                     will all be failed? with that error hash. 
# [:timing]           in seconds, fill out the results as if they took
#                     this long. 
class BentoSearch::MockEngine
    include BentoSearch::SearchEngine
    
    def search_implementation(args)
      results = BentoSearch::Results.new
      
      if configuration.error
        results.error = configuration.error
        return results
      end
      
      1.upto(configuration.num_results || args[:per_page] ) do |i|
        results << BentoSearch::ResultItem.new(:title => "Item #{i}: #{args[:query]}", :link => configuration.link)
      end
      results.total_items = configuration.total_items      
      return results
    end    
    
    def search(*args)
      results = super(*args)
      results.timing = configuration.timing if configuration.timing
      return results
    end
    
    def self.default_configuration
      { :num_results => nil,
        :total_items => 1000, 
        :link => "http://example.org",
        :error => nil,
        :timing => nil}
    end
    
    def sort_definitions
      configuration.sort_definitions || {}
    end
    
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bento_search-0.5.0 app/search_engines/bento_search/mock_engine.rb