Class: QuickSearch::TypeaheadController

Inherits:
ApplicationController show all
Defined in:
app/controllers/quick_search/typeahead_controller.rb

Instance Method Summary collapse

Instance Method Details

#typeaheadObject

This method should return a list of search suggestions for a given searcher It should return errors if there is no param called ‘searcher’, if the searcher does not exist or if the searcher doesn’t implement the ‘typeahead’ method Otherwise, it should return the result of calling the ‘typeahead’ method as JSON



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/quick_search/typeahead_controller.rb', line 10

def typeahead
  # :searcher param expected to be name of a searcher with underscores, ie: ematrix_journal
  searcher = params[:searcher]
  query = params[:q] or ''
  total = params[:total] or 3

  if searcher.blank?
    logger.error "Typeahead request: no searcher param provided"
    head :bad_request
    return nil
  end

  searcher_string = "QuickSearch::#{searcher.camelize}Searcher"

  begin 
    klass = searcher_string.constantize
  rescue NameError
    logger.error "Typeahead request: searcher #{searcher} does not exist"
    head :bad_request
    return nil
  end

  if klass.method_defined? :typeahead
    typeahead_result = klass.new(HTTPClient.new, query, total).typeahead

    if params.has_key? 'callback'
      render json: typeahead_result, callback: params['callback']
    else
      render json: typeahead_result
    end
  else
    logger.error "Typeahead request: searcher #{searcher} has no typeahead method"
    head :bad_request
    return nil
  end
end