From 3608fc82d368a137950819f6603303e1816b1de5 Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Wed, 17 Jul 2019 22:43:16 +0200 Subject: [PATCH] handle base-encoding errors. #315 --- app/models/doi.rb | 10 ++++++++-- app/serializers/doi_serializer.rb | 6 +++++- spec/requests/dois_spec.rb | 5 ++--- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/models/doi.rb b/app/models/doi.rb index 6ff6072f5..24bca9259 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -459,7 +459,7 @@ def self.import_by_ids(options={}) # get every id between from_id and end_id (from_id..until_id).step(500).each do |id| - DoiImportByIdJob.perform_later(id: id) + DoiImportByIdJob.perform_later(options.merge(id: id)) puts "Queued importing for DOIs with IDs starting with #{id}." end @@ -470,7 +470,13 @@ def self.import_by_id(options={}) return nil unless options[:id].present? id = options[:id].to_i - index = Rails.env.test? ? "dois-test" : self.inactive_index + index = if Rails.env.test? + "dois-test" + elsif options[:index].present? + options[:index] + else + self.inactive_index + end errors = 0 count = 0 diff --git a/app/serializers/doi_serializer.rb b/app/serializers/doi_serializer.rb index db6c1b7a4..cfe6dc254 100644 --- a/app/serializers/doi_serializer.rb +++ b/app/serializers/doi_serializer.rb @@ -12,7 +12,11 @@ class DoiSerializer has_many :media, if: Proc.new { |object, params| params && params[:detail] } attribute :xml, if: Proc.new { |object, params| params && params[:detail] } do |object| - xml_encoded + begin + Base64.strict_encode64(object.xml) if object.xml.present? + rescue ArgumentError + nil + end end attribute :doi do |object| diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index 101124532..cda0e6b00 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -35,10 +35,9 @@ expect(last_response.status).to eq(200) expect(json['data'].size).to eq(3) - json['data'].each{ - |doi| + json['data'].each do |doi| expect(doi.dig('attributes')).to include('xml') - } + end end end