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.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rspotify/track.rb', line 62

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']
  @is_playable       = options['is_playable']

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

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

  @linked_from = if options['linked_from']
    TrackLink.new options['linked_from']
  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



18
19
20
# File 'lib/rspotify/track.rb', line 18

def album
  @album
end

#artistsArray<Artist>

The artists who performed the track

Returns:

  • (Array<Artist>)

    the current value of artists



18
19
20
# File 'lib/rspotify/track.rb', line 18

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



18
19
20
# File 'lib/rspotify/track.rb', line 18

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



18
19
20
# File 'lib/rspotify/track.rb', line 18

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



18
19
20
# File 'lib/rspotify/track.rb', line 18

def disc_number
  @disc_number
end

#duration_msInteger

The track length in milliseconds

Returns:

  • (Integer)

    the current value of duration_ms



18
19
20
# File 'lib/rspotify/track.rb', line 18

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



18
19
20
# File 'lib/rspotify/track.rb', line 18

def explicit
  @explicit
end

#external_idsHash

Known external IDs for the track

Returns:

  • (Hash)

    the current value of external_ids



18
19
20
# File 'lib/rspotify/track.rb', line 18

def external_ids
  @external_ids
end

#is_playableBoolean

Whether or not the track is playable in the given market. Only present when track relinking is applied by specifying a market when looking up the track

Returns:

  • (Boolean)

    the current value of is_playable



18
19
20
# File 'lib/rspotify/track.rb', line 18

def is_playable
  @is_playable
end

#linked_fromTrackLink

Details of the requested track. Only present when track relinking is applied and the returned track is different to the one requested because the latter is not available in the given market

Returns:

  • (TrackLink)

    the current value of linked_from



18
19
20
# File 'lib/rspotify/track.rb', line 18

def linked_from
  @linked_from
end

#nameString

The name of the track

Returns:

  • (String)

    the current value of name



18
19
20
# File 'lib/rspotify/track.rb', line 18

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



18
19
20
# File 'lib/rspotify/track.rb', line 18

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



18
19
20
# File 'lib/rspotify/track.rb', line 18

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



18
19
20
# File 'lib/rspotify/track.rb', line 18

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



18
19
20
# File 'lib/rspotify/track.rb', line 18

def track_number
  @track_number
end

Class Method Details

.find(ids, market: nil) ⇒ 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:

Returns:



35
36
37
# File 'lib/rspotify/track.rb', line 35

def self.find(ids, market: nil)
  super(ids, 'track', market: market)
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:



53
54
55
# File 'lib/rspotify/track.rb', line 53

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



58
59
60
# File 'lib/rspotify/track.rb', line 58

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