From a0d43bbbe95ddd302082796656979154b9eedd98 Mon Sep 17 00:00:00 2001 From: Nathan Fixler Date: Tue, 2 Mar 2010 12:56:16 -0500 Subject: [PATCH] Added the MARC leader regex from LOC without any fields defined (yet) --- lib/wcapi.rb | 2 +- lib/wcapi/record.rb | 8 +++- lib/wcapi/record/leader.rb | 15 +++++++ spec/wcapi/record/leader_spec.rb | 7 +++ spec/wcapi/record_spec.rb | 76 ++++++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 lib/wcapi/record/leader.rb create mode 100644 spec/wcapi/record/leader_spec.rb create mode 100644 spec/wcapi/record_spec.rb diff --git a/lib/wcapi.rb b/lib/wcapi.rb index 199bb80..2cf2633 100644 --- a/lib/wcapi.rb +++ b/lib/wcapi.rb @@ -1,4 +1,4 @@ -%w(response_parser rexml_parser hpricot_parser client open_search_response get_record_response get_location_response sru_search_response record record/isbn holding).each do |file| +%w(response_parser rexml_parser hpricot_parser client open_search_response get_record_response get_location_response sru_search_response record record/isbn record/leader holding).each do |file| require File.join(File.dirname(__FILE__), 'wcapi', file) end diff --git a/lib/wcapi/record.rb b/lib/wcapi/record.rb index db52c41..b56ebdc 100644 --- a/lib/wcapi/record.rb +++ b/lib/wcapi/record.rb @@ -6,11 +6,15 @@ class Record def initialize(doc = '') @doc = doc end - + def oclc_id @id ||= xpath_get_text(xpath_first(@doc, "controlfield[@tag='001']")) end - + + def leader + @leader ||= WCAPI::Record::Leader.new(xpath_get_text(xpath_first(@doc, "leader"))) + end + def isbns unless @isbns @isbns = [] diff --git a/lib/wcapi/record/leader.rb b/lib/wcapi/record/leader.rb new file mode 100644 index 0000000..b6d9029 --- /dev/null +++ b/lib/wcapi/record/leader.rb @@ -0,0 +1,15 @@ +module WCAPI + class Record + class Leader + def initialize(str) + @regex = str.match(/([\d]{5})([\dA-Za-z ]{1})([\dA-Za-z ]{1})([\dA-Za-z ]{3})(2| )(2| )([\d]{5})([\dA-Za-z ]{3})(4500| )/) || [] + end + + def raw + @regex[0] + end + + + end + end +end \ No newline at end of file diff --git a/spec/wcapi/record/leader_spec.rb b/spec/wcapi/record/leader_spec.rb new file mode 100644 index 0000000..60926bd --- /dev/null +++ b/spec/wcapi/record/leader_spec.rb @@ -0,0 +1,7 @@ +require File.dirname(__FILE__) + '/../../spec_helper' + +describe WCAPI::Record::Leader do + it "should make the raw value accessible" do + WCAPI::Record::Leader.new('01427cem 22003731 4500').raw.should == '01427cem 22003731 4500' + end +end \ No newline at end of file diff --git a/spec/wcapi/record_spec.rb b/spec/wcapi/record_spec.rb new file mode 100644 index 0000000..c4f765d --- /dev/null +++ b/spec/wcapi/record_spec.rb @@ -0,0 +1,76 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe WCAPI::Record do + before(:all) do + reader = XMLReader.new(File.dirname(__FILE__)+ '/../xml/get_record_response.xml') + @record = WCAPI::Record.new(reader.record) + end + + context "using convenience methods" do + it "#oclc_id" do + @record.oclc_id.should == '57358293' + end + + it "#leader" do + @record.leader.should be_an_instance_of(WCAPI::Record::Leader) + @record.leader.raw.should == '00000 a2200000 4500' + end + + it "#isbns" do + @record.isbns.should be_an_instance_of(Array) + @record.isbns.each do |isbn| + isbn.should be_an_instance_of(WCAPI::Record::ISBN) + end + end + + it "#isbns.to_s" do + @record.isbns.collect {|isbn| "#{isbn}"}.should == ["0439784549 (hardcover)", "9780439784542 (hardcover)", "0439786770 (reinforced lib. bdg.)", "9780439786775 (reinforced lib. bdg.)", "0439791324 (deluxe edition)", "9780439791328 (deluxe edition)", "0439785960 (pbk.)", "9780439785969 (pbk.)"] + end + + it "#isbns.to_i" do + @record.isbns.should be_an_instance_of(Array) + @record.isbns.collect {|isbn| "#{isbn.to_i}"}.should == ["0439784549", "9780439784542", "0439786770", "9780439786775", "0439791324", "9780439791328", "0439785960", "9780439785969"] + end + + it "#title" do + @record.title.should == ['Harry Potter and the Half-Blood Prince /'] + end + + it "#link" do + @record.link.should == 'http://www.worldcat.org/oclc/57358293' + end + + it "#authors" do + @record.authors.first.should eql("Rowling, J. K.") + + # There's some issues here with character encoding, so I'm ignoring this + # 700 field for now. + # @record.authors.last.should eql("GrandPré, Mary,") + end + + it "#summary" do + @record.summary.should == "Sixth-year Hogwarts student Harry Potter " \ + + "gains valuable insights into the boy Voldemort once was, even as " \ + + "his own world is transformed by maturing friendships, schoolwork " \ + + "assistance from an unexpected source, and devastating losses." + end + + it "#citation" do + @record.citation.should == '' + end + end +end + +class XMLReader + include WCAPI + include ResponseParser + + def initialize(path) + @path = path + end + + def record + doc = get_parser(File.read(@path)) + xpath_first(doc, "/record") + end +end \ No newline at end of file