diff --git a/app/controllers/admin/commercials_controller.rb b/app/controllers/admin/commercials_controller.rb index 0b7c8af3f..093ea3914 100644 --- a/app/controllers/admin/commercials_controller.rb +++ b/app/controllers/admin/commercials_controller.rb @@ -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 diff --git a/app/models/commercial.rb b/app/models/commercial.rb index c62a34ab9..5e1f8eb43 100644 --- a/app/models/commercial.rb +++ b/app/models/commercial.rb @@ -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 } @@ -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) + "".html_safe + end + def self.read_file(file) require 'csv' errors = {} diff --git a/spec/models/commercial_spec.rb b/spec/models/commercial_spec.rb index a09077f8e..2adc17c97 100644 --- a/spec/models/commercial_spec.rb +++ b/spec/models/commercial_spec.rb @@ -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