Class: Asciidoctor::Foodogsquared::Extensions::MusicBrainzInlineMacro

Inherits:
Extensions::InlineMacroProcessor
  • Object
show all
Defined in:
lib/asciidoctor/foodogsquared/extensions/musicbrainz-inline-macro.rb

Instance Method Summary collapse

Instance Method Details

#process(parent, target, attrs) ⇒ Object



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
# File 'lib/asciidoctor/foodogsquared/extensions/musicbrainz-inline-macro.rb', line 15

def process(parent, target, attrs)
  doc = parent.document
  root_endpoint = 'https://musicbrainz.org/ws/2'

  begin
    headers = {
      'Accept' => 'application/json',
      'User-Agent' => ::Asciidoctor::Foodogsquared::USER_AGENT
    }

    uri = %(#{root_endpoint}/#{attrs['type']}/#{target})

     = OpenURI.open_uri(uri, headers) { |f| JSON.parse(f.read) }
    attrs['caption'] ||= case attrs['type']
                         when 'artist', 'area', 'events', 'genre', 'instrument', 'label', 'place', 'series'
                           ['name']
                         when 'recording', 'release-group', 'release', 'cdstub', 'work'
                           ['title']
                         when 'url'
                           ['resource']
                         end

    target = %(https://musicbrainz.org/#{attrs['type']}/#{target})
    doc.register :links, target
    create_anchor parent, attrs['caption'], type: :link, target: target
  rescue StandardError
    warning = %(error while getting Musicbrainz database object '#{target}: #{e}')
    warn_or_raise doc, warning
    reader.push_include warning, target, target, 1, attrs
  end
end