Class: RSpotify::Category

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#embed, #method_missing, #respond_to?

Constructor Details

#initialize(options = {}) ⇒ Category

Returns a new instance of Category.



66
67
68
69
70
71
# File 'lib/rspotify/category.rb', line 66

def initialize(options = {})
  @icons = options['icons']
  @name  = options['name']

  super(options)
end

Dynamic Method Handling

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

Instance Attribute Details

#external_urlsNilClass

Inexistent for Category.

Returns:

  • (NilClass)

    the current value of external_urls



10
11
12
# File 'lib/rspotify/category.rb', line 10

def external_urls
  @external_urls
end

#hrefString

A link to the Spotify Web API endpoint returning full details of the category.

Returns:

  • (String)

    the current value of href



10
11
12
# File 'lib/rspotify/category.rb', line 10

def href
  @href
end

#iconsArray

An array of image objects. The category icons, in various sizes.

Returns:

  • (Array)

    the current value of icons



10
11
12
# File 'lib/rspotify/category.rb', line 10

def icons
  @icons
end

#idString

The Spotify ID of the category

Returns:

  • (String)

    the current value of id



10
11
12
# File 'lib/rspotify/category.rb', line 10

def id
  @id
end

#nameString

The name of the category.

Returns:

  • (String)

    the current value of name



10
11
12
# File 'lib/rspotify/category.rb', line 10

def name
  @name
end

#typeNilClass

Inexistent for Category.

Returns:

  • (NilClass)

    the current value of type



10
11
12
# File 'lib/rspotify/category.rb', line 10

def type
  @type
end

#uriNilClass

Inexistent for Category.

Returns:

  • (NilClass)

    the current value of uri



10
11
12
# File 'lib/rspotify/category.rb', line 10

def uri
  @uri
end

Class Method Details

.find(id, **options) ⇒ Category

Returns Category object with id provided

Examples:

category = RSpotify::Category.find('party')
category = RSpotify::Category.find('party', country: 'US')
category = RSpotify::Category.find('party', locale: 'es_MX')

Parameters:

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rspotify/category.rb', line 23

def self.find(id, **options)
  url = "browse/categories/#{id}"
  url << '?' if options.any?

  options.each_with_index do |option, index|
    url << "#{option[0]}=#{option[1]}"
    url << '&' unless index == options.size-1
  end

  response = RSpotify.get(url)
  return response if RSpotify.raw_response
  Category.new response
end

.list(limit: 20, offset: 0, **options) ⇒ Array<Category>

Get a list of categories used to tag items in Spotify

Examples:

categories = RSpotify::Category.list
categories = RSpotify::Category.list(country: 'US')
categories = RSpotify::Category.list(locale: 'es_MX', limit: 10)

Parameters:

  • limit (Integer) (defaults to: 20)

    Optional. Maximum number of categories to return. Maximum: 50. Default: 20.

  • offset (Integer) (defaults to: 0)

    Optional. The index of the first category to return. Use with limit to get the next set of categories. Default: 0.

  • country (String)

    Optional. A country: an ISO 3166-1 alpha-2 country code. Provide this parameter if you want to narrow the list of returned categories to those relevant to a particular country. If omitted, the returned categories will be globally relevant.

  • locale (String)

    Optional. The desired language, consisting of a lowercase ISO 639 language code and an uppercase ISO 3166-1 alpha-2 country code, joined by an underscore. For details access / here and look for the locale parameter description.

Returns:



49
50
51
52
53
54
55
56
57
58
# File 'lib/rspotify/category.rb', line 49

def self.list(limit: 20, offset: 0, **options)
  url = "browse/categories?limit=#{limit}&offset=#{offset}"
  options.each do |option, value|
    url << "&#{option}=#{value}"
  end

  response = RSpotify.get(url)
  return response if RSpotify.raw_response
  response['categories']['items'].map { |i| Category.new i }
end

.searchObject

Spotify does not support search for categories.



61
62
63
64
# File 'lib/rspotify/category.rb', line 61

def self.search(*)
  warn 'Spotify API does not support search for categories'
  false
end

Instance Method Details

#complete!Object



74
75
76
# File 'lib/rspotify/category.rb', line 74

def complete!
  initialize RSpotify.get("browse/categories/#{@id}")
end

#playlists(limit: 20, offset: 0, **options) ⇒ Array<Playlist>

Get a list of Spotify playlists tagged with a particular category.

Examples:

playlists = category.playlists
playlists = category.playlists(country: 'BR')
playlists = category.playlists(limit: 10, offset: 20)

Parameters:

  • limit (Integer) (defaults to: 20)

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

  • offset (Integer) (defaults to: 0)

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

  • country (String)

    Optional. A country: an ISO 3166-1 alpha-2 country code. Provide this parameter if you want to narrow the list of returned playlists to those relevant to a particular country. If omitted, the returned playlists will be globally relevant.

Returns:



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rspotify/category.rb', line 89

def playlists(limit: 20, offset: 0, **options)
  url = "browse/categories/#{@id}/playlists"\
        "?limit=#{limit}&offset=#{offset}"

  options.each do |option, value|
    url << "&#{option}=#{value}"
  end

  response = RSpotify.get(url)
  return response if RSpotify.raw_response
  response['playlists']['items'].map { |i| Playlist.new i }
end