Module: BerkeleyLibrary::Alma::SRU

Extended by:
SRU
Includes:
Constants, Logging
Included in:
SRU
Defined in:
lib/berkeley_library/alma/sru/sru.rb,
lib/berkeley_library/alma/sru/xml_reader.rb

Defined Under Namespace

Classes: XMLReader

Constant Summary collapse

DEFAULT_MAX_RECORDS =
10

Constants included from Constants

Constants::ALMA_RECORD_RE, Constants::DEFAULT_USER_AGENT, Constants::MILLENNIUM_RECORD_RE

Instance Method Summary collapse

Instance Method Details

#get_marc_records(*record_ids, max_records: DEFAULT_MAX_RECORDS, freeze: false) ⇒ Enumerator::Lazy<MARC::Record>

Given a list of record IDs, returns the MARC records for each ID (if found). Note that the order of the records is not guaranteed.

Parameters:

  • record_ids (Array<String, RecordId>)

    the record IDs to look up

  • freeze (Boolean) (defaults to: false)

    whether to freeze the records

  • max_records (defaults to: DEFAULT_MAX_RECORDS)

    the number of records per SRU page.

Returns:

  • (Enumerator::Lazy<MARC::Record>)

    the records

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
# File 'lib/berkeley_library/alma/sru/sru.rb', line 27

def get_marc_records(*record_ids, max_records: DEFAULT_MAX_RECORDS, freeze: false)
  # noinspection RubyMismatchedReturnType
  parsed_ids = record_ids.filter_map { |id| RecordId.parse(id) }
  raise ArgumentError, "Argument #{record_ids.inspect} contain no valid record IDs" if parsed_ids.empty?

  sru_query_value = parsed_ids.map(&:sru_query_value).join(' or ')
  SRU.marc_records_for(sru_query_value, max_records: max_records, freeze: freeze)
end

#make_sru_query(query_value, max_records: DEFAULT_MAX_RECORDS) ⇒ String?

Makes an SRU query for the specified query value and returns the query response as a string.

Parameters:

  • query_value (String)

    the value of the SRU query parameter

  • max_records (defaults to: DEFAULT_MAX_RECORDS)

    the number of records per SRU page

Returns:

  • (String, nil)

    the SRU query response body, or nil in the event of an error.



57
58
59
60
# File 'lib/berkeley_library/alma/sru/sru.rb', line 57

def make_sru_query(query_value, max_records: DEFAULT_MAX_RECORDS)
  uri = sru_query_uri(query_value, max_records: max_records)
  do_get(uri)
end

#marc_records_for(query_value, max_records: DEFAULT_MAX_RECORDS, freeze: false) ⇒ Enumerator::Lazy<MARC::Record>

Makes an SRU query for the specified query value and returns the query response as MARC records.

Parameters:

  • query_value (String)

    the value of the SRU query parameter

  • freeze (Boolean) (defaults to: false)

    whether to freeze the records

  • max_records (defaults to: DEFAULT_MAX_RECORDS)

    the number of records per SRU page

Returns:

  • (Enumerator::Lazy<MARC::Record>)

    the records



69
70
71
72
73
74
# File 'lib/berkeley_library/alma/sru/sru.rb', line 69

def marc_records_for(query_value, max_records: DEFAULT_MAX_RECORDS, freeze: false)
  Enumerator.new do |y|
    uri = sru_query_uri(query_value, max_records: max_records)
    perform_query(uri, freeze: freeze) { |rec| y << rec }
  end.lazy
end

#sru_query_uri(sru_query_value, max_records: DEFAULT_MAX_RECORDS) ⇒ URI

Returns a URI for retrieving records for the specified query via SRU. Requires Config.alma_sru_base_uri to be set.

Parameters:

  • max_records (defaults to: DEFAULT_MAX_RECORDS)

    the number of records per SRU page

Returns:

  • (URI)

    the MARC URI



41
42
43
44
45
46
47
48
49
# File 'lib/berkeley_library/alma/sru/sru.rb', line 41

def sru_query_uri(sru_query_value, max_records: DEFAULT_MAX_RECORDS)
  query_string = URI.encode_www_form(
    'version' => '1.2',
    'operation' => 'searchRetrieve',
    'query' => sru_query_value
  )

  Util::URIs.append(Config.alma_sru_base_uri, '?', query_string, '&', "maximumRecords=#{max_records}")
end