-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #339 from snap-cloud/import-media-urls
Better Registrations Datatable + Media URL embedding
- Loading branch information
Showing
10 changed files
with
166 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Transform a URL to a version that allows iframes | ||
|
||
class EmbeddableURL | ||
attr_accessor :url | ||
|
||
DEFAULT_FRAME_ATTRS = 'width=560 height=315 frameborder=0 allowfullscreen'.freeze | ||
|
||
TRANSFORMATIONS = { | ||
/snap\.berkeley\.edu/ => :snap, | ||
/docs\.google\.com/ => :google_docs, | ||
/dropbox\.com/ => :dropbox | ||
}.freeze | ||
|
||
def initialize(url) | ||
self.url = url | ||
end | ||
|
||
def render_embed | ||
return render_dropbox if url.include?('dropbox.com') | ||
|
||
# TODO-A11Y: Set an iframe title | ||
"<iframe #{DEFAULT_FRAME_ATTRS} src='#{iframe_url}'></iframe>" | ||
end | ||
|
||
def iframe_url | ||
TRANSFORMATIONS.each do |regex, fn| | ||
return send(fn, url) if url.match?(regex) | ||
end | ||
url | ||
end | ||
|
||
# TODO: Consider adjusting the id / loading if > 1 dropbox embed per page. | ||
def render_dropbox | ||
<<~HTML | ||
<div> | ||
<script type="text/javascript" src="https://www.dropbox.com/static/api/2/dropins.js" id="dropboxjs" data-app-key="#{ENV.fetch('DROPBOX_APP_KEY', nil)}"></script> | ||
<a href="#{dropbox(url)}" | ||
class="dropbox-embed" data-height="315px" data-width="560px"></a> | ||
</div> | ||
HTML | ||
end | ||
|
||
private | ||
|
||
def optional_params | ||
return '' unless url.include?('snap.berkeley') | ||
|
||
'allow="geolocation;microphone;camera"' | ||
end | ||
|
||
def google_docs(url) | ||
# replace /edit, /share, /comment with /embed and remove the querystring | ||
url.gsub(%r{(/edit|/share|/comment).*}, '/embed') | ||
end | ||
|
||
def dropbox(url) | ||
# debugger | ||
uri = URI.parse(url) | ||
params = URI.decode_www_form(uri.query)&.to_h | ||
params.delete('raw') | ||
params['dl'] = '0' | ||
# params['rlkey'] = params['rlkey'] | ||
uri.query = params.to_query | ||
uri.to_s | ||
end | ||
|
||
def snap(url) | ||
uri = URI.parse(url) | ||
query = CGI.parse(uri.query) | ||
username = query['username'][0] || query['user'][0] | ||
project = query['projectname'][0] || query['project'][0] | ||
"https://snap.berkeley.edu/embed?projectname=#{project}&username=#{username}&showTitle=true&showAuthor=true&editButton=true&pauseButton=true" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe EmbeddableURL do | ||
describe '#iframe_url' do | ||
it 'returns the original url if no transformations apply' do | ||
url = 'https://example.com' | ||
expect(EmbeddableURL.new(url).iframe_url).to eq url | ||
end | ||
|
||
it 'transforms a Google Drive URL' do | ||
url = EmbeddableURL.new('https://docs.google.com/presentation/d/1eGbEQtcOPW2N2P5rKfBVfSo2zn4C307Sh6C7vpJsruE/edit#slide=id.g1088c029399_0_47').iframe_url | ||
expect(url).to include '/embed' | ||
expect(url).not_to include('/edit') | ||
end | ||
|
||
it 'transforms a Dropbox URL' do | ||
url = EmbeddableURL.new('https://www.dropbox.com/scl/fi/49gkp6ghfnxgqex64zvzd/Guzdial-SnapCon23.pdf?rlkey=ecwvmcmfscqtwfq21l3kzqcul&dl=1').iframe_url | ||
expect(url).to include('dl=0') | ||
expect(url).not_to include('raw=') | ||
end | ||
|
||
it 'transforms a Snap! Project URL' do | ||
url = EmbeddableURL.new('https://snap.berkeley.edu/project?username=jedi_force&projectname=Autograder%2dlite').iframe_url | ||
expect(url).to include('/embed') | ||
end | ||
end | ||
end |