Module: ActsAsVideo::ClassMethods

Defined in:
lib/acts_as_video/acts_as_video.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_video(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/acts_as_video/acts_as_video.rb', line 8

def acts_as_video(options = {})
  cattr_accessor :video_hosts      
  attr :url
  validates_presence_of :url, :on => :create
  
  self.video_hosts = (options[:video_hosts] || [:vimeo, :youtube])
  
  send :include, InstanceMethods

  private
  def domain_from_url(url)
    matches = /(http:\/\/)?(www.)?([A-Za-z0-9._%-]*)\.com/.match(url)
    matches.nil? ? raise("Invalid Url") : domain = matches[3]        
  end

  def class_from_url(url)
    # try to convert the url to a class, if no class exisists, rescue error and raise error
    domain = domain_from_url(url)
    raise "Unsupported Domain" unless video_hosts.include?(domain.to_sym)
    domain_from_url(url).capitalize.constantize rescue raise "Unsupported Domain"
  end  
end

#class_from_url(url) ⇒ Object



23
24
25
26
27
28
# File 'lib/acts_as_video/acts_as_video.rb', line 23

def class_from_url(url)
  # try to convert the url to a class, if no class exisists, rescue error and raise error
  domain = domain_from_url(url)
  raise "Unsupported Domain" unless video_hosts.include?(domain.to_sym)
  domain_from_url(url).capitalize.constantize rescue raise "Unsupported Domain"
end

#domain_from_url(url) ⇒ Object



18
19
20
21
# File 'lib/acts_as_video/acts_as_video.rb', line 18

def domain_from_url(url)
  matches = /(http:\/\/)?(www.)?([A-Za-z0-9._%-]*)\.com/.match(url)
  matches.nil? ? raise("Invalid Url") : domain = matches[3]        
end