]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - src/website.rb
Made the output filename the responsibility of the website subclass.
[dead/whatever-dl.git] / src / website.rb
1 #
2 # Copyright Michael Orlitzky
3 #
4 # http://michael.orlitzky.com/
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # http://www.fsf.org/licensing/licenses/gpl.html
17 #
18
19 # This class keeps track of all its subclasses
20 # We use this to loop through every "website" in an
21 # attempt to determine to which site a URL belongs.
22 class Website
23
24 protected;
25
26 @url = nil
27
28
29 def self.inherited(subclass)
30 if superclass.respond_to? :inherited
31 superclass.inherited(subclass)
32 end
33
34 # Every time we're subclassed, add the new
35 # subclass to our list of subclasses.
36 @subclasses ||= []
37 @subclasses << subclass
38 end
39
40
41 public;
42
43 def initialize(url)
44 @url = url
45 end
46
47
48 def self.create(url)
49 # Factory method returning an instance of
50 # the appropriate subclass.
51
52 # Check the URL against each website's class.
53 # The class will know whether or not the URL
54 # "belongs" to its website.
55 @subclasses.each do |w|
56 if w.owns_url?(url)
57 return w.new(url)
58 end
59 end
60
61 # If nothing matched, we don't return an instance
62 # of anything.
63 return nil
64 end
65
66
67 # Abstract definition. Each subclass of Website
68 # should support it on its own.
69 def self.owns_url?(url)
70 raise NotImplementedError
71 end
72
73
74 # Same here. Abstract.
75 def get_video_url()
76 raise NotImplementedError
77 end
78
79
80 # The website class should be responsible for determining the
81 # video's filename. By default, we can take the last component
82 # of the video URL, but in some cases, subclasses will want
83 # to override this behavior.
84 def get_video_filename()
85 # Use whatever comes after the final front slash.
86 return get_video_url().split('/').pop()
87 end
88
89 end