Skip to content

Commit

Permalink
Added the MARC leader regex from LOC without any fields defined (yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
fixlr committed Mar 2, 2010
1 parent 77f0459 commit a0d43bb
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
2 changes: 1 addition & 1 deletion 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

Expand Down
8 changes: 6 additions & 2 deletions lib/wcapi/record.rb
Expand Up @@ -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 = []
Expand Down
15 changes: 15 additions & 0 deletions 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
7 changes: 7 additions & 0 deletions 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
76 changes: 76 additions & 0 deletions 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

0 comments on commit a0d43bb

Please sign in to comment.