Sha256: 843fe074a7b699dbdddb0e3745aaa452db64fef1b8eb2b51469170bcf4ca0590

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

require "celsius/transformation/step"
require "ox"

class Celsius::Primo::SoapApi::Searcher::SearchBrief::SearchRequestTransformation::
  AddSortByList < Celsius::Transformation::Step

  #
  # SortField is optional, so we have to add it on the fly if necessary
  #
  def call
    if sort_fields = (Celsius::Hash.deep_find_key(source, :sort) || []).first
      # map sort field values to primo equivalents
      primo_sort_fields = sort_fields.map(&transformation.method(:sort_field_mapping))

      # memoize this node to shorten further lines
      primo_search_request = transformation.inner_search_request.locate("PrimoSearchRequest").first

      # Order matters, SortField *must* be inserted after <Languages>
      index_of_languages = primo_search_request.nodes.index do |node|
        node.value == "Languages"
      end

      # primo can only handle *one* sort field
      sort_by_list = Ox.parse(
        <<-xml
          <SortByList>
            <SortField>#{primo_sort_fields.first}</SortField>
          </SortByList>
        xml
      )

      # insert the SortByList at the appropriate position
      primo_search_request.nodes.insert(index_of_languages + 1, sort_by_list)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
celsius-primo-0.1.3 lib/celsius/primo/soap_api/searcher/search_brief/search_request_transformation/add_sort_by_list.rb
celsius-primo-0.1.2 lib/celsius/primo/soap_api/searcher/search_brief/search_request_transformation/add_sort_by_list.rb
celsius-primo-0.1.1 lib/celsius/primo/soap_api/searcher/search_brief/search_request_transformation/add_sort_by_list.rb
celsius-primo-0.1.0 lib/celsius/primo/soap_api/searcher/search_brief/search_request_transformation/add_sort_by_list.rb