FIX: Twitch video and clips onebox requires the domain that is embedding

FIX: Twitch video and clips onebox requires the domain that is embedding

diff --git a/README.md b/README.md
index 4cee7ee..ac2f8f3 100644
--- a/README.md
+++ b/README.md
@@ -46,6 +46,14 @@ preview = Onebox.preview(url)
 "#{preview}" == preview.to_s #=> true
 `‍``
 
+### Twitch Onebox
+
+To be able to embed Twitch video and clips, pass `hostname` in the options to `Onebox.preview`
+
+`‍``ruby
+preview = Onebox.preview(url, hostname: 'www.example.com')
+`‍``
+
 Ruby Support
 ------------
 
diff --git a/lib/onebox/mixins/twitch_onebox.rb b/lib/onebox/mixins/twitch_onebox.rb
index 60e5d88..c0ba266 100644
--- a/lib/onebox/mixins/twitch_onebox.rb
+++ b/lib/onebox/mixins/twitch_onebox.rb
@@ -24,7 +24,9 @@ module Onebox
         end
 
         def to_html
-          "<iframe src=\"//#{base_url}#{query_params}&autoplay=false\" width=\"620\" height=\"378\" frameborder=\"0\" style=\"overflow: hidden;\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"></iframe>"
+          <<~HTML
+          <iframe src="//#{base_url}#{query_params}&parent=#{options[:hostname]}&autoplay=false" width="620" height="378" frameborder="0" style="overflow: hidden;" scrolling="no" allowfullscreen="allowfullscreen"></iframe>
+          HTML
         end
       end
     end
diff --git a/spec/lib/onebox/engine/twitch_clips_onebox_spec.rb b/spec/lib/onebox/engine/twitch_clips_onebox_spec.rb
index 4f991bd..4fa807d 100644
--- a/spec/lib/onebox/engine/twitch_clips_onebox_spec.rb
+++ b/spec/lib/onebox/engine/twitch_clips_onebox_spec.rb
@@ -3,10 +3,11 @@
 require 'spec_helper'
 
 describe Onebox::Engine::TwitchClipsOnebox do
+  let(:hostname) { 'www.example.com' }
+  let(:options) { { hostname: hostname } }
 
   it "has the iframe with the correct channel" do
-    expect(Onebox.preview('https://clips.twitch.tv/FunVastGalagoKlappa').to_s).to match(/<iframe src="\/\/clips\.twitch\.tv\/embed\?clip=FunVastGalagoKlappa/)
-
+    expect(Onebox.preview('https://clips.twitch.tv/FunVastGalagoKlappa', options).to_s).to match(/<iframe src="\/\/clips\.twitch\.tv\/embed\?clip=FunVastGalagoKlappa&amp;parent=#{hostname}/)
   end
 
 end
diff --git a/spec/lib/onebox/engine/twitch_stream_onebox_spec.rb b/spec/lib/onebox/engine/twitch_stream_onebox_spec.rb
index ec4525b..afdef3b 100644
--- a/spec/lib/onebox/engine/twitch_stream_onebox_spec.rb
+++ b/spec/lib/onebox/engine/twitch_stream_onebox_spec.rb
@@ -3,15 +3,15 @@
 require 'spec_helper'
 
 describe Onebox::Engine::TwitchStreamOnebox do
+  let(:hostname) { 'www.example.com' }
+  let(:options) { { hostname: hostname } }
 
   it "has the iframe with the correct channel" do
-    expect(Onebox.preview('https://www.twitch.tv/theduckie908').to_s).to match(/<iframe src="\/\/player\.twitch\.tv\/\?channel=theduckie908/)
-
+    expect(Onebox.preview('https://www.twitch.tv/theduckie908', options).to_s).to match(/<iframe src="\/\/player\.twitch\.tv\/\?channel=theduckie908&amp;parent=#{hostname}/)
   end
 
   it "works in the twitch new interface/url" do
-    expect(Onebox.preview('https://go.twitch.tv/admiralbulldog').to_s).to match(/<iframe src="\/\/player\.twitch\.tv\/\?channel=admiralbulldog/)
-
+    expect(Onebox.preview('https://go.twitch.tv/admiralbulldog', options).to_s).to match(/<iframe src="\/\/player\.twitch\.tv\/\?channel=admiralbulldog&amp;parent=#{hostname}/)
   end
 
 end
diff --git a/spec/lib/onebox/engine/twitch_video_onebox_spec.rb b/spec/lib/onebox/engine/twitch_video_onebox_spec.rb
index 83c50f4..30207f7 100644
--- a/spec/lib/onebox/engine/twitch_video_onebox_spec.rb
+++ b/spec/lib/onebox/engine/twitch_video_onebox_spec.rb
@@ -3,10 +3,10 @@
 require 'spec_helper'
 
 describe Onebox::Engine::TwitchVideoOnebox do
+  let(:hostname) { 'www.example.com' }
+  let(:options) { { hostname: hostname } }
 
   it "has the iframe with the correct channel" do
-    expect(Onebox.preview('https://www.twitch.tv/videos/140675974').to_s).to match(/<iframe src="\/\/player\.twitch\.tv\/\?video=v140675974/)
-
+    expect(Onebox.preview('https://www.twitch.tv/videos/140675974', options).to_s).to match(/<iframe src="\/\/player\.twitch\.tv\/\?video=v140675974&amp;parent=#{hostname}/)
   end
-
 end

GitHub sha: 60046f7e

1 Like

Do we need to use this hostname in any of the other iframe oneboxes? maybe for future proofing?

We only need it for the Twitch onebox at the moment.

1 Like