Class: BerkeleyLibrary::Alma::SRU::XMLReader

Inherits:
Object
  • Object
show all
Includes:
Util::Files, Enumerable, MARC::NokogiriReader
Defined in:
lib/berkeley_library/alma/sru/xml_reader.rb

Overview

A customized XML reader for reading MARC records from SRU search results.

Constant Summary collapse

NS_SRW =

Constants

'http://www.loc.gov/zing/srw/'.freeze
NS_MARC =
'http://www.loc.gov/MARC21/slim'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, freeze: false) ⇒ XMLReader

Initializer



53
54
55
56
57
# File 'lib/berkeley_library/alma/sru/xml_reader.rb', line 53

def initialize(source, freeze: false)
  @handle = ensure_io(source)
  @freeze = freeze
  init
end

Instance Attribute Details

#last_record_idInteger? (readonly)

Returns the record identifier of the most recently parsed record, if any.

Returns:

  • (Integer, nil)

    the record identifier of the most recently parsed record, if any



24
25
26
# File 'lib/berkeley_library/alma/sru/xml_reader.rb', line 24

def last_record_id
  @last_record_id
end

#last_record_positionInteger? (readonly)

Returns the record position of the most recently parsed record, if any.

Returns:

  • (Integer, nil)

    the record position of the most recently parsed record, if any



27
28
29
# File 'lib/berkeley_library/alma/sru/xml_reader.rb', line 27

def last_record_position
  @last_record_position
end

#next_record_positionInteger? (readonly)

Returns the next record position, if present.

Returns:

  • (Integer, nil)

    the next record position, if present



30
31
32
# File 'lib/berkeley_library/alma/sru/xml_reader.rb', line 30

def next_record_position
  @next_record_position
end

Class Method Details

.read(source, freeze: false) ⇒ Object

Reads MARC records from an XML datasource given either as an XML string, a file path, or as an IO object.

Parameters:

  • source (String, Pathname, IO)

    an XML string, the path to a file, or an IO to read from directly

  • freeze (Boolean) (defaults to: false)

    whether to freeze each record after reading



65
66
67
# File 'lib/berkeley_library/alma/sru/xml_reader.rb', line 65

def read(source, freeze: false)
  new(source, freeze: freeze)
end

Instance Method Details

#characters(string) ⇒ Object

rubocop:disable Metrics/MethodLength

See Also:

  • Nokogiri::XML::Sax::Document#characters


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/berkeley_library/alma/sru/xml_reader.rb', line 112

def characters(string)
  return super unless NS_SRW == @current_element_ns
  return unless (name = @current_element_name)

  case name
  when 'numberOfRecords'
    @num_records = string
  when 'recordIdentifier'
    @last_record_id = string
  when 'recordPosition'
    @last_record_position = string.to_i
  when 'nextRecordPosition'
    @next_record_position = string.to_i
  end
end

#end_element_namespace(name, prefix = nil, uri = nil) ⇒ Object

See Also:

  • Nokogiri::XML::Sax::Document#end_element_namespace


97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/berkeley_library/alma/sru/xml_reader.rb', line 97

def end_element_namespace(name, prefix = nil, uri = nil)
  # Delay yielding record till we reach the end of the outer SRU <record/>
  # element (not the inner MARC <record/> element), so we can record the
  # values of <recordIdentifier> and <recordPosition/>
  if name.downcase == 'record'
    yield_record if uri == NS_SRW
  elsif uri == NS_MARC
    super
  end

  @current_element_name = nil
end

#num_recordsInteger?

Returns the total number of records, based on the <numberOfRecords/> tag returned in the SRU response.

Note that the total is not guaranteed to be present, and if present, may not be present unless at least some records have been parsed.

Returns:

  • (Integer, nil)

    the total number of records, or nil if the total has not been read yet



39
40
41
# File 'lib/berkeley_library/alma/sru/xml_reader.rb', line 39

def num_records
  @num_records&.to_i
end

#records_yieldedInteger

Returns the number of records yielded.

Returns:

  • (Integer)

    the number of records yielded.



46
47
48
# File 'lib/berkeley_library/alma/sru/xml_reader.rb', line 46

def records_yielded
  @records_yielded ||= 0
end

#start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = []) ⇒ Object

rubocop:disable Metrics/ParameterLists

See Also:

  • Nokogiri::XML::Sax::Document#start_element_namespace


88
89
90
91
92
93
# File 'lib/berkeley_library/alma/sru/xml_reader.rb', line 88

def start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = [])
  super

  @current_element_ns = uri
  @current_element_name = name
end

#yield_recordObject

MARC::GenericPullParser overrides



73
74
75
76
77
78
79
80
81
# File 'lib/berkeley_library/alma/sru/xml_reader.rb', line 73

def yield_record
  @record[:record].tap do |record|
    record.freeze if @freeze
  end

  super
ensure
  increment_records_yielded!
end