Class: XiamiRadio::Radio

Inherits:
Object
  • Object
show all
Defined in:
lib/xiami_radio/radio.rb

Overview

There is a radio as you saw

Constant Summary collapse

TRACKS_PATH =
'/radio/xml/type/%{type}/id/%{oid}'
RADIO_PATH =
'/radio/play/type/%{type}/oid/%{oid}'
XIAMI_CAI =
{type: 8, oid: 0}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, oid:) ⇒ Radio

Returns a new instance of Radio.



11
12
13
14
15
16
17
# File 'lib/xiami_radio/radio.rb', line 11

def initialize(type:, oid:)
  @type = type
  @oid = oid
  @client = Client.new user: User.instance
  @nori = Nori.new(:convert_tags_to => -> (tag) { tag.snakecase.to_sym })
  @play_queue = []
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/xiami_radio/radio.rb', line 9

def client
  @client
end

#noriObject (readonly)

Returns the value of attribute nori.



9
10
11
# File 'lib/xiami_radio/radio.rb', line 9

def nori
  @nori
end

#oidObject (readonly)

Returns the value of attribute oid.



9
10
11
# File 'lib/xiami_radio/radio.rb', line 9

def oid
  @oid
end

#play_queueObject (readonly)

Returns the value of attribute play_queue.



9
10
11
# File 'lib/xiami_radio/radio.rb', line 9

def play_queue
  @play_queue
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/xiami_radio/radio.rb', line 9

def type
  @type
end

Instance Method Details

#get_new_playlistObject



19
20
21
22
# File 'lib/xiami_radio/radio.rb', line 19

def get_new_playlist
  tracks = client.get(uri, format: :xml, headers: headers_referer).dig(:play_list, :track_list, :track)
  tracks.map { |track| Track.new track, radio: self }
end

#headers_refererObject



29
30
31
32
# File 'lib/xiami_radio/radio.rb', line 29

def headers_referer
  referer = client.uri(path: RADIO_PATH % {type: type, oid: oid}).to_s
  {'Referer': referer}
end

#next_trackObject



24
25
26
27
# File 'lib/xiami_radio/radio.rb', line 24

def next_track
  @play_queue += get_new_playlist if @play_queue.size < 2
  @play_queue.shift
end