Class: BlacklightIiifSearch::IiifSuggestResponse

Inherits:
Object
  • Object
show all
Includes:
Ignored
Defined in:
app/models/blacklight_iiif_search/iiif_suggest_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ignored

#ignored

Constructor Details

#initialize(solr_response, params, controller) ⇒ IiifSuggestResponse

Returns a new instance of IiifSuggestResponse.

Parameters:

  • solr_response (Blacklight::Solr::Response)
  • params (Hash)
  • controller (CatalogController)


12
13
14
15
16
17
18
# File 'app/models/blacklight_iiif_search/iiif_suggest_response.rb', line 12

def initialize(solr_response, params, controller)
  @solr_response = solr_response
  @query = params[:q]
  @document_id = params[:solr_document_id]
  @controller = controller
  @iiif_config = controller.iiif_search_config
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



6
7
8
# File 'app/models/blacklight_iiif_search/iiif_suggest_response.rb', line 6

def controller
  @controller
end

#document_idObject (readonly)

Returns the value of attribute document_id.



6
7
8
# File 'app/models/blacklight_iiif_search/iiif_suggest_response.rb', line 6

def document_id
  @document_id
end

#iiif_configObject (readonly)

Returns the value of attribute iiif_config.



6
7
8
# File 'app/models/blacklight_iiif_search/iiif_suggest_response.rb', line 6

def iiif_config
  @iiif_config
end

#queryObject (readonly)

Returns the value of attribute query.



6
7
8
# File 'app/models/blacklight_iiif_search/iiif_suggest_response.rb', line 6

def query
  @query
end

#solr_responseObject (readonly)

Returns the value of attribute solr_response.



6
7
8
# File 'app/models/blacklight_iiif_search/iiif_suggest_response.rb', line 6

def solr_response
  @solr_response
end

Instance Method Details

#iiif_search_url(term) ⇒ String

Create a URL corresponding to a IIIF Content Search request for the term

Parameters:

  • term (String)

Returns:

  • (String)


52
53
54
# File 'app/models/blacklight_iiif_search/iiif_suggest_response.rb', line 52

def iiif_search_url(term)
  controller.solr_document_iiif_search_url(document_id, q: term)
end

#term_listIIIF::OrderedHash

Constructs the termList as IIIF::Presentation::Resource

Returns:

  • (IIIF::OrderedHash)


23
24
25
26
27
28
29
30
31
# File 'app/models/blacklight_iiif_search/iiif_suggest_response.rb', line 23

def term_list
  list_id = controller.request.original_url
  term_list = IIIF::Presentation::Resource.new('@id' => list_id)
  term_list['@context'] = 'http://iiif.io/api/search/1/context.json'
  term_list['@type'] = 'search:TermList'
  term_list['terms'] = terms
  term_list['ignored'] = ignored
  term_list.to_ordered_hash(force: true, include_context: false)
end

#termsArray

Turn solr_response into array of hashes ‘try’ chain pattern copied from Blacklight::Suggest::Response#suggestions

Returns:

  • (Array)


37
38
39
40
41
42
43
44
45
46
# File 'app/models/blacklight_iiif_search/iiif_suggest_response.rb', line 37

def terms
  terms_for_list = []
  terms_array = solr_response.try(:[], 'suggest').try(:[], iiif_config[:suggester_name]).try(:[], query).try(:[], 'suggestions') || []
  terms = terms_array.map { |v| v['term'] }
  terms.sort.each do |term|
    term_hash = { match: term, url: iiif_search_url(term) }
    terms_for_list << term_hash
  end
  terms_for_list
end