Class: BlacklightIiifSearch::IiifSearchResponse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Ignored

#ignored

Constructor Details

#initialize(solr_response, parent_document, controller) ⇒ IiifSearchResponse

Returns a new instance of IiifSearchResponse.

Parameters:

  • solr_response (Blacklight::Solr::Response)
  • parent_document (SolrDocument)
  • controller (CatalogController)


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

def initialize(solr_response, parent_document, controller)
  @solr_response = solr_response
  @parent_document = parent_document
  @controller = controller
  @iiif_config = controller.iiif_search_config
  @resources = []
  @hits = []
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



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

def controller
  @controller
end

#iiif_configObject (readonly)

Returns the value of attribute iiif_config.



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

def iiif_config
  @iiif_config
end

#solr_responseObject (readonly)

Returns the value of attribute solr_response.



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

def solr_response
  @solr_response
end

Instance Method Details

#annotation_listIIIF::OrderedHash

constructs the IIIF::Presentation::AnnotationList

Returns:

  • (IIIF::OrderedHash)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/blacklight_iiif_search/iiif_search_response.rb', line 24

def annotation_list
  list_id = controller.request.original_url
  anno_list = IIIF::Presentation::AnnotationList.new('@id' => list_id)
  anno_list['@context'] = %w[
    http://iiif.io/api/presentation/2/context.json
    http://iiif.io/api/search/1/context.json
  ]
  anno_list['resources'] = resources
  anno_list['hits'] = @hits
  anno_list['within'] = within
  anno_list['prev'] = paged_url(solr_response.prev_page) if solr_response.prev_page
  anno_list['next'] = paged_url(solr_response.next_page) if solr_response.next_page
  anno_list['startIndex'] = 0 unless solr_response.total_pages > 1
  anno_list.to_ordered_hash(force: true, include_context: false)
end

#clean_paramsActionController::Parameters

remove ignored or irrelevant params from the params hash

Returns:

  • (ActionController::Parameters)


98
99
100
101
# File 'app/models/blacklight_iiif_search/iiif_search_response.rb', line 98

def clean_params
  remove = ignored.map(&:to_sym)
  controller.iiif_search_params.except(*%i[page solr_document_id] + remove)
end

#paged_url(page_index) ⇒ String

create a URL for the previous/next page of results

Returns:

  • (String)


91
92
93
# File 'app/models/blacklight_iiif_search/iiif_search_response.rb', line 91

def paged_url(page_index)
  controller.solr_document_iiif_search_url(clean_params.merge(page: page_index))
end

#resourcesArray

Return an array of IiifSearchAnnotation objects

Returns:

  • (Array)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/blacklight_iiif_search/iiif_search_response.rb', line 43

def resources
  @total = 0
  solr_response['highlighting'].each do |id, hl_hash|
    hit = { '@type': 'search:Hit', 'annotations': [] }
    document = solr_response.documents.select { |v| v[:id] == id }.first
    if hl_hash.empty?
      @total += 1
      annotation = IiifSearchAnnotation.new(document,
                                            solr_response.params['q'],
                                            0, nil, controller,
                                            @parent_document)
      @resources << annotation.as_hash
      hit[:annotations] << annotation.annotation_id
    else
      hl_hash.each_value do |hl_array|
        hl_array.each_with_index do |hl, hl_index|
          @total += 1
          annotation = IiifSearchAnnotation.new(document,
                                                solr_response.params['q'],
                                                hl_index, hl, controller,
                                                @parent_document)
          @resources << annotation.as_hash
          hit[:annotations] << annotation.annotation_id
        end
      end
    end
    @hits << hit
  end
  @resources
end

#withinIIIF::Presentation::Layer

Returns:

  • (IIIF::Presentation::Layer)


76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/blacklight_iiif_search/iiif_search_response.rb', line 76

def within
  within_hash = IIIF::Presentation::Layer.new
  within_hash['ignored'] = ignored
  if solr_response.total_pages > 1
    within_hash['first'] = paged_url(1)
    within_hash['last'] = paged_url(solr_response.total_pages)
  else
    within_hash['total'] = @total
  end
  within_hash
end