Skip to content

Commit

Permalink
Add bib import to ingest API
Browse files Browse the repository at this point in the history
  • Loading branch information
bkeese committed Dec 12, 2015
1 parent 9cdc216 commit f4ed293
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
18 changes: 16 additions & 2 deletions app/controllers/media_objects_controller.rb
Expand Up @@ -87,10 +87,24 @@ def update_mediaobject

@mediaobject.collection = collection
@mediaobject.avalon_uploader = 'REST API'
@mediaobject.update_datastream(:descMetadata, params[:fields]) if params.has_key?(:fields) and params[:fields].respond_to?(:has_key?)

populate_from_catalog = !!params[:import_bib_record]
if populate_from_catalog and Avalon::BibRetriever.configured?
begin
@mediaobject.descMetadata.populate_from_catalog!(Array(params[:fields][:bibliographic_id]).first,
Array(params[:fields][:bibliographic_id_label]).first)
rescue Exception => e
render json: {errors: ['Bib import failed', e.message]}, status: 422
return
end
else
@mediaobject.update_datastream(:descMetadata, params[:fields]) if params.has_key?(:fields) and params[:fields].respond_to?(:has_key?)
end

error_messages = []

if !@mediaobject.save
error_messages += ['Failed to create media object:']+@mediaobject.errors.full_messages
error_messages += ['Failed to create media object:']+@mediaobject.errors.full_messages
elsif params[:files].respond_to?('each')
params[:files].each do |file_spec|
master_file = MasterFile.new(file_spec.except :structure)
Expand Down
13 changes: 13 additions & 0 deletions spec/controllers/media_objects_controller_spec.rb
Expand Up @@ -27,6 +27,9 @@
let!(:filename) {'videoshort.high.mp4'}
let!(:absolute_location) {File.join(testdir, filename)}
let!(:structure) {File.read(File.join(testdir, 'structure.xml'))}
let!(:bib_id) { '7763100' }
let!(:sru_url) { "http://zgate.example.edu:9000/db?version=1.1&operation=searchRetrieve&maximumRecords=1&recordSchema=marcxml&query=rec.id=#{bib_id}" }
let!(:sru_response) { File.read(File.expand_path("../../fixtures/#{bib_id}.xml",__FILE__)) }
let!(:masterfile) {{
file_location: absolute_location,
label: "Part 1",
Expand Down Expand Up @@ -97,6 +100,16 @@
expect(new_media_object.date_issued).to eq media_object.date_issued
expect(new_media_object.parts_with_order).to eq new_media_object.parts
end
it "should create a new mediaobject with successful bib import" do
Avalon::Configuration['bib_retriever'] = { 'protocol' => 'sru', 'url' => 'http://zgate.example.edu:9000/db' }
FakeWeb.register_uri :get, sru_url, body: sru_response
fields = { bibliographic_id: bib_id }
post 'create', import_bib_record: true, fields: fields, files: [masterfile], collection_id: collection.pid, api_key:'secret_token'
expect(response.status).to eq(200)
new_media_object = MediaObject.find(JSON.parse(response.body)['id'])
expect(new_media_object.bibliographic_id).to eq(['local', bib_id])
expect(new_media_object.title).to eq('245 A : B F G K N P S')
end
end
describe "#update" do
let!(:media_object) { FactoryGirl.create(:media_object_with_master_file) }
Expand Down

0 comments on commit f4ed293

Please sign in to comment.