Class: RisReader
- Inherits:
-
Object
- Object
- RisReader
- Defined in:
- lib/ris_reader.rb
Overview
Several utilities to work with PubMed API
Instance Attribute Summary collapse
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
-
#initialize(string) ⇒ RisReader
constructor
A new instance of RisReader.
- #process ⇒ Object
Constructor Details
#initialize(string) ⇒ RisReader
Returns a new instance of RisReader.
32 33 34 35 36 37 38 39 40 |
# File 'lib/ris_reader.rb', line 32 def initialize(string) @string=string @errors=[] if @string.length>3 and @string.bytes[0..2]==[239,187,191] @string=@string[1..-1] end @records=[] end |
Instance Attribute Details
#records ⇒ Object (readonly)
Returns the value of attribute records.
31 32 33 |
# File 'lib/ris_reader.rb', line 31 def records @records end |
Instance Method Details
#process ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ris_reader.rb', line 43 def process current_record={} ris_pattern = /^(?<tag>[A-Z][A-Z0-9])\s{0,2}-\s{0,2}(?<value>.*)$/ # WoS RIS is savage. Both \r and \n are used. # No point in be civilized lines=@string.split(/[\r\n]+/) i=0 @string.each_line do |line| line.strip! next if line=="" #puts line.inspect # Match the pattern against the RIS line match = line.match(ris_pattern) # Access the tag and value using named capture groups if match tag = match[:tag] value = match[:value].strip if tag=="ER" @records.push(current_record.dup) current_record={} else if current_record.has_key? tag if current_record[tag].is_a? Array current_record[tag].push(value) else current_record[tag]=[current_record[tag], value] end else current_record[tag]=value end end elsif line=~/(^.+):(.+)$/ current_record[$1]=$2.strip else #puts "****" #hex_codes = line.bytes.map { |b| sprintf("%02X", b) }.join #puts hex_codes #puts match.inspect #puts line.inspect #raise "ERROR" @errors.push({line_number:i, text:line}) end i+=1 end end |