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
perform_search(search_id: nil, freeze: false, &block)
click to toggle source
# File lib/berkeley_library/tind/api/search.rb, line 61 def perform_search(search_id: nil, freeze: false, &block) logger.info("perform_search(search_id: #{search_id.inspect})") params = search_id ? self.params.merge(search_id: search_id) : self.params next_search_id = perform_single_search(params, freeze, &block) perform_search(search_id: next_search_id, freeze: freeze, &block) if next_search_id end
perform_single_search(params, freeze, &block)
click to toggle source
# File lib/berkeley_library/tind/api/search.rb, line 68 def perform_single_search(params, freeze, &block) API.get(:search, **params) do |body| process_body(body, freeze, &block) ensure body.close end rescue APIException => e return nil if empty_result?(e) raise 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