Class: Shoutout
- Inherits:
-
Object
show all
- Includes:
- QuickAccess
- Defined in:
- lib/shoutout.rb,
lib/shoutout/util.rb,
lib/shoutout/headers.rb,
lib/shoutout/version.rb,
lib/shoutout/metadata.rb,
lib/shoutout/quick_access.rb
Defined Under Namespace
Modules: QuickAccess, Util
Classes: Headers, Metadata
Constant Summary
collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#audio_info, #bitrate, #content_type, included, #metadata_interval, #now_playing, #public?, #website
Constructor Details
#initialize(url) ⇒ Shoutout
Returns a new instance of Shoutout.
32
33
34
|
# File 'lib/shoutout.rb', line 32
def initialize(url)
@url = url
end
|
Instance Attribute Details
#url ⇒ Object
Returns the value of attribute url.
13
14
15
|
# File 'lib/shoutout.rb', line 13
def url
@url
end
|
Class Method Details
27
28
29
|
# File 'lib/shoutout.rb', line 27
def metadata(url)
new(url).metadata
end
|
.open(url) ⇒ Object
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/shoutout.rb', line 16
def open(url)
shoutcast = new(url)
begin
shoutcast.connect
yield shoutcast
ensure
shoutcast.disconnect
end
end
|
Instance Method Details
#connect ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/shoutout.rb', line 40
def connect
return false if @connected
uri = URI.parse(@url)
@socket = TCPSocket.new(uri.host, uri.port)
@socket.puts "GET #{uri.path} HTTP/1.0"
@socket.puts "Icy-MetaData: 1"
@socket.puts
status_line = @socket.gets
status_code = status_line.match(/\AHTTP\/([0-9]\.[0-9]) ([0-9]{3})/)[2].to_i
@connected = true
if status_code >= 300 && status_code < 400 && [:location]
disconnect
@url = URI.join(uri, [:location]).to_s
return connect
end
unless status_code >= 200 && status_code < 300
disconnect
return false
end
unless metadata_interval
disconnect
return false
end
@read_metadata_thread = Thread.new(&method(:read_metadata))
true
end
|
#connected? ⇒ Boolean
36
37
38
|
# File 'lib/shoutout.rb', line 36
def connected?
@connected
end
|
#disconnect ⇒ Object
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/shoutout.rb', line 82
def disconnect
return false unless @connected
@connected = false
@socket.close if @socket && !@socket.closed?
@socket = nil
true
end
|
#listen ⇒ Object
93
94
95
96
97
98
|
# File 'lib/shoutout.rb', line 93
def listen
return unless @connected
@read_metadata_thread.join
@last_metadata_change_thread.join if @last_metadata_change_thread
end
|
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/shoutout.rb', line 100
def metadata
return @metadata if defined?(@metadata)
original_metadata_change_block = @metadata_change_block
received = false
metadata_change do |new_metadata|
received = true
end
already_connected = @connected
connect unless already_connected
sleep 0.015 until received
disconnect unless already_connected
metadata_change(&original_metadata_change_block) if original_metadata_change_block
@metadata
end
|
122
123
124
125
126
127
128
|
# File 'lib/shoutout.rb', line 122
def metadata_change(&block)
@metadata_change_block = block
report_metadata_change(@metadata) if defined?(@metadata)
true
end
|