Module: Commonmeta::Readers::BibtexReader

Included in:
MetadataUtils
Defined in:
lib/commonmeta/readers/bibtex_reader.rb

Instance Method Summary collapse

Instance Method Details

#read_bibtex(string: nil, **options) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/commonmeta/readers/bibtex_reader.rb', line 6

def read_bibtex(string: nil, **options)
  read_options = ActiveSupport::HashWithIndifferentAccess.new(options.except(:doi, :id, :url,
                                                                             :sandbox, :validate, :ra))

  meta = string.present? ? BibTeX.parse(string).first : OpenStruct.new

  bibtex_type = meta.try(:type).to_s
  type = Commonmeta::Utils::BIB_TO_CM_TRANSLATIONS.fetch(bibtex_type, 'Other')

  doi = meta.try(:doi).to_s.presence || options[:doi]

  contributors = Array(meta.try(:author)).map do |a|
    { 'type' => 'Person', 'contributorRoles' => ['Author'], 'givenName' => a.first, 'familyName' => a.last }.compact
  end

  container = if meta.try(:journal).present?
                first_page = meta.try(:pages).present? ? meta.try(:pages).split('-').map(&:strip)[0] : nil
                last_page = meta.try(:pages).present? ? meta.try(:pages).split('-').map(&:strip)[1] : nil

                { 'type' => 'Journal',
                  'title' => meta.journal.to_s,
                  'identifier' => meta.try(:issn).to_s.presence,
                  'identifierType' => meta.try(:issn).present? ? 'ISSN' : nil,
                  'volume' => meta.try(:volume).to_s.presence,
                  'firstPage' => first_page,
                  'lastPage' => last_page }.compact
              end

  state = meta.try(:doi).to_s.present? || read_options.present? ? 'findable' : 'not_found'

  date = {}
  date['published'] =
    meta.try(:date).present? && Date.edtf(meta.date.to_s).present? ? meta.date.to_s : nil?

  license = meta.try(:copyright).present? ? hsh_to_spdx('rightsURI' => meta[:copyright]) : nil

  { 'id' => normalize_doi(doi),
    'type' => type,
    'url' => meta.try(:url).to_s.presence,
    'titles' => meta.try(:title).present? ? [{ 'title' => meta.try(:title).to_s }] : [],
    'contributors' => contributors,
    'container' => container,
    'publisher' => meta.try(:publisher).to_s ? { 'name' => meta.publisher.to_s } : nil,
    'date' => date,
    'descriptions' => if meta.try(:abstract).present?
                        [{
                          'description' => meta.try(:abstract) && sanitize(meta.abstract.to_s).presence, 'descriptionType' => 'Abstract'
                        }]
                      else
                        []
                      end,
    'license' => license,
    'state' => state }.merge(read_options)
end