Class: Caldera::Model::Track

Inherits:
Object
  • Object
show all
Defined in:
lib/caldera/model/track.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Track

Returns a new instance of Track.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/caldera/model/track.rb', line 35

def initialize(data)
  # track_data could maybe use a better name. It's
  # a base64 representation of a binary data representation
  # of a track=
  @track_data = data['track']

  info = data['info']
  @identifier = info['identifier']
  @seekable = info['isSeekable']
  @author = info['author']
  @length = info['length']
  @stream = info['isStream']
  @position = info['position']
  @title = info['title']
  @uri = info['uri']
  @source = info['source']
end

Instance Attribute Details

#authorObject (readonly)

Parameters:

  • The (String)

    track author.



18
19
20
# File 'lib/caldera/model/track.rb', line 18

def author
  @author
end

#identifierObject (readonly)

Parameters:

  • The (String)

    track identifier



12
13
14
# File 'lib/caldera/model/track.rb', line 12

def identifier
  @identifier
end

#lengthObject (readonly)

Parameters:

  • Length (Integer)

    in milliseconds.



21
22
23
# File 'lib/caldera/model/track.rb', line 21

def length
  @length
end

#positionObject (readonly)

Parameters:

  • The (Integer)

    current position in milliseconds.



27
28
29
# File 'lib/caldera/model/track.rb', line 27

def position
  @position
end

#seekableObject (readonly)

Parameters:

  • (true, false)


15
16
17
# File 'lib/caldera/model/track.rb', line 15

def seekable
  @seekable
end

#streamObject (readonly)

Parameters:

  • (true, false)


24
25
26
# File 'lib/caldera/model/track.rb', line 24

def stream
  @stream
end

#titleObject (readonly)

Parameters:

  • The (String)

    track title.



30
31
32
# File 'lib/caldera/model/track.rb', line 30

def title
  @title
end

#track_dataObject (readonly)

Parameters:

  • Base64 (String)

    representation of the track



9
10
11
# File 'lib/caldera/model/track.rb', line 9

def track_data
  @track_data
end

#uriObject (readonly)

Parameters:

  • The (String)

    URI to the track source.



33
34
35
# File 'lib/caldera/model/track.rb', line 33

def uri
  @uri
end

Class Method Details

.from_b64(b64_data) ⇒ Object

Decode a track from base64 track data.

Parameters:

  • b64_data (String)

    Base64 encoded track data, recieved from the Lavalink server.



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
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/caldera/model/track.rb', line 55

def self.from_b64(b64_data)
  data = Base64.decode64(b64_data)
  flags, version = data.unpack('NC')

  raise 'Unsupported track data' if (flags >> 30) != 1

  # This is gross but it's easier than not doing it
  case version
  when 1
    title, author, length, identifier, is_stream, source = data.unpack('@7Z*xZ*Q>xZ*CxZ*')
    Track.new(
      'track' => b64_data,
      'info' => {
        'title' => title,
        'author' => author,
        'length' => length,
        'identifier' => identifier,
        'isStream' => is_stream == 1,
        'source' => source
      }
    )
  when 2
    title, author, length, identifier, is_stream, uri, source = data.unpack('@7Z*xZ*Q>xZ*CxxZ*xZ*xZ*')
    Track.new(
      'track' => b64_data,
      'info' => {
        'title' => title,
        'author' => author,
        'length' => length,
        'identifier' => identifier,
        'isStream' => is_stream == 1,
        'source' => source,
        'uri' => uri
      }
    )
  else
    raise 'Unsupported track version'
  end
end