Class: RSpotify::Artist

Inherits:
Base
  • Object
show all
Defined in:
lib/rspotify/artist.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 = {}) ⇒ Artist

Returns a new instance of Artist.



46
47
48
49
50
51
52
53
54
# File 'lib/rspotify/artist.rb', line 46

def initialize(options = {})
  @genres     = options['genres']
  @images     = options['images']
  @name       = options['name']
  @popularity = options['popularity']
  @top_tracks = {}

  super(options)
end

Dynamic Method Handling

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

Instance Attribute Details

#genresArray<String>

A list of the genres used to classify the album. If not yet classified, the array is empty.

Returns:

  • (Array<String>)

    the current value of genres



7
8
9
# File 'lib/rspotify/artist.rb', line 7

def genres
  @genres
end

#imagesArray<Hash>

The cover art for the album in various sizes, widest first

Returns:

  • (Array<Hash>)

    the current value of images



7
8
9
# File 'lib/rspotify/artist.rb', line 7

def images
  @images
end

#nameString

The name of the album

Returns:

  • (String)

    the current value of name



7
8
9
# File 'lib/rspotify/artist.rb', line 7

def name
  @name
end

#popularityInteger

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

Returns:

  • (Integer)

    the current value of popularity



7
8
9
# File 'lib/rspotify/artist.rb', line 7

def popularity
  @popularity
end

Class Method Details

.find(ids) ⇒ Artist+

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

Examples:

artist = RSpotify::Artist.find('7Ln80lUS6He07XvHI8qqHH')
artist.class #=> RSpotify::Artist
artist.name  #=> "Arctic Monkeys"

ids = %w(7Ln80lUS6He07XvHI8qqHH 3dRfiJ2650SZu6GbydcHNb)
artists = RSpotify::Artist.find(ids)
artists.class       #=> Array
artists.first.class #=> RSpotify::Artist

Parameters:

  • ids (String, Array)

    Maximum: 50 IDs

Returns:



23
24
25
# File 'lib/rspotify/artist.rb', line 23

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

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

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

Examples:

artists = RSpotify::Artist.search('Arctic')
artists.size        #=> 20
artists.first.class #=> RSpotify::Artist
artists.first.name  #=> "Arctic Monkeys"

artists = RSpotify::Artist.search('Arctic', 10)
artists.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 artists to return. Minimum: 1. Maximum: 50.

  • offset (Integer) (defaults to: 0)

    The index of the first artist to return. Use with limit to get the next set of artists.

Returns:



42
43
44
# File 'lib/rspotify/artist.rb', line 42

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

Instance Method Details

#albumsArray<Album>

Returns all albums from artist

Examples:

albums = artist.albums
albums.class       #=> Array
albums.first.class #=> RSpotify::Album
albums.first.name  #=> "AM"

Returns:



65
66
67
68
69
# File 'lib/rspotify/artist.rb', line 65

def albums
  return @albums unless @albums.nil?
  json = RSpotify.get("artists/#{@id}/albums")
  @albums = json['items'].map { |a| Album.new a }
end

#top_tracks(country) ⇒ Array<Track>

Returns artist’s 10 top tracks by country.

Examples:

top_tracks = artist.top_tracks(:US)
top_tracks.class       #=> Array
top_tracks.size        #=> 10
top_tracks.first.class #=> RSpotify::Track

Parameters:

Returns:



81
82
83
84
85
# File 'lib/rspotify/artist.rb', line 81

def top_tracks(country)
  return @top_tracks[country] unless @top_tracks[country].nil?
  json = RSpotify.get("artists/#{@id}/top-tracks?country=#{country}")
  @top_tracks[country] = json['tracks'].map { |t| Track.new t }
end