Class: RemoteVideo
- Inherits:
-
Content
- Object
- Content
- RemoteVideo
- Defined in:
- app/models/remote_video.rb
Constant Summary collapse
- DISPLAY_NAME =
'Video'- VIDEO_VENDORS =
{ :YouTube => { :id => "YouTube", :url => "https://www.youtube.com/embed/" }, :Vimeo => { :id => "Vimeo", :url => "https://player.vimeo.com/video/" } }
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
Class Method Summary collapse
Instance Method Summary collapse
-
#create_config ⇒ Object
Create a new configuration hash if one does not already exist.
-
#load_config ⇒ Object
Load a configuration hash.
-
#load_info ⇒ Object
Load some info about this video from YouTube.
-
#player_url(params = {}) ⇒ Object
Build a URL for an iframe player.
- #render_details ⇒ Object
-
#save_config ⇒ Object
Prepare the configuration to be saved.
-
#set_kind ⇒ Object
Automatically set the kind for the content if it is new.
- #video_id_must_exist ⇒ Object
- #video_vendor_supported ⇒ Object
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
18 19 20 |
# File 'app/models/remote_video.rb', line 18 def config @config end |
Class Method Details
.form_attributes ⇒ Object
48 49 50 51 52 |
# File 'app/models/remote_video.rb', line 48 def self.form_attributes attributes = super() # what about :thumb_url, :title, :description attributes.concat([:config => [:video_vendor, :video_id, :allow_flash]]) end |
Instance Method Details
#create_config ⇒ Object
Create a new configuration hash if one does not already exist. Called during ‘after_initialize`, where a config may or may not exist.
30 31 32 |
# File 'app/models/remote_video.rb', line 30 def create_config self.config = {} if !self.config end |
#load_config ⇒ Object
Load a configuration hash. Converts the JSON data stored for the content into the configuration. Called during ‘after_find`.
37 38 39 |
# File 'app/models/remote_video.rb', line 37 def load_config self.config = JSON.load(self.data) end |
#load_info ⇒ Object
Load some info about this video from YouTube.
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 110 111 112 113 |
# File 'app/models/remote_video.rb', line 55 def load_info # dont abort if there is a duration specified return if self.config['video_id'].nil? #|| !self.duration.nil? require 'net/http' if self.config['video_vendor'] == VIDEO_VENDORS[:YouTube][:id] #begin video_id = URI.escape(self.config['video_id']) url = "http://gdata.youtube.com/feeds/api/videos?q=#{video_id}&v=2&max-results=1&format=5&alt=jsonc" json = Net::HTTP.get_response(URI.parse(url)).body data = ActiveSupport::JSON.decode(json) #rescue # Rails.logger.debug("YouTube not reachable @ #{url}.") # config['video_id'] = '' # return #end if data['data']['totalItems'].to_i <= 0 Rails.logger.debug('No video found from ' + url) self.config['video_id'] = '' return end video_data = data['data']['items'][0] self.config['video_id'] = video_data['id'] self.duration = video_data['duration'].to_i self.config['thumb_url'] = video_data['thumbnail']['hqDefault'] self.config['title'] = video_data['title'] self.config['description'] = video_data['description'] elsif self.config['video_vendor'] == VIDEO_VENDORS[:Vimeo][:id] #todo: put these info urls in the VV constant #http://vimeo.com/api/v2/video/video_id.json data=[] #begin video_id = URI.escape(self.config['video_id']) url = "http://vimeo.com/api/v2/video/#{video_id}.json" uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) response = http.request(request) if response.code == '200' #ok json = response.body data = ActiveSupport::JSON.decode(json) end #rescue # Rails.logger.debug("YouTube not reachable @ #{url}.") # config['video_id'] = '' # return #end if data.empty? Rails.logger.debug('No video found from ' + url) self.config['video_id'] = '' return end video_data = data[0] # some vimeo videos have zero for their duration, so in that case use what the user supplied self.duration = (video_data['duration'].to_i > 0 ? video_data['duration'].to_i : self.duration.to_i) self.config['thumb_url'] = video_data['thumbnail_small'] self.config['title'] = video_data['title'] self.config['description'] = video_data['description'] end end |
#player_url(params = {}) ⇒ Object
Build a URL for an iframe player.
116 117 118 119 120 121 122 123 |
# File 'app/models/remote_video.rb', line 116 def player_url(params={}) url = VIDEO_VENDORS[self.config['video_vendor'].to_sym][:url] + self.config['video_id'] if self.config['allow_flash'] == '0' params['html5'] = 1 end url += '?' + URI.escape(params.collect{|k,v| "#{k}=#{v}"}.join('&')) return url end |
#render_details ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'app/models/remote_video.rb', line 137 def render_details if self.config['video_vendor'] == VIDEO_VENDORS[:YouTube][:id] settings = { :autoplay => 1, # Autostart the video :end => self.duration, # Stop it around the duration :controls => 0, # Don't show any controls :modestbranding => 1, # Use the less fancy branding :rel => 0, # Don't show related videos :showinfo => 0, # Don't show the video info :iv_load_policy => 3 # Don't show any of those in-video labels } elsif self.config['video_vendor'] == VIDEO_VENDORS[:Vimeo][:id] settings = { :api => 1, # use Javascript API :player_id => 'playerv', #arbitrary id of iframe :byline => 0, :portrait => 0, :autoplay => 1 } end {:path => player_url(settings)} end |
#save_config ⇒ Object
Prepare the configuration to be saved. Compress the config hash back into JSON to be stored in the database. Called during ‘before_valication`.
44 45 46 |
# File 'app/models/remote_video.rb', line 44 def save_config self.data = JSON.dump(self.config) end |
#set_kind ⇒ Object
Automatically set the kind for the content if it is new. We use this hidden type that no fields render so Dynamic Content meta content never gets displayed.
23 24 25 26 |
# File 'app/models/remote_video.rb', line 23 def set_kind return unless new_record? self.kind = Kind.where(:name => 'Graphics').first end |
#video_id_must_exist ⇒ Object
125 126 127 128 129 |
# File 'app/models/remote_video.rb', line 125 def video_id_must_exist if config['video_id'].empty? errors.add(:video_id, 'could not be found') end end |
#video_vendor_supported ⇒ Object
131 132 133 134 135 |
# File 'app/models/remote_video.rb', line 131 def video_vendor_supported if config['video_vendor'].empty? || !VIDEO_VENDORS.collect { |a,b| b[:id] }.include?(config['video_vendor']) errors.add(:video_vendor, 'must be ' + VIDEO_VENDORS.collect { |a,b| b[:id] }.join(" or ")) end end |