Skip to content

Commit

Permalink
Merge pull request #374 from cs169/187436622-commercials-embed-snap
Browse files Browse the repository at this point in the history
187436622 commercials embed snap
  • Loading branch information
cycomachead authored Jul 20, 2024
2 parents 03a4cc4 + 847e252 commit fdab814
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/controllers/admin/commercials_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def aggregate_errors(errors)
end

def commercial_params
params.require(:commercial).permit(:title, :url)
params.require(:commercial).permit(:title, :url).tap do |params|
params[:url] = Commercial.generate_snap_embed(params[:url]) if params[:url]
end
end
end
end
33 changes: 33 additions & 0 deletions app/models/commercial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Commercial < ApplicationRecord

def self.render_from_url(url)
register_provider
url = Commercial.generate_snap_embed(url)
begin
resource = OEmbed::Providers.get(url, maxwidth: 560, maxheight: 315)
{ html: resource.html.html_safe }
Expand All @@ -39,6 +40,38 @@ def self.render_from_url(url)
end
end

def self.generate_snap_embed(url)
return url unless url

uri = URI.parse(url)
if uri.host == 'snap.berkeley.edu' && uri.path == '/project'
args = URI.decode_www_form(uri.query).to_h
else
return url
end
if args.key?('username') && args.key?('projectname')
new_query = URI.encode_www_form({
'projectname' => args['projectname'],
'username' => args['username'],
'showTitle' => 'true',
'showAuthor' => 'true',
'editButton' => 'true',
'pauseButton' => 'true'
})
new_uri = URI::HTTPS.build(
host: uri.host,
path: '/embed',
query: new_query
)
return new_uri.to_s
end
url
end

def self.iframe_fallback(url)
"<iframe width=560 height=315 frameborder=0 allowfullscreen=true src=\"#{url}\"></iframe>".html_safe
end

def self.read_file(file)
require 'csv'
errors = {}
Expand Down
7 changes: 7 additions & 0 deletions spec/models/commercial_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@
commercial = build(:conference_commercial)
expect(commercial.valid?).to be true
end

it 'parses snap url' do
url = 'https://snap.berkeley.edu/project?username=avi_shor&projectname=stamps'
transformed_url = Commercial.generate_snap_embed(url)
expected_url = 'https://snap.berkeley.edu/embed?projectname=stamps&username=avi_shor&showTitle=true&showAuthor=true&editButton=true&pauseButton=true'
expect(transformed_url).to eq expected_url
end
end

0 comments on commit fdab814

Please sign in to comment.