Class: Shoutout

Inherits:
Object
  • 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

Methods included from QuickAccess

#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

#urlObject (readonly)

Returns the value of attribute url.



13
14
15
# File 'lib/shoutout.rb', line 13

def url
  @url
end

Class Method Details

.metadata(url) ⇒ Object



27
28
29
# File 'lib/shoutout.rb', line 27

def (url)
  new(url).
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

#connectObject



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

  # Read status line
  status_line = @socket.gets
  status_code = status_line.match(/\AHTTP\/([0-9]\.[0-9]) ([0-9]{3})/)[2].to_i

  @connected = true

  read_headers

  if status_code >= 300 && status_code < 400 && headers[:location]
    disconnect

    @url = URI.join(uri, headers[:location]).to_s

    return connect
  end

  unless status_code >= 200 && status_code < 300
    disconnect

    return false
  end

  unless 
    disconnect

    return false
  end

   = Thread.new(&method(:read_metadata))

  true
end

#connected?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/shoutout.rb', line 36

def connected?
  @connected
end

#disconnectObject



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

#listenObject



93
94
95
96
97
98
# File 'lib/shoutout.rb', line 93

def listen
  return unless @connected

  .join
  .join if 
end

#metadataObject



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 
  return  if defined?()

   = 

  received = false
   do ||
    received = true
  end

  already_connected = @connected
  connect unless already_connected

  sleep 0.015 until received

  disconnect unless already_connected

  (&) if 

  
end

#metadata_change(&block) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/shoutout.rb', line 122

def (&block)
   = block

  () if defined?()

  true
end