Module: ActsAsVideo::InstanceMethods

Defined in:
lib/acts_as_video/acts_as_video.rb

Instance Method Summary collapse

Instance Method Details

#embed_code(width = 720, height = 480) ⇒ Object



65
66
67
# 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) ⇒ Object



60
61
62
63
# 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_urlObject

Raises:

  • (NotImplementedError)


69
70
71
72
# 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 = {}) ⇒ Object



74
75
76
77
78
79
80
# 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

#urlObject



56
57
58
# File 'lib/acts_as_video/acts_as_video.rb', line 56

def url
  @url
end

#url=(url) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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