Module: Commonmeta::Readers::RisReader

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

Instance Method Summary collapse

Instance Method Details

#read_ris(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
# File 'lib/commonmeta/readers/ris_reader.rb', line 6

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

  meta = ris_meta(string: string)

  ris_type = meta.fetch('TY', nil) || 'GEN'
  type = Commonmeta::Utils::RIS_TO_CM_TRANSLATIONS.fetch(ris_type, 'Other')

  id = normalize_doi(options[:doi] || meta.fetch('DO', nil))

  author = Array.wrap(meta.fetch('AU', nil)).map { |a| { 'name' => a } }
  date_parts = meta.fetch('PY', nil).to_s.split('/')
  created_date_parts = meta.fetch('Y1', nil).to_s.split('/')
  date = {}
  date['published'] = get_date_from_parts(*date_parts) if meta.fetch('PY', nil).present?
  date['created'] = get_date_from_parts(*created_date_parts) if meta.fetch('Y1', nil).present?

  container = if meta.fetch('T2', nil).present?
                { 'type' => 'Journal',
                  'title' => meta.fetch('T2', nil),
                  'identifier' => meta.fetch('SN', nil),
                  'volume' => meta.fetch('VL', nil),
                  'issue' => meta.fetch('IS', nil),
                  'firstPage' => meta.fetch('SP', nil),
                  'lastPage' => meta.fetch('EP', nil) }.compact
              end
  state = meta.fetch('DO', nil).present? || read_options.present? ? 'findable' : 'not_found'
  subjects = Array.wrap(meta.fetch('KW', nil)).reduce([]) do |sum, subject|
    sum += name_to_fos(subject)

    sum
  end

  { 'id' => id,
    'type' => type,
    'url' => meta.fetch('UR', nil),
    'titles' => meta.fetch('T1', nil).present? ? [{ 'title' => meta.fetch('T1', nil) }] : nil,
    'creators' => get_authors(author),
    'publisher' => { 'name' => meta.fetch('PB', '(:unav)') },
    'container' => container,
    'date' => date,
    'descriptions' => if meta.fetch('AB', nil).present?
                        [{ 'description' => sanitize(meta.fetch('AB')),
                           'descriptionType' => 'Abstract' }]
                      end,
    'subjects' => subjects,
    'language' => meta.fetch('LA', nil),
    'state' => state }.compact.merge(read_options)
end

#ris_meta(string: nil) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/commonmeta/readers/ris_reader.rb', line 57

def ris_meta(string: nil)
  h = Hash.new { |h, k| h[k] = [] }
  string.split("\n").each_with_object(h) do |line, _sum|
    k, v = line.split('-', 2)
    h[k.strip] << v.to_s.strip
  end.transform_values(&:unwrap).compact
end