Class: RSpotify::Track

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

Instance Attribute Summary collapse

Attributes inherited from Base

#external_urls, #href, #id, #type, #uri

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#complete!, #method_missing, #respond_to?

Constructor Details

#initialize(options = {}) ⇒ Track

Returns a new instance of Track.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rspotify/track.rb', line 53

def initialize(options = {})
  @available_markets = options['available_markets']
  @disc_number       = options['disc_number']
  @duration_ms       = options['duration_ms']
  @explicit          = options['explicit']
  @external_ids      = options['external_ids']
  @name              = options['name']
  @popularity        = options['popularity']
  @preview_url       = options['preview_url']
  @track_number      = options['track_number']

  @album = if options['album']
    Album.new options['album']
  end

  @artists = if options['artists']
    options['artists'].map { |a| Artist.new a }
  end

  super(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RSpotify::Base

Instance Attribute Details

#albumAlbum

The album on which the track appears

Returns:

  • (Album)

    the current value of album



14
15
16
# File 'lib/rspotify/track.rb', line 14

def album
  @album
end

#artistsArray<Artist>

The artists who performed the track

Returns:

  • (Array<Artist>)

    the current value of artists



14
15
16
# File 'lib/rspotify/track.rb', line 14

def artists
  @artists
end

#available_marketsArray<String>

The markets in which the track can be played. See ISO 3166-1 alpha-2 country codes

Returns:

  • (Array<String>)

    the current value of available_markets



14
15
16
# File 'lib/rspotify/track.rb', line 14

def available_markets
  @available_markets
end

#disc_numberInteger

The disc number. Usually 1 unless the album consists of more than one disc

Returns:

  • (Integer)

    the current value of disc_number



14
15
16
# File 'lib/rspotify/track.rb', line 14

def disc_number
  @disc_number
end

#duration_msInteger

The track length in milliseconds

Returns:

  • (Integer)

    the current value of duration_ms



14
15
16
# File 'lib/rspotify/track.rb', line 14

def duration_ms
  @duration_ms
end

#explicitBoolean

Whether or not the track has explicit lyrics. true = yes it does; false = no it does not OR unknown

Returns:

  • (Boolean)

    the current value of explicit



14
15
16
# File 'lib/rspotify/track.rb', line 14

def explicit
  @explicit
end

#external_idsHash

Known external IDs for the track

Returns:

  • (Hash)

    the current value of external_ids



14
15
16
# File 'lib/rspotify/track.rb', line 14

def external_ids
  @external_ids
end

#nameString

The name of the track

Returns:

  • (String)

    the current value of name



14
15
16
# File 'lib/rspotify/track.rb', line 14

def name
  @name
end

#popularityInteger

The popularity of the track. The value will be between 0 and 100, with 100 being the most popular

Returns:

  • (Integer)

    the current value of popularity



14
15
16
# File 'lib/rspotify/track.rb', line 14

def popularity
  @popularity
end

#preview_urlString

A link to a 30 second preview (MP3 format) of the track

Returns:

  • (String)

    the current value of preview_url



14
15
16
# File 'lib/rspotify/track.rb', line 14

def preview_url
  @preview_url
end

#track_numberInteger

The number of the track. If an album has several discs, the track number is the number on the specified disc

Returns:

  • (Integer)

    the current value of track_number



14
15
16
# File 'lib/rspotify/track.rb', line 14

def track_number
  @track_number
end

Class Method Details

.find(ids) ⇒ Track+

Returns Track object(s) with id(s) provided

Examples:

track = RSpotify::Track.find('2UzMpPKPhbcC8RbsmuURAZ')
track.class #=> RSpotify::Track
track.name  #=> "Do I Wanna Know?"

ids = %w(2UzMpPKPhbcC8RbsmuURAZ 7Jzsc04YpkRwB1zeyM39wE)
tracks = RSpotify::Base.find(ids, 'track')
tracks.class       #=> Array
tracks.first.class #=> RSpotify::Track

Parameters:

  • ids (String, Array)

    Maximum: 50 IDs

Returns:



30
31
32
# File 'lib/rspotify/track.rb', line 30

def self.find(ids)
  super(ids, 'track')
end

.search(query, limit = 20, offset = 0) ⇒ Array<Track>

Returns array of Track objects matching the query, ordered by popularity

Examples:

tracks = RSpotify::Track.search('Thriller')
tracks.size        #=> 20
tracks.first.class #=> RSpotify::Track
tracks.first.name  #=> "Thriller"

tracks = RSpotify::Track.search('Thriller', 10)
tracks.size #=> 10

Parameters:

  • query (String)

    The search query’s keywords. See the q description in here for details.

  • limit (Integer) (defaults to: 20)

    Maximum number of tracks to return. Minimum: 1. Maximum: 50.

  • offset (Integer) (defaults to: 0)

    The index of the first track to return. Use with limit to get the next set of tracks.

Returns:



49
50
51
# File 'lib/rspotify/track.rb', line 49

def self.search(query, limit = 20, offset = 0)
  super(query, 'track', limit, offset)
end