Skip to content

Commit

Permalink
MarcxmlResource validates marc record
Browse files Browse the repository at this point in the history
  • Loading branch information
ndushay committed Aug 31, 2019
1 parent 898c00a commit b8353a4
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Expand Up @@ -294,6 +294,7 @@ Style/FormatString:
- 'config/initializers/okcomputer.rb'
- 'spec/controllers/marcxml_controller_spec.rb'
- 'spec/requests/metadata_refresh_spec.rb'
- 'spec/models/marcxml_resource_spec.rb'
- 'spec/models/symphony_reader_spec.rb'

# Offense count: 1
Expand Down
25 changes: 24 additions & 1 deletion app/models/marcxml_resource.rb
Expand Up @@ -2,6 +2,8 @@

# MARC resource model for retrieving and transforming MARC records
class MarcxmlResource
class InvalidMarcError < RuntimeError; end

def self.find_by(catkey: nil, barcode: nil)
if catkey
new(catkey: catkey)
Expand Down Expand Up @@ -37,6 +39,27 @@ def marc_to_mods_xslt
end

def marc_record
SymphonyReader.new(catkey: catkey).to_marc
mr = SymphonyReader.new(catkey: catkey).to_marc
mr.fields.freeze
validate_marc_record(mr)
end

def validate_marc_record(marc_rec)
err_prefix = "MARC record #{catkey} from Symphony should have exactly one populated"
raise InvalidMarcError, "#{err_prefix} leader" if marc_rec.leader.blank?

cf001s = marc_rec.fields('001')
raise InvalidMarcError, "#{err_prefix} 001" if cf001s.length != 1 || cf001s.first.value.blank?

cf008s = marc_rec.fields('008')
raise InvalidMarcError, "#{err_prefix} 008" if cf008s.length != 1 || cf008s.first.value.blank?

df245s = marc_rec.fields('245')
raise InvalidMarcError, "#{err_prefix} 245" if df245s.length != 1

sub_as = df245s[0].find_all { |subfield| subfield.code == 'a' }
raise InvalidMarcError, "#{err_prefix} 245 subfield a" if sub_as.length != 1 || sub_as.first.value.blank?

marc_rec
end
end
99 changes: 99 additions & 0 deletions spec/models/marcxml_resource_spec.rb
@@ -0,0 +1,99 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe MarcxmlResource do
subject(:marcxml_rsrc) { described_class.new(catkey: catkey) }

let(:catkey) { 'catkey' }
let(:body) do
{
resource: '/catalog/bib',
key: catkey,
fields: {
bib: {
standard: 'MARC21',
type: 'BIB',
leader: '00956cem 2200229Ma 4500',
fields: [
{ tag: '008', subfields: [{ code: '_', data: '041202s2000 ja nnn s f eng d' }] },
{
tag: '245',
inds: '41',
subfields: [{ code: 'a', data: 'the title' }]
}
]
}
}
}
end

describe '#marc_record' do
let(:err_prefix) { 'MARC record catkey from Symphony should have exactly one populated' }

context 'when missing leader' do
before do
body_len = 268
my_body = Marshal.load(Marshal.dump(body))
my_body[:fields][:bib].delete(:leader)
stub_request(:get, Settings.catalog.symphony.json_url % { catkey: catkey }).to_return(body: my_body.to_json, headers: { 'Content-Length': body_len })
end

it 'raises InvalidMarcError' do
expect { marcxml_rsrc.send(:marc_record) }.to raise_error(MarcxmlResource::InvalidMarcError, "#{err_prefix} leader")
end
end

context 'when missing 008' do
before do
body_len = 212
my_body = Marshal.load(Marshal.dump(body))
my_body[:fields][:bib][:fields].delete_at(0)
stub_request(:get, Settings.catalog.symphony.json_url % { catkey: catkey }).to_return(body: my_body.to_json, headers: { 'Content-Length': body_len })
end

it 'raises InvalidMarcError' do
expect { marcxml_rsrc.send(:marc_record) }.to raise_error(MarcxmlResource::InvalidMarcError, "#{err_prefix} 008")
end
end

context 'when missing 245' do
before do
body_len = 231
my_body = Marshal.load(Marshal.dump(body))
my_body[:fields][:bib][:fields].delete_at(1)
stub_request(:get, Settings.catalog.symphony.json_url % { catkey: catkey }).to_return(body: my_body.to_json, headers: { 'Content-Length': body_len })
end

it 'raises InvalidMarcError' do
expect { marcxml_rsrc.send(:marc_record) }.to raise_error(MarcxmlResource::InvalidMarcError, "#{err_prefix} 245")
end
end

context 'when missing 245 subfield a' do
before do
body_len = 303
my_body = Marshal.load(Marshal.dump(body))
my_body[:fields][:bib][:fields][1][:subfields][0][:code] = 'b'
stub_request(:get, Settings.catalog.symphony.json_url % { catkey: catkey }).to_return(body: my_body.to_json, headers: { 'Content-Length': body_len })
end

it 'raises InvalidMarcError' do
expect { marcxml_rsrc.send(:marc_record) }.to raise_error(MarcxmlResource::InvalidMarcError, "#{err_prefix} 245 subfield a")
end
end

context 'when empty 245 subfield a' do
before do
body_len = 294
my_body = Marshal.load(Marshal.dump(body))
my_body[:fields][:bib][:fields][1][:subfields][0][:data] = ''
stub_request(:get, Settings.catalog.symphony.json_url % { catkey: catkey }).to_return(body: my_body.to_json, headers: { 'Content-Length': body_len })
end

it 'raises InvalidMarcError' do
expect { marcxml_rsrc.send(:marc_record) }.to raise_error(MarcxmlResource::InvalidMarcError, "#{err_prefix} 245 subfield a")
end
end
end
end

0 comments on commit b8353a4

Please sign in to comment.