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
# File 'lib/audio_addict/channel.rb', line 9

def initialize(radio, properties)
  @radio, @properties  = radio, 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)


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

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

#current_trackObject



34
35
36
# File 'lib/audio_addict/channel.rb', line 34

def current_track
  track_history.first
end

#inspectableObject



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

def inspectable
  [:key, :name, :id]
end

#similar_channelsObject



38
39
40
41
42
43
# File 'lib/audio_addict/channel.rb', line 38

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



25
26
27
# File 'lib/audio_addict/channel.rb', line 25

def track_history
  @track_history ||= track_history!
end

#track_history!Object



29
30
31
32
# File 'lib/audio_addict/channel.rb', line 29

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



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/audio_addict/channel.rb', line 45

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

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

  log_like if direction == :up and Config.like_log
end