• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pulibrary / orangelight / 00dbc10b-d747-4ad7-b211-7b26d753abbb

14 Aug 2025 01:25PM UTC coverage: 0.483% (-94.9%) from 95.343%
00dbc10b-d747-4ad7-b211-7b26d753abbb

push

circleci

web-flow
Merge pull request #5181 from pulibrary/dependabot/bundler/activestorage-7.2.2.2

Bump activestorage from 7.2.2.1 to 7.2.2.2

47 of 9721 relevant lines covered (0.48%)

0.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/app/models/concerns/blacklight/marc/document_extension.rb
1
# frozen_string_literal: true
2

3
require 'marc'
×
4
require 'openurl/context_object'
×
5

6
# rubocop:disable Metrics/ModuleLength
7
module Blacklight
×
8
  module Marc
×
9
    module DocumentExtension
×
10
      include Blacklight::Marc::DocumentExport
×
11
      include OpenURL
×
12

13
      # Prepend our overloaded method to bypass bug in Blacklight
14
      # See https://stackoverflow.com/questions/5944278/overriding-method-by-another-defined-in-module
15
      prepend Blacklight::Marc::Document::MarcExportOverride
×
16

17
      class UnsupportedMarcFormatType < RuntimeError; end
×
18

19
      def self.extended(document)
×
20
        Blacklight::Solr::Document::MarcExport.register_export_formats(document)
×
21
      end
×
22

23
      # Accesses the MARC::Record constructed from data retrieved over the HTTP
24
      # @return [MARC::Record]
25
      def to_marc
×
26
        @_ruby_marc_obj ||= load_marc
×
27
      end
×
28

29
      # These are registered by default
30
      # @see Blacklight::Solr::Document::MarcExport.register_export_formats
31

32
      # Generate the string-serialized XML from the remote MARC record
33
      # @see Blacklight::Solr::Document::MarcExport#export_as_marcxml
34
      # @return [String]
35
      def export_as_marcxml
×
36
        return '' unless to_marc
×
37
        super
×
38
      end
×
39

40
      # @see Blacklight::Solr::Document::MarcExport#export_as_marc
41
      # @return [String]
42
      def export_as_marc
×
43
        return '' unless to_marc
×
44
        super
×
45
      end
×
46

47
      # @see Blacklight::Solr::Document::MarcExport#export_as_openurl_ctx_kev
48
      # @return [String]
49
      def export_as_openurl_ctx_kev(format = nil)
×
50
        ctx = to_ctx(format)
×
51
        # send back the encoded string
52
        ctx.kev
×
53
      end
×
54

55
      # Generate the refworks citation format from the remote MARC record
56
      # @see Blacklight::Solr::Document::MarcExport#export_as_refworks_marc_txt
57
      # @return [String]
58
      def export_as_refworks_marc_txt
×
59
        return '' unless to_marc
×
60
        super
×
61
      end
×
62

63
      # These are not registered by default, but still provided as public methods
64

65
      # @see Blacklight::Solr::Document::MarcExport#export_as_apa_citation_txt
66
      # @return [String]
67
      def export_as_apa_citation_txt
×
68
        return '' unless to_marc
×
69
        super
×
70
      end
×
71

72
      # @see Blacklight::Solr::Document::MarcExport#export_as_mla_citation_txt
73
      # @return [String]
74
      def export_as_mla_citation_txt
×
75
        return '' unless to_marc
×
76
        super
×
77
      end
×
78

79
      # @see Blacklight::Solr::Document::MarcExport#export_as_chicago_citation_txt
80
      # @return [String]
81
      def export_as_chicago_citation_txt
×
82
        return '' unless to_marc
×
83
        super
×
84
      end
×
85

86
      # @see Blacklight::Solr::Document::MarcExport#export_as_endnote
87
      # @return [String]
88
      def export_as_endnote
×
89
        return '' unless to_marc
×
90
        super
×
91
      end
×
92

93
      # return openurl ctx object
94
      def to_ctx(format = nil)
×
95
        @_ctx || build_ctx(format)
×
96
      end
×
97

98
      # We allow the user to retry in very specific scenarios.
99
      def can_retry?
×
100
        @can_retry
×
101
      end
×
102

103
        protected
×
104

105
          def build_ctx(format = nil)
×
106
            format ||= first('format')&.downcase
×
107
            unless format.nil?
×
108
              format = format.is_a?(Array) ? format[0].downcase.strip : format.downcase.strip
×
109
            end
×
110
            if format == 'book'
×
111
              BookCtxBuilder.new(document: self, format:).build
×
112
            elsif /journal/i.match?(format) # checking using include because institutions may use formats like Journal or Journal/Magazine
×
113
              JournalCtxBuilder.new(document: self, format:).build
×
114
            else
×
115
              CtxBuilder.new(document: self, format:).build
×
116
            end
×
117
          end
×
118

119
          # Retrieves the bib. ID from the Solr Document
120
          # @return [String]
121
          def marc_source
×
122
            @_marc_source ||= fetch(_marc_source_field)
×
123
          end
×
124

125
          # Retrieve the MARC 21 bitstream over the HTTP
126
          # @return [MARC::Record]
127
          def marc_record_from_marc21
×
128
            return if marc_source.blank?
×
129
            MARC::Record.new_from_marc marc_source
×
130
          end
×
131

132
          # Retrieve the MARC JSON over the HTTP
133
          # @return [MARC::Record]
134
          def marc_record_from_json
×
135
            return if marc_source.blank?
×
136

137
            begin
×
138
              marc_json = JSON.parse(marc_source)
×
139
            rescue JSON::ParserError => json_error
×
140
              Rails.logger.error "#{self.class}: Failed to parse the MARC JSON: #{json_error}"
×
141
              return
×
142
            end
×
143
            MARC::Record.new_from_hash marc_json
×
144
          end
×
145

146
          # Construct a MARC::Record using MARC record data retrieved over the HTTP
147
          # @return [MARC::Record]
148
          def load_marc
×
149
            marc_format = _marc_format_type.to_s
×
150

151
            case marc_format
×
152
            when 'marcxml'
×
153
              marc_record_from_marcxml
×
154
            when 'marc21'
×
155
              marc_record_from_marc21
×
156
            when 'json'
×
157
              marc_record_from_json
×
158
            else
×
159
              raise UnsupportedMarcFormatType, "Only marcxml, marc21, and json are supported, this documents format is #{_marc_format_type} and the current extension parameters are #{self.class.extension_parameters.inspect}"
×
160
            end
×
161
          rescue StandardError => e
×
162
            Rails.logger.error("Blacklight failed to parse MARC record. Exception was: #{e}")
×
163
            nil
×
164
          end
×
165

166
          # Construct a MARC::Record using MARC-XML data retrieved over the HTTP
167
          # @return [MARC::Record]
168
          def marc_record_from_marcxml
×
169
            id = fetch(_marc_source_field)
×
170

171
            response = Faraday.get("#{Requests.config['bibdata_base']}/bibliographic/#{id}")
×
172
            @can_retry = response.status == 429
×
173
            response_stream = StringIO.new(response.body)
×
174
            marc_reader = ::MARC::XMLReader.new(response_stream)
×
175
            marc_records = marc_reader.to_a
×
176
            marc_records.first
×
177
          end
×
178

179
          def _marc_helper
×
180
            @_marc_helper ||= Blacklight::Marc::Document.new fetch(_marc_source_field), _marc_format_type
×
181
          end
×
182

183
          def _marc_source_field
×
184
            self.class.extension_parameters[:marc_source_field]
×
185
          end
×
186

187
          def _marc_format_type
×
188
            # TODO: Raise if not present
189
            self.class.extension_parameters[:marc_format_type]
×
190
          end
×
191

192
          # Overwites the get_author_list(record) method from the module Blacklight::Solr::Document::MarcExport
193
          def get_author_list(record)
×
194
            author_list = []
×
195
            authors_primary = record.find { |f| f.tag == '100' }
×
196
            begin
×
197
              author_primary = authors_primary.find { |s| s.code == 'a' }.value unless authors_primary.nil?
×
198
            rescue StandardError
×
199
              ''
×
200
            end
×
201
            author_list.push(clean_end_punctuation(author_primary)) unless author_primary.nil?
×
202
            authors_secondary = record.find_all { |f| f.tag == '700' }
×
203
            authors_secondary&.each do |l|
×
204
              unless l.find { |s| s.code == 'a' }.nil?
×
205
                author_list.push(clean_end_punctuation(l.find { |s| s.code == 'a' }.value)) unless l.find { |s| s.code == 'a' }.value.nil?
×
206
              end
×
207
            end
×
208
            author_list.uniq!
×
209
            author_list
×
210
          end
×
211
    end
×
212
    # rubocop:enable Metrics/ModuleLength
213
  end
×
214
end
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc