Class: Atreides::Video

Inherits:
Base
  • Object
show all
Includes:
Extendable
Defined in:
app/models/atreides/video.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#dom_id, #to_param

Instance Attribute Details

#uploadObject

Constants



6
7
8
# File 'app/models/atreides/video.rb', line 6

def upload
  @upload
end

Class Method Details

.base_classObject



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 embed(*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(options = {})
  @embed_url ||= if embedable?
    # Parse for URL
    if (tag = Hpricot(embed).at('embed'))
      tag.attributes['src']
    elsif (tag = Hpricot(embed).at("param[@name='movie']"))
      tag.attributes['value']
    elsif (tag = Hpricot(embed).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(options)
    "http://vimeo.com/moogaloop.swf?#{params.to_query}"
  else
    url
  end
end

#embedable?(target = :embed) ⇒ Boolean

Instance Methods

Returns:

  • (Boolean)


93
94
95
# File 'app/models/atreides/video.rb', line 93

def embedable?(target = :embed)
  send("#{target}?") and !send(target).strip.match(%r{^<\w+}).nil?
end

#flash_varsObject



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_vimeoObject



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, caption) if caption?
      if post
        # vimeo_video.set_title(vimeo_id, post.title) if post.title? # TODO: Why is the API broken here
        vimeo_video.add_tags(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_videoObject



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 !embedable? or url_changed?
    if embedable?(:url)
      self.embed = url
      doc = Hpricot(self.embed)

      # 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.embed     = 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.embed = resp["html"]
        }
        r.any? { |resp|
          self.embed = resp["html"]
          self.thumb_url = resp["thumbnail_url"]
        }
      end
    end
  end
end