Implement simplified commercials workflow

It's now possible to simply enter the url of the third party provider to enter a commercial.
This commit is contained in:
chrisbr 2015-04-22 21:30:32 +02:00 committed by Christian Bruckmayer
parent fb7ebab5a0
commit 23664119bd
25 changed files with 220 additions and 194 deletions

View file

@ -1,8 +1,44 @@
class Commercial < ActiveRecord::Base
require 'oembed'
belongs_to :commercialable, polymorphic: true
validates :commercial_id, presence: true
validates :commercial_type, presence: true
validates :url, presence: true
validates :url, format: URI::regexp(%w(http https))
validates :commercial_type, inclusion: { in: CONFIG['commercial_types'].values }
validate :valid_url
def self.render_from_url(url)
register_provider
begin
resource = OEmbed::Providers.get(url, maxwidth: 560, maxheight: 315)
{ html: resource.html.html_safe }
rescue StandardError => exception
{ error: exception.message }
end
end
private
def valid_url
result = Commercial.render_from_url(url)
if result[:error]
errors.add(:base, result[:error])
end
end
def self.register_provider
speakerdeck = OEmbed::Provider.new('http://speakerdeck.com/oembed.json')
speakerdeck << 'https://speakerdeck.com/*'
speakerdeck << 'http://speakerdeck.com/*'
OEmbed::Providers.register(
OEmbed::Providers::Youtube,
OEmbed::Providers::Vimeo,
OEmbed::Providers::Slideshare,
OEmbed::Providers::Flickr,
OEmbed::Providers::Instagram,
speakerdeck
)
end
end