module ActsAsVideo::InstanceMethods

Public Instance Methods

embed_code(width = 720, height = 480) click to toggle source
# File lib/acts_as_video/acts_as_video.rb, line 65
def embed_code(width = 720, height = 480)
  response({:maxwidth => width, :maxheight => height})['html']
end
embed_id_from_url(url, host) click to toggle source
# File lib/acts_as_video/acts_as_video.rb, line 60
def embed_id_from_url(url, host)
  match = url.match(host::EMBED_ID_REGEX)
  match[1] ? match[1] : raise("Video doesnt exist")
end
embed_url() click to toggle source
# File lib/acts_as_video/acts_as_video.rb, line 69
def embed_url
  raise NotImplementedError, "Can only call #embed_url on a subclass of acts_as_video class" unless host
  host.constantize.send :embed_url, embed_id
end
response(options={}) click to toggle source
# File lib/acts_as_video/acts_as_video.rb, line 74
def response(options={})
  arguments = options.map{ |key, value| "&#{key}=#{value}"}.join
  uri = URI.parse(self.embed_url + arguments)
  res = Net::HTTP.get_response(uri)
  raise "Video doesnt exist" unless res.code == '200'
  JSON.parse(res.body)
end
url() click to toggle source
# File lib/acts_as_video/acts_as_video.rb, line 56
def url
  @url
end
url=(url) click to toggle source
# File lib/acts_as_video/acts_as_video.rb, line 33
def url=(url)
  begin
    domain_class = self.class.send :class_from_url, url          
    self.host = domain_class.to_s
    self.embed_id = embed_id_from_url(url, domain_class)
    data = response
    self.title = data['title']
    self.thumbnail_url = data['thumbnail_url']
    @url = url
  rescue Exception => ex
    case ex.message          
      when "Unsupported Domain"
        self.errors.add :url, "Unsupported Domain, supported video hosts are #{self.class.video_hosts.join(', ')}"
      when "Invalid Url"
        self.errors.add :url, "Invalid Url"
      when "Video doesnt exist"
        self.errors.add :url, "Video not found"
      else
        raise ex
    end
  end
end