Class: Spotify::SDK::Me

Inherits:
Base
  • Object
show all
Defined in:
lib/spotify/sdk/me.rb,
lib/spotify/sdk/me/info.rb

Defined Under Namespace

Classes: Info

Instance Attribute Summary

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Base

#initialize, #inspect, #send_http_request

Constructor Details

This class inherits a constructor from Spotify::SDK::Base

Instance Method Details

#following(limit = 50, override_opts = {}) ⇒ Array

Get the current user’s followed artists. Requires the ‘user-read-follow` scope. GET /v1/me/following

Examples:

@sdk.me.following

Parameters:

  • n (Integer)

    Number of results to return.

  • override_opts (Hash) (defaults to: {})

    Custom options for HTTParty.

Returns:

  • (Array)

    artists A list of followed artists, wrapped in Spotify::SDK::Artist



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/spotify/sdk/me.rb', line 93

def following(limit=50, override_opts={})
  request = {
    method:    :get,
    # TODO: Spotify API bug - `limit={n}` returns n-1 artists.
    # ^ Example: `limit=5` returns 4 artists.
    # TODO: Support `type=users` as well as `type=artists`.
    http_path: "/v1/me/following?type=artist&limit=#{[limit, 50].min}",
    keys:      %i[artists items],
    limit:     limit
  }

  send_multiple_http_requests(request, override_opts).map do |artist|
    Spotify::SDK::Artist.new(artist, self)
  end
end

#following?(list, type = :artist, override_opts = {}) ⇒ Hash

Check if the current user is following N users.

Examples:

artists = %w(3q7HBObVc0L8jNeTe5Gofh 0NbfKEOTQCcwd6o7wSDOHI 3TVXtAsR1Inumwj472S9r4)
@sdk.me.following?(artists, :artist)
  # => {"3q7HBObVc0L8jNeTe5Gofh" => false, "0NbfKEOTQCcwd6o7wSDOHI" => false, ...}

users = %w(3q7HBObVc0L8jNeTe5Gofh 0NbfKEOTQCcwd6o7wSDOHI 3TVXtAsR1Inumwj472S9r4)
@sdk.me.following?(users, :user)
  # => {"3q7HBObVc0L8jNeTe5Gofh" => false, "0NbfKEOTQCcwd6o7wSDOHI" => false, ...}

Parameters:

  • list (Array)

    List of Spotify user/artist IDs. Cannot mix user and artist IDs in single request.

  • type (Symbol) (defaults to: :artist)

    Either :user or :artist. Checks if follows respective type of account.

  • override_opts (Hash) (defaults to: {})

    Custom options for HTTParty.

Returns:

  • (Hash)

    hash A hash containing a key with the ID, and a value that equals is_following (boolean).



66
67
68
69
70
71
72
# File 'lib/spotify/sdk/me.rb', line 66

def following?(list, type=:artist, override_opts={})
  raise "Must contain an array" unless list.is_a?(Array)
  raise "Must contain an array of String or Spotify::SDK::Artist" if any_of?(list, [String, Spotify::SDK::Artist])
  raise "type must be either 'artist' or 'user'" unless %i[artist user].include?(type)

  send_is_following_http_requests(list.map {|id| id.try(:id) || id }, type, override_opts)
end

#following_artists?(list, override_opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/spotify/sdk/me.rb', line 74

def following_artists?(list, override_opts={})
  following?(list, :artist, override_opts)
end

#following_users?(list, override_opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/spotify/sdk/me.rb', line 78

def following_users?(list, override_opts={})
  following?(list, :user, override_opts)
end

#history(limit = 10, override_opts = {}) ⇒ Array

Check what tracks a user has recently played.

Examples:

@sdk.me.history
@sdk.me.history(20)

Parameters:

  • limit (Integer) (defaults to: 10)

    How many results to request. Defaults to 10.

  • override_opts (Hash) (defaults to: {})

    Custom options for HTTParty.

Returns:

  • (Array)

    response List of recently played tracked, in chronological order.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/spotify/sdk/me.rb', line 36

def history(limit=10, override_opts={})
  request = {
    method:    :get,
    http_path: "/v1/me/player/recently-played",
    keys:      %i[items],
    limit:     limit
  }

  send_multiple_http_requests(request, override_opts).map do |item|
    Spotify::SDK::Item.new(item, self)
  end
end

#info(override_opts = {}) ⇒ Spotify::SDK::Me::Info

Get the current user’s information. Respective information requires the ‘user-read-private user-read-email user-read-birthdate` scopes. GET /v1/me

Examples:

me = @sdk.me.info

Parameters:

  • override_opts (Hash) (defaults to: {})

    Custom options for HTTParty.

Returns:

See Also:



20
21
22
23
# File 'lib/spotify/sdk/me.rb', line 20

def info(override_opts={})
  me_info = send_http_request(:get, "/v1/me", override_opts)
  Spotify::SDK::Me::Info.new(me_info, self)
end