Class: AudioAddict::Channel

Inherits:
Object
  • Object
show all
Includes:
AutoProperties, Cache, Inspectable
Defined in:
lib/audio_addict/channel.rb

Instance Attribute Summary collapse

Attributes included from AutoProperties

#properties

Instance Method Summary collapse

Methods included from Inspectable

#inspect

Methods included from AutoProperties

#method_missing, #respond_to_missing?

Methods included from Cache

#cache, #cache_dir, #cache_dir!, #cache_life, #cache_life!

Constructor Details

#initialize(radio, properties) ⇒ Channel

Returns a new instance of Channel.



9
10
11
12
# File 'lib/audio_addict/channel.rb', line 9

def initialize(radio, properties)
  @radio = radio
  @properties = properties
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class AudioAddict::AutoProperties

Instance Attribute Details

#radioObject (readonly)

Returns the value of attribute radio.



7
8
9
# File 'lib/audio_addict/channel.rb', line 7

def radio
  @radio
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
# File 'lib/audio_addict/channel.rb', line 18

def active?
  # Seems like each network has a different way of marking inactive channels.
  # This is where we normalize it
  return false unless properties['asset_id']
  return false if (name[0] == 'X') && (key[0] != 'x')

  true
end

#current_trackObject



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

def current_track
  track_history.first
end

#inspectableObject



14
15
16
# File 'lib/audio_addict/channel.rb', line 14

def inspectable
  %i[key name id]
end

#similar_channelsObject



40
41
42
43
44
45
46
# File 'lib/audio_addict/channel.rb', line 40

def similar_channels
  similar = properties['similar_channels']
  return [] unless similar

  ids = similar.map { |s| s['similar_channel_id'] }
  radio.search_by_id ids
end

#track_historyObject



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

def track_history
  @track_history ||= track_history!
end

#track_history!Object



31
32
33
34
# File 'lib/audio_addict/channel.rb', line 31

def track_history!
  response = radio.api.get "track_history/channel/#{id}"
  response.map { |track| Track.new self, track }
end

#vote(direction = :up, track: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/audio_addict/channel.rb', line 48

def vote(direction = :up, track: nil)
  track ||= current_track
  endpoint = "tracks/#{track.id}/vote/#{id}"

  if direction == :delete
    radio.api.delete endpoint
  else
    radio.api.post "#{endpoint}/#{direction}"
  end

  log_like track if (direction == :up) && Config.like_log
end