Change default behavior for creating network requests when caption is unset

This commit is contained in:
Gabriel Arazas 2023-10-29 12:48:09 +08:00
parent bfbea57d6b
commit 924fd9eac5
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
3 changed files with 13 additions and 19 deletions

View File

@ -16,10 +16,8 @@ class FDroidLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
app_id = target
app_metadata_uri = %(https://gitlab.com/fdroid/fdroiddata/-/raw/master/metadata/#{app_id}.yml)
if attrs['caption'].nil?
metadata = OpenURI.open_uri(app_metadata_uri) { |f| YAML.safe_load(f.read) }
attrs['caption'] = metadata['AutoName']
end
metadata = OpenURI.open_uri(app_metadata_uri) { |f| YAML.safe_load(f.read) }
attrs['caption'] ||= metadata['AutoName']
url = %(https://f-droid.org/#{attrs['lang']}/packages/#{app_id})
doc.register :links, url

View File

@ -23,10 +23,8 @@ class FlathubLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
'User-Agent' => ::Asciidoctor::FoodogsquaredCustomExtensions::USER_AGENT
}
if attrs['caption'].nil?
metadata = OpenURI.open_uri(app_metadata_uri, headers) { |f| JSON.parse(f.read) }
attrs['caption'] = metadata['name']
end
metadata = OpenURI.open_uri(app_metadata_uri, headers) { |f| JSON.parse(f.read) }
attrs['caption'] ||= metadata['name']
url = %(https://flathub.org/apps/#{app_id})
doc.register :links, url

View File

@ -23,17 +23,15 @@ class MusicBrainzLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
uri = %(#{root_endpoint}/#{attrs['type']}/#{target})
if attrs['caption'].nil?
metadata = OpenURI.open_uri(uri, headers) { |f| JSON.parse(f.read) }
attrs['caption'] ||= case attrs['type']
when 'artist', 'area', 'events', 'genre', 'instrument', 'label', 'place', 'series'
metadata['name']
when 'recording', 'release-group', 'release', 'cdstub', 'work'
metadata['title']
when 'url'
metadata['resource']
end
end
metadata = OpenURI.open_uri(uri, headers) { |f| JSON.parse(f.read) }
attrs['caption'] ||= case attrs['type']
when 'artist', 'area', 'events', 'genre', 'instrument', 'label', 'place', 'series'
metadata['name']
when 'recording', 'release-group', 'release', 'cdstub', 'work'
metadata['title']
when 'url'
metadata['resource']
end
target = %(https://musicbrainz.org/#{attrs['type']}/#{target})
doc.register :links, target