Class: BBC::Music::Song

Inherits:
Object
  • Object
show all
Defined in:
lib/bbc/music/song.rb

Constant Summary collapse

CLIENT =
BBC::Client.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Song

Returns a new instance of Song.



17
18
19
20
21
22
23
24
25
# File 'lib/bbc/music/song.rb', line 17

def initialize(params)
  if params
    @artist_gid = params['artist_gid']
    @artist_name = params['artist']
    @title = params['title']
  else
    raise BBC::NoDataReturnedError
  end
end

Instance Attribute Details

#artist_gidObject (readonly)

Returns the value of attribute artist_gid.



7
8
9
# File 'lib/bbc/music/song.rb', line 7

def artist_gid
  @artist_gid
end

#artist_nameObject (readonly)

Returns the value of attribute artist_name.



7
8
9
# File 'lib/bbc/music/song.rb', line 7

def artist_name
  @artist_name
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/bbc/music/song.rb', line 7

def title
  @title
end

Class Method Details

.played_recentlyObject



13
14
15
# File 'lib/bbc/music/song.rb', line 13

def self.played_recently
  Array.wrap(CLIENT.now_playing.slice(1, 6)).map { |song| new(song) }
end

.playing_nowObject



9
10
11
# File 'lib/bbc/music/song.rb', line 9

def self.playing_now
  new(CLIENT.now_playing.first)
end

Instance Method Details

#artistObject



27
28
29
# File 'lib/bbc/music/song.rb', line 27

def artist
  @artist ||= BBC::Music::Artist.find(artist_gid)
end

#to_hObject



31
32
33
34
35
36
37
# File 'lib/bbc/music/song.rb', line 31

def to_h
  {
    :artist_gid => @artist_gid,
    :artist_name => @artist_name,
    :title => @title
  }
end

#to_json(options = {}) ⇒ Object



39
40
41
# File 'lib/bbc/music/song.rb', line 39

def to_json(options={})
  JSON.generate(to_h)
end