Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add audio.com onebox provider (#22936)

Roman Agilov3eac47443f1a
diff --git a/lib/onebox/engine/audio_com_onebox.rb b/lib/onebox/engine/audio_com_onebox.rb
new file mode 100644
index 00000000000..394cab24b26
--- /dev/null
+++ b/lib/onebox/engine/audio_com_onebox.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+module Onebox
+ module Engine
+ class AudioComOnebox
+ include Engine
+ include StandardEmbed
+
+ matches_regexp(%r{^https?://audio\.com})
+ requires_iframe_origins "https://audio.com"
+ always_https
+
+ def to_html
+ oembed = get_oembed
+ oembed.html.gsub("visual=true", "visual=false")
+ end
+
+ def placeholder_html
+ oembed = get_oembed
+ return if Onebox::Helpers.blank?(oembed.thumbnail_url)
+ "<img src='#{oembed.thumbnail_url}' #{oembed.title_attr}>"
+ end
+
+ protected
+
+ def get_oembed_url
+ oembed_url = "https://api.audio.com/oembed?url=#{url}"
+ oembed_url += "&maxheight=228" unless url["/collections/"]
+ oembed_url
+ end
+ end
+ end
+end
diff --git a/spec/fixtures/onebox/audio_com_audio_oembed.response b/spec/fixtures/onebox/audio_com_audio_oembed.response
new file mode 100644
index 00000000000..cf628d9ed75
--- /dev/null
+++ b/spec/fixtures/onebox/audio_com_audio_oembed.response
@@ -0,0 +1 @@
+{"type":"rich","version":1,"title":"Discourse onebox test audio","author_name":"Roman Agilov","author_url":"https://audio.com/agilov","provider_name":"audio.com","provider_url":"https://audio.com","cache_age":432000,"html":"<iframe src=\"https://audio.com/embed/audio/1773123508340882?theme=image\" width=\"600\" height=\"204\" style=\"border-radius: 6px; border: none; height: 204px; width: 600px;\"></iframe>","width":600,"height":204}
diff --git a/spec/fixtures/onebox/audio_com_collection_oembed.response b/spec/fixtures/onebox/audio_com_collection_oembed.response
new file mode 100644
index 00000000000..d7dbc0a6f28
--- /dev/null
+++ b/spec/fixtures/onebox/audio_com_collection_oembed.response
@@ -0,0 +1 @@
+{"type":"rich","version":1,"title":"Discourse test collection","author_name":"Roman Agilov","author_url":"https://audio.com/agilov","provider_name":"audio.com","provider_url":"https://audio.com","cache_age":432000,"html":"<iframe src=\"https://audio.com/embed/collection/1773124246389900?theme=image\" width=\"600\" height=\"696\" style=\"border-radius: 6px; border: none; height: 696px; width: 600px;\" allowtransparency=\"true\"></iframe>","width":600,"height":696}
diff --git a/spec/lib/onebox/engine/audio_com_onebox_spec.rb b/spec/lib/onebox/engine/audio_com_onebox_spec.rb
new file mode 100644
index 00000000000..3147b26164f
--- /dev/null
+++ b/spec/lib/onebox/engine/audio_com_onebox_spec.rb
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+RSpec.describe Onebox::Engine::AudioComOnebox do
+ it "has the iframe with the correct audio" do
+ stub_request(
+ :get,
+ "https://api.audio.com/oembed?maxheight=228&url=https://audio.com/agilov/audio/discourse-onebox-test-audio",
+ ).to_return(status: 200, body: onebox_response("audio_com_audio_oembed"))
+ expect(
+ Onebox.preview("https://audio.com/agilov/audio/discourse-onebox-test-audio").to_s,
+ ).to match(%r{<iframe src="https://audio\.com/embed/audio/1773123508340882})
+ end
+
+ it "has the iframe with the correct collection" do
+ stub_request(
+ :get,
+ "https://api.audio.com/oembed?url=https://audio.com/agilov/collections/discourse-test-collection",
+ ).to_return(status: 200, body: onebox_response("audio_com_collection_oembed"))
+ expect(
+ Onebox.preview("https://audio.com/agilov/collections/discourse-test-collection").to_s,
+ ).to match(%r{<iframe src="https://audio\.com/embed/collection/1773124246389900})
+ end
+end