class BerkeleyLibrary::TIND::API::Search

Attributes

collection[R]
date_range[R]
format[R]
index[R]
pattern[R]

Public Class Methods

new(collection: nil, pattern: nil, index: nil, date_range: nil, format: Format::XML) click to toggle source
# File lib/berkeley_library/tind/api/search.rb, line 14
def initialize(collection: nil, pattern: nil, index: nil, date_range: nil, format: Format::XML)
  raise ArgumentError, 'Search requires a collection' unless collection

  @collection = collection
  @pattern = pattern
  @index = index
  @date_range = DateRange.ensure_date_range(date_range)
  @format = Format.ensure_format(format)
end

Public Instance Methods

each_result(freeze: false, &block) click to toggle source

Iterates over the records returned by this search. @overload each_result(freeze: false, &block)

Yields each record to the provided block.
@param freeze [Boolean] whether to freeze each record before yielding.
@yieldparam marc_record [MARC::Record] each record
@return [self]

@overload each_result(freeze: false)

Returns an enumerator of the records.
@param freeze [Boolean] whether to freeze each record before yielding.
@return [Enumerable<MARC::Record>] the records
# File lib/berkeley_library/tind/api/search.rb, line 52
def each_result(freeze: false, &block)
  return to_enum(:each_result, freeze: freeze) unless block_given?

  perform_search(freeze: freeze, &block)
  self
end
params() click to toggle source

rubocop: disable Metrics/AbcSize

# File lib/berkeley_library/tind/api/search.rb, line 25
def params
  @params ||= {}.tap do |params|
    params[:c] = collection if collection
    params[:p] = pattern if pattern
    params[:f] = index if index
    params.merge!(date_range.to_params) if date_range
    params[:format] = self.format.to_s if self.format
  end
end
results() click to toggle source

Performs this search and returns the results as array. @return [Array<MARC::Record>] the results

# File lib/berkeley_library/tind/api/search.rb, line 38
def results
  each_result.to_a
end

Private Instance Methods

empty_result?(api_ex) click to toggle source
# File lib/berkeley_library/tind/api/search.rb, line 88
def empty_result?(api_ex)
  return false unless api_ex.status_code == 500
  return false unless (body = api_ex.body)
  return false unless (result = JSON.parse(body, symbolize_names: true))

  result[:success] == false && result[:error].include?('0 values')
rescue JSON::ParserError
  false
end
process_body(body, freeze, &block) click to toggle source
# File lib/berkeley_library/tind/api/search.rb, line 80
def process_body(body, freeze, &block)
  xml_reader = BerkeleyLibrary::TIND::MARC::XMLReader.read(body, freeze: freeze)
  xml_reader.each(&block)
  logger.debug("yielded #{xml_reader.records_yielded} of #{xml_reader.total} records")
  logger.debug("next search ID: #{xml_reader.search_id}")
  xml_reader.search_id if xml_reader.records_yielded > 0
end