forked from snap-cloud/snapcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommercial.rb
109 lines (90 loc) · 3.04 KB
/
commercial.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# frozen_string_literal: true
# == Schema Information
#
# Table name: commercials
#
# id :bigint not null, primary key
# commercial_type :string
# commercialable_type :string
# title :string
# url :string
# created_at :datetime
# updated_at :datetime
# commercial_id :string
# commercialable_id :integer
#
require 'csv'
class Commercial < ApplicationRecord
require 'oembed'
belongs_to :commercialable, polymorphic: true, touch: true
has_paper_trail ignore: [:updated_at], meta: { conference_id: :conference_id }
validates :url, presence: true, uniqueness: { scope: :commercialable }
validates :url, format: URI::DEFAULT_PARSER.make_regexp(%w[https])
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
{ html: iframe_fallback(url) }
# { error: exception.message }
end
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)
errors = {}
errors[:no_event] = []
errors[:validation_errors] = []
# Check if the file has a .csv extension
unless File.extname(file.original_filename).casecmp('.csv').zero?
errors[:validation_errors] << 'File must be a CSV.'
return errors
end
CSV.foreach(file.path, headers: true) do |row|
# You can access columns by their names if headers are included in the file
id = row['Event_ID'].to_i
title = row['Title']
url = row['URL']
event = Event.find_by(id: id)
# Go to next event if the event is not found
(errors[:no_event] << id) && next unless event
commercial = event.commercials.new(title: title, url: url)
unless commercial.save
errors[:validation_errors] << ("Could not create materials for event with ID #{event.id} (" + commercial.errors.full_messages.to_sentence + ')')
end
end
errors
end
private
def valid_url
result = Commercial.render_from_url(url)
errors.add(:base, result[:error]) if result[:error]
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
)
# OEmbed::Providers.register_fallback(
# OEmbed::ProviderDiscovery,
# OEmbed::Providers::Noembed
# )
end
def conference_id
case commercialable_type
when 'Conference' then commercialable_id
when 'Event' then Event.find(commercialable_id).program.conference_id
when 'Venue' then Venue.find(commercialable_id).conference_id
end
end
end