Class: SpotTracks::Track

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Track

Returns a new instance of Track.



5
6
7
8
9
10
# File 'lib/spot_tracks/track.rb', line 5

def initialize(params = {})
  @name = params[:name]
  @artist = params[:artist]
  @image = params[:image]
  @uri = params[:uri]
end

Instance Attribute Details

#artistObject (readonly)

Returns the value of attribute artist.



3
4
5
# File 'lib/spot_tracks/track.rb', line 3

def artist
  @artist
end

#imageObject (readonly)

Returns the value of attribute image.



3
4
5
# File 'lib/spot_tracks/track.rb', line 3

def image
  @image
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/spot_tracks/track.rb', line 3

def name
  @name
end

#uriObject (readonly)

Returns the value of attribute uri.



3
4
5
# File 'lib/spot_tracks/track.rb', line 3

def uri
  @uri
end

Class Method Details

.parse(data = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/spot_tracks/track.rb', line 16

def self.parse(data = {})
  tracks = data.fetch('tracks', {})
  item   = tracks.fetch('items', []).first
  return if item.nil?

  album  = item['album']
  artist = item['artists'].first
  image  = album['images'].first

  new({
    name:   item['name'],
    artist: artist['name'],
    image:  image['url'],
    uri:    item['uri']
  })
end

.search(title) ⇒ Object



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

def self.search(title)
  SpotTracks::Player.new.search(title)
end

Instance Method Details

#titleObject



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

def title
  "#{artist} - #{name}"
end

#to_hObject



37
38
39
40
41
42
# File 'lib/spot_tracks/track.rb', line 37

def to_h
  {
    title: title, name: name, artist: artist,
    urls: { image: image, uri: uri }
  }
end