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 the artist is associated with. 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>

Images of the artist 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 artist

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 artist. 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', limit: 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. Maximum: 50. Default: 20.

  • offset (Integer) (defaults to: 0)

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

Returns:



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

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

Instance Method Details

#albums(limit: 20, offset: 0, **filters) ⇒ Array<Album>

Returns array of albums from artist

Examples:

artist.albums
artist.albums(album_type: 'single,compilation')
artist.albums(limit: 50, country: 'US')

Parameters:

  • limit (Integer) (defaults to: 20)

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

  • offset (Integer) (defaults to: 0)

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

  • album_type (String)

    Optional. A comma-separated list of keywords that will be used to filter the response. If not supplied, all album types will be returned. Valid values are: album; single; appears_on; compilation.

  • market (String)

    Optional. (synonym: country). An ISO 3166-1 alpha-2 country code. Supply this parameter to limit the response to one particular geographical market. If not supplied, results will be returned for all markets. Note if you do not provide this field, you are likely to get duplicate results per album, one for each market in which the album is available.

Returns:



68
69
70
71
72
73
74
75
76
# File 'lib/rspotify/artist.rb', line 68

def albums(limit: 20, offset: 0, **filters)
  url = "artists/#{@id}/albums?limit=#{limit}&offset=#{offset}"
  filters.each do |filter_name, filter_value|
    url << "&#{filter_name}=#{filter_value}"
  end

  json = RSpotify.get(url)
  json['items'].map { |i| Album.new i }
end

Returns array of similar artists. Similarity is based on analysis of the Spotify community’s listening history.

Examples:

artist.name #=> "Arctic Monkeys"
related_artists = artist.related_artists

related_artists.size       #=> 20
related_artists.first.name #=> "Miles Kane"

Returns:



88
89
90
91
92
# File 'lib/rspotify/artist.rb', line 88

def related_artists
  return @related_artists unless @related_artists.nil?
  json = RSpotify.get("artists/#{@id}/related-artists")
  @related_artists = json['artists'].map { |a| Artist.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:



104
105
106
107
108
# File 'lib/rspotify/artist.rb', line 104

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