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!, #embed, #method_missing, #respond_to?

Constructor Details

#initialize(options = {}) ⇒ Track

Returns a new instance of Track.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rspotify/track.rb', line 59

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']
  @uri               = options['uri']
  @name              = options['name']
  @popularity        = options['popularity']
  @preview_url       = options['preview_url']
  @track_number      = options['track_number']
  @played_at         = options['played_at']
  @context_type      = options['context_type']

  @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



16
17
18
# File 'lib/rspotify/track.rb', line 16

def album
  @album
end

#artistsArray<Artist>

The artists who performed the track

Returns:

  • (Array<Artist>)

    the current value of artists



16
17
18
# File 'lib/rspotify/track.rb', line 16

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



16
17
18
# File 'lib/rspotify/track.rb', line 16

def available_markets
  @available_markets
end

#context_typeString

The context the track was played from. Only present when pulled from /recently-played

Returns:

  • (String)

    the current value of context_type



16
17
18
# File 'lib/rspotify/track.rb', line 16

def context_type
  @context_type
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



16
17
18
# File 'lib/rspotify/track.rb', line 16

def disc_number
  @disc_number
end

#duration_msInteger

The track length in milliseconds

Returns:

  • (Integer)

    the current value of duration_ms



16
17
18
# File 'lib/rspotify/track.rb', line 16

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



16
17
18
# File 'lib/rspotify/track.rb', line 16

def explicit
  @explicit
end

#external_idsHash

Known external IDs for the track

Returns:

  • (Hash)

    the current value of external_ids



16
17
18
# File 'lib/rspotify/track.rb', line 16

def external_ids
  @external_ids
end

#nameString

The name of the track

Returns:

  • (String)

    the current value of name



16
17
18
# File 'lib/rspotify/track.rb', line 16

def name
  @name
end

#played_atString

The date and time the track was played. Only present when pulled from /recently-played

Returns:

  • (String)

    the current value of played_at



16
17
18
# File 'lib/rspotify/track.rb', line 16

def played_at
  @played_at
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



16
17
18
# File 'lib/rspotify/track.rb', line 16

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



16
17
18
# File 'lib/rspotify/track.rb', line 16

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



16
17
18
# File 'lib/rspotify/track.rb', line 16

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:



32
33
34
# File 'lib/rspotify/track.rb', line 32

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

.search(query, limit: 20, offset: 0, market: nil) ⇒ Array<Track>

Returns array of Track objects matching the query, ordered by popularity. It’s also possible to find the total number of search results for the query

Examples:

tracks = RSpotify::Track.search('Wanna Know')
tracks = RSpotify::Track.search('Wanna Know', limit: 10, market: 'US')
tracks = RSpotify::Track.search('Wanna Know', market: { from: user })

RSpotify::Track.search('Wanna Know').total #=> 3686

Parameters:

  • query (String)

    The search query’s keywords. For details access here and look for the q parameter description.

  • limit (Integer) (defaults to: 20)

    Maximum number of tracks to return. Maximum: 50. Default: 20.

  • offset (Integer) (defaults to: 0)

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

  • market (String, Hash) (defaults to: nil)

    Optional. An ISO 3166-1 alpha-2 country code or the hash { from: user }, where user is a RSpotify user authenticated using OAuth with scope user-read-private. This will take the user’s country as the market value. For details access here and look for the market parameter description.

Returns:



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

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

Instance Method Details

#audio_featuresObject

Retrieves the audio features for the track



55
56
57
# File 'lib/rspotify/track.rb', line 55

def audio_features
  RSpotify::AudioFeatures.find(@id)
end