Skip to content

Commit

Permalink
Add catalog service
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Nov 24, 2016
1 parent 124a92a commit 1f7190f
Show file tree
Hide file tree
Showing 6 changed files with 5,813 additions and 0 deletions.
25 changes: 25 additions & 0 deletions app/controllers/marcxml_controller.rb
@@ -0,0 +1,25 @@
class MarcxmlController < ApplicationController #:nodoc:
before_action :set_marcxml_resource

def catkey
render plain: @marcxml.catkey
end

def marcxml
render xml: @marcxml.marcxml
end

def mods
render xml: @marcxml.mods
end

private

def set_marcxml_resource
@marcxml = MarcxmlResource.find_by(**marcxml_resource_params)
end

def marcxml_resource_params
params.slice(:barcode, :catkey).to_unsafe_h.symbolize_keys
end
end
32 changes: 32 additions & 0 deletions app/models/marcxml_resource.rb
@@ -0,0 +1,32 @@
# MARC resource model for retrieving and transforming MARC records
class MarcxmlResource
MARC_TO_MODS_XSLT = Nokogiri::XSLT(File.read(File.join(Rails.root, 'app', 'xslt', 'MARC21slim2MODS3-6.xsl')))

def self.find_by(catkey: nil, barcode: nil)
if catkey
new(catkey: catkey)
elsif barcode
solr = RSolr.connect(url: Settings.CATALOG.SOLR_URL)
response = solr.get('barcode', params: { n: barcode })
catkey = response[:response][:docs].first[:id]

new(catkey: catkey)
else
raise ArgumentError, 'Must supply either a catkey or barcode'
end
end

attr_reader :catkey

def initialize(catkey:)
@catkey = catkey
end

def mods
MARC_TO_MODS_XSLT.transform(Nokogiri::XML(marcxml)).to_xml
end

def marcxml
Faraday.get(Settings.CATALOG.MARCXML_URL % { catkey: catkey }).body
end
end

0 comments on commit 1f7190f

Please sign in to comment.