Class: Skala::PrimoAdapter::Search::ResultTransformation::SetFacets

Inherits:
Transformator::Transformation::Step
  • Object
show all
Defined in:
lib/skala/primo_adapter/search/result_transformation/set_facets.rb

Instance Method Summary collapse

Instance Method Details

#callObject



7
8
9
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
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
# File 'lib/skala/primo_adapter/search/result_transformation/set_facets.rb', line 7

def call
  requested_facets = transformation.search_request.facets

  target.facets =
  transformation.search_brief_response.xpath("//FACET")
  .map do |_source_facet|
    # in primo facet fields are named "facet_foo", while the name is "foo"
    field = "facet_#{_source_facet.attr("NAME")}"

    corresponding_search_request_facet = requested_facets.find do |_requested_facet|
      _requested_facet.field == field
    end

    case corresponding_search_request_facet.try(:type)
    when "histogram"
      target.class::HistogramFacet.new(
        field: field,
        name: corresponding_search_request_facet.name,
        entries: begin
          _source_facet.xpath("./FACET_VALUES")
          .select do |_source_facet_value|
            _source_facet_value.attr("KEY")[/\d{4}/]
          end
          .each_with_object({}) do |_source_facet_value, _hash|
            key = _source_facet_value.attr("KEY").to_i
            count = _source_facet_value.attr("VALUE").to_i

            _hash[key] = count
          end
          .tap do |_key_count_mapping|
            keys = _key_count_mapping.keys.map(&:to_i)

            (keys.min..keys.max).each do |_key_from_range|
              _key_count_mapping[_key_from_range] ||= 0
            end
          end
          .each_with_object([]) do |(_key, _count), _array|
            _array.push(count: _count, key: _key)
          end
          .sort do |_entry, _other_entry|
            _entry[:key] <=> _other_entry[:key]
          end
        end
      )
    when "terms"
      target.class::TermsFacet.new(
        field: field,
        name: corresponding_search_request_facet.name,
        terms: _source_facet.xpath("./FACET_VALUES").map do |_source_facet_value|
          {
            count: _source_facet_value.attr("VALUE").to_i,
            term: begin
              # https://github.com/ubpb/katalog_ubpb/issues/58
              # https://github.com/sparklemotion/nokogiri/issues/214
              _source_facet_value.to_s.match(/KEY="([^"]+)"/).captures.try(:first) ||
              _source_facet_value.attr("KEY")
            end
          }
        end
      )
    end
  end
  .compact
end