Class: BerkeleyLibrary::TIND::MARC::XMLReader
- Inherits:
-
Object
- Object
- BerkeleyLibrary::TIND::MARC::XMLReader
- Extended by:
- MARCExtensions::XMLReaderClassExtensions
- Includes:
- Enumerable, MARC::NokogiriReader
- Defined in:
- lib/berkeley_library/tind/marc/xml_reader.rb
Overview
A customized XML reader for reading MARC records from TIND search results.
Constant Summary collapse
- COMMENT_TOTAL_RE =
Constant
/Search-Engine-Total-Number-Of-Results: ([0-9]+)/.freeze
Instance Attribute Summary collapse
-
#search_id ⇒ Object
readonly
###################################################### Attributes.
Instance Method Summary collapse
- #characters(string) ⇒ Object
- #comment(string) ⇒ Object
- #end_element_namespace(name, prefix = nil, uri = nil) ⇒ Object
-
#initialize(source, freeze: false) ⇒ XMLReader
constructor
Reads MARC records from an XML datasource given either as a file path, or as an IO object.
-
#records_yielded ⇒ Integer
Returns the number of records yielded.
-
#start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = []) ⇒ Object
rubocop:disable Metrics/ParameterLists.
-
#total ⇒ Integer?
Returns the total number of records, based on the
<total/>tag returned by the TIND Search API, or the special commentSearch-Engine-Total-Number-Of-Resultsreturned by TIND Regular Search in XML format. -
#yield_record ⇒ Object
###################################################### MARC::GenericPullParser overrides.
Constructor Details
#initialize(source, freeze: false) ⇒ XMLReader
Reads MARC records from an XML datasource given either as a file path, or as an IO object.
51 52 53 54 55 |
# File 'lib/berkeley_library/tind/marc/xml_reader.rb', line 51 def initialize(source, freeze: false) @handle = ensure_io(source) @freeze = freeze init end |
Instance Attribute Details
#search_id ⇒ Object (readonly)
Attributes
21 22 23 |
# File 'lib/berkeley_library/tind/marc/xml_reader.rb', line 21 def search_id @search_id end |
Instance Method Details
#characters(string) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/berkeley_library/tind/marc/xml_reader.rb', line 91 def characters(string) return unless (name = @current_element_name) case name when 'search_id' @search_id = string when 'total' @total = string.to_i else super end end |
#comment(string) ⇒ Object
105 106 107 108 109 |
# File 'lib/berkeley_library/tind/marc/xml_reader.rb', line 105 def comment(string) return unless (md = COMMENT_TOTAL_RE.match(string)) @total = md[1].to_i end |
#end_element_namespace(name, prefix = nil, uri = nil) ⇒ Object
84 85 86 87 88 |
# File 'lib/berkeley_library/tind/marc/xml_reader.rb', line 84 def end_element_namespace(name, prefix = nil, uri = nil) super @current_element_name = nil end |
#records_yielded ⇒ Integer
Returns the number of records yielded.
39 40 41 |
# File 'lib/berkeley_library/tind/marc/xml_reader.rb', line 39 def records_yielded @records_yielded ||= 0 end |
#start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = []) ⇒ Object
rubocop:disable Metrics/ParameterLists
76 77 78 79 80 |
# File 'lib/berkeley_library/tind/marc/xml_reader.rb', line 76 def start_element_namespace(name, attrs = [], prefix = nil, uri = nil, ns = []) super @current_element_name = name end |
#total ⇒ Integer?
Returns the total number of records, based on the <total/> tag
returned by the TIND Search API, or the special comment
Search-Engine-Total-Number-Of-Results returned by TIND
Regular Search in XML format.
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.
32 33 34 |
# File 'lib/berkeley_library/tind/marc/xml_reader.rb', line 32 def total @total&.to_i end |
#yield_record ⇒ Object
MARC::GenericPullParser overrides
64 65 66 67 68 69 |
# File 'lib/berkeley_library/tind/marc/xml_reader.rb', line 64 def yield_record @record[:record].freeze if @freeze super ensure increment_records_yielded! end |