Class: Song
- Inherits:
-
Hash
show all
- Defined in:
- lib/httmpc/song.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(args) ⇒ Song
Returns a new instance of Song.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/httmpc/song.rb', line 4
def initialize(args)
args.each do |s|
k,v = s.split(/: /)
k.downcase!
case k
when 'file'
Song.parse_file self, k, v
when /-modified/
self[k] = DateTime.parse v
self['onair'] = self[k] unless self.has_key? 'onair'
when 'time'
self[k] = v.to_f
when 'pos', 'id'
self[k] = v.to_i
when 'title'
else
v.force_encoding(Encoding::UTF_8) unless v.encoding == Encoding::UTF_8
self[k] = v
end
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object
49
50
51
52
53
54
55
56
|
# File 'lib/httmpc/song.rb', line 49
def method_missing(method_name)
method_key = method_name.to_s
if self.key? method_key
self[method_key]
else
super
end
end
|
Class Method Details
.parse_file(hash, k, v) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/httmpc/song.rb', line 35
def self.parse_file(hash,k,v)
v.force_encoding('utf-8')
hash[k] = v
v =~ /\.(\d{4}-\d{2}-\d{2}(T\d{2}=\d{2})?)\./
if $1
onair = $1.sub(/=/, ":")
hash['onair'] = DateTime.parse onair
hash['title'] = v.split(/\./)[0]
else
hash['title'] = v
end
end
|
Instance Method Details
#playtime ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/httmpc/song.rb', line 27
def playtime
if self.has_key? 'time'
(Time.parse("1/1") + time).strftime("%H:%M")
else
"?"
end
end
|