Class: BerkeleyLibrary::Alma::SRU::XMLReader
- Inherits:
-
Object
- Object
- BerkeleyLibrary::Alma::SRU::XMLReader
- 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
-
#last_record_id ⇒ Integer?
readonly
The record identifier of the most recently parsed record, if any.
-
#last_record_position ⇒ Integer?
readonly
The record position of the most recently parsed record, if any.
-
#next_record_position ⇒ Integer?
readonly
The next record position, if present.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#characters(string) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #end_element_namespace(name, prefix = nil, uri = nil) ⇒ Object
-
#initialize(source, freeze: false) ⇒ XMLReader
constructor
###################################################### Initializer.
-
#num_records ⇒ Integer?
Returns the total number of records, based on the
<numberOfRecords/>
tag returned in the SRU response. -
#records_yielded ⇒ Integer
Returns the number of records yielded.
-
#start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = []) ⇒ Object
rubocop:disable Metrics/ParameterLists.
-
#yield_record ⇒ Object
###################################################### MARC::GenericPullParser overrides.
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_id ⇒ Integer? (readonly)
Returns 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_position ⇒ Integer? (readonly)
Returns 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_position ⇒ Integer? (readonly)
Returns 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.
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
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
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_records ⇒ Integer?
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.
39 40 41 |
# File 'lib/berkeley_library/alma/sru/xml_reader.rb', line 39 def num_records @num_records&.to_i end |
#records_yielded ⇒ Integer
Returns 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
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_record ⇒ Object
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 |