class PrimoCentralCounter::Counter

Public Class Methods

call(searcher_url, queries, options = {}) click to toggle source
# File lib/primo_central_counter/counter.rb, line 7
def self.call(searcher_url, queries, options = {})
  index_field = options[:index_field]
  institution = options[:institution]
  logger = options[:logger]
  on_campus = options[:on_campus]
  uri = URI.parse(searcher_url)

  # http://stackoverflow.com/questions/11269224/ruby-https-post-with-headers
  http = Net::HTTP.new(uri.host, uri.port)

  queries.each_with_object({}) do |_query, _hash|
    request = Net::HTTP::Post.new(uri.path)
    request["Content-Type"] = "application/xml" # necessary since new soap endpoint (else -> premature end of file error)
    request["SOAPAction"] = "searchBrief"
    request.body = soap_request(
      index_field: index_field,
      query: _query,
      institution: institution,
      on_campus: on_campus
    )

    response = http.request(request)
    total_hits = response.body.match(/TOTALHITS[^\s]+/)[0][/\d+/].to_i

    log_info(logger, "Retriving TOTALHITS for #{institution} #{index_field}:\"#{_query}\" (on_campus: #{on_campus}) ... #{total_hits.to_s.rjust(9)}")
    _hash[_query] = total_hits
  end
end

Private Class Methods

log_info(logger, message) click to toggle source
# File lib/primo_central_counter/counter.rb, line 38
def self.log_info(logger, message)
  logger.info(message) if logger
end
soap_request(options = {}) click to toggle source
# File lib/primo_central_counter/counter.rb, line 42
  def self.soap_request(options = {})
    <<-xml

<env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:impl="http://primo.kobv.de/PrimoWebServices/services/searcher" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ins0="http://xml.apache.org/xml-soap">
  <env:Body>
    <impl:searchBrief>
      <searchRequestStr>
        <![CDATA[
          <searchRequest xmlns="http://www.exlibris.com/primo/xsd/wsRequest" xmlns:uic="http://www.exlibris.com/primo/xsd/primoview/uicomponents">
            <PrimoSearchRequest xmlns="http://www.exlibris.com/primo/xsd/search/request">
              <QueryTerms>
                <BoolOpeator>AND</BoolOpeator>
                <QueryTerm>
                  <IndexField>#{options[:index_field]}</IndexField>
                  <PrecisionOperator>contains</PrecisionOperator>
                  <Value>#{options[:query]}</Value>
                </QueryTerm>
              </QueryTerms>
              <StartIndex>1</StartIndex>
              <BulkSize>0</BulkSize>
              <Locations>
                <uic:Location type="adaptor" value="primo_central_multiple_fe"/>               
              </Locations>
            </PrimoSearchRequest>
            <institution>#{options[:institution]}</institution>
            <onCampus>#{options[:on_campus]}</onCampus>
          </searchRequest>
        ]]>
      </searchRequestStr>
    </impl:searchBrief>
  </env:Body>
</env:Envelope>
    xml
  end