Class: Atreides::Video
- Includes:
- Extendable
- Defined in:
- app/models/atreides/video.rb
Instance Attribute Summary collapse
-
#upload ⇒ Object
Constants.
Class Method Summary collapse
Instance Method Summary collapse
- #embed(*args) ⇒ Object
- #embed_url(options = {}) ⇒ Object
-
#embedable?(target = :embed) ⇒ Boolean
Instance Methods.
- #flash_vars ⇒ Object
- #send_to_vimeo ⇒ Object
- #upload_video ⇒ Object
Methods inherited from Base
Instance Attribute Details
#upload ⇒ Object
Constants
6 7 8 |
# File 'app/models/atreides/video.rb', line 6 def upload @upload end |
Class Method Details
.base_class ⇒ Object
84 85 86 |
# File 'app/models/atreides/video.rb', line 84 def base_class self end |
Instance Method Details
#embed(*args) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/atreides/video.rb', line 97 def (*args) html = read_attribute(:embed) dims = args.first if args.is_a?(Array) return html if !dims.is_a?(Hash) or html.blank? # Process embed for provided dimensions doc = Hpricot(html) %w(iframe object embed).each do |tag| el = doc.at("#{tag}[@width][@height]") next unless el unless dims[:width].blank? el.attributes['width'] = dims[:width].to_s end unless dims[:height].blank? el.attributes['height'] = dims[:height].to_s else unless dims[:width].blank? begin el.attributes['height'] = (( self.height * dims[:width]) / self.width).to_s rescue => e logger.error "VIDEO - Error while resizing embed video : " + e.backtrace.join("\n") end end end end doc.to_s end |
#embed_url(options = {}) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/models/atreides/video.rb', line 125 def ( = {}) @embed_url ||= if # Parse for URL if (tag = Hpricot().at('embed')) tag.attributes['src'] elsif (tag = Hpricot().at("param[@name='movie']")) tag.attributes['value'] elsif (tag = Hpricot().at("iframe[@src]")) tag.attributes['src'] end elsif vimeo_id? params = { :clip_id => vimeo_id, :server => "vimeo.com", :show_title => 0, :show_byline => 0, :show_portrait => 0, :full_screen => 1, :color => "ffffff" }.update() "http://vimeo.com/moogaloop.swf?#{params.to_query}" else url end end |
#embedable?(target = :embed) ⇒ Boolean
Instance Methods
93 94 95 |
# File 'app/models/atreides/video.rb', line 93 def (target = :embed) send("#{target}?") and !send(target).strip.match(%r{^<\w+}).nil? end |
#flash_vars ⇒ Object
151 152 153 154 155 156 157 |
# File 'app/models/atreides/video.rb', line 151 def flash_vars @flash_vars ||= if vimeo_id? {:clip_id => vimeo_id, :server => 'vimeo.com', :fullscreen => 1, :show_title => 1, :show_byline => 1, :show_portrait => 1, :color => '00ADEF'}.to_json else {}.to_json end end |
#send_to_vimeo ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'app/models/atreides/video.rb', line 159 def send_to_vimeo return unless upload.respond_to?(:path) and File.exists?(upload.path) # @vimeo.confirm("ticket_id", "json_manifest") quota = vimeo_upload.get_quota # Check size of upload if upload.size > quota['user']['upload_space']['free'].to_i return errors.add(:upload, "exceeds remaining Viemo quota") end ticket = vimeo_upload.get_ticket resp = vimeo_upload.upload(Settings.vimeo.user_token, upload.path, ticket['ticket']['id'], ticket['ticket']['endpoint']) conf = vimeo_upload.confirm(ticket['ticket']['id']) self.vimeo_id = conf['ticket']['video_id'] # Set video's attributes if vimeo_id? begin vimeo_video.set_privacy(vimeo_id, "anybody") vimeo_video.set_description(vimeo_id, ) if if post # vimeo_video.set_title(vimeo_id, post.title) if post.title? # TODO: Why is the API broken here vimeo_video.(vimeo_id, post.tag_list.join(",")) unless post.tag_list.empty? end rescue Exception => exc logger.error { "Unable to set video attributes: #{exc}\nResp: #{resp.inspect}\nConf:#{conf}" } end self.upload = nil else logger.error { "Unable to save Vimeo video!\nResp: #{resp.inspect}\nConf:#{conf}" } end return vimeo_id? end |
#upload_video ⇒ Object
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 |
# File 'app/models/atreides/video.rb', line 22 def upload_video # upload to vimeo send_to_vimeo if upload and File.exists?(upload) # Set embed code if ! or url_changed? if (:url) self. = url doc = Hpricot(self.) # Get dimensions from embed code object = doc.at("object[@width][@height]") self.width = object.attributes['width'].to_i self.height = object.attributes['height'].to_i # Get url from embed code param = doc.at("param[@name=movie]") self.url = param.attributes['value'] # Guess image url if from youtube if self.url =~ %r{^http://www.youtube.com/v/([0-9A-Za-z\-\_]+)\&.*$} self.thumb_url = "http://i1.ytimg.com/vi/#{$1}/default.jpg" end else require 'oembed_links' OEmbed.register_yaml_file(File.join(Rails.root, 'config/oembed.yml')) OEmbed.transform(url) do |r, url| r.from?(:youtube) { |resp| self.width = resp["width"].to_i self.height = resp["height"].to_i self.thumb_url = resp["thumbnail_url"] self. = resp["html"] # Extract URL from embed html res = resp["html"].scan(/(http\:\/\/www\.youtube\.com\/embed\/.+?)\?/) self.url = res.flatten.shift } r.from?(:vimeo) { |resp| self.vimeo_id = resp["video_id"] self.width = resp["width"].to_i self.height = resp["height"].to_i self.thumb_url = resp["thumbnail_url"] self. = resp["html"] } r.any? { |resp| self. = resp["html"] self.thumb_url = resp["thumbnail_url"] } end end end end |