Class: SpotifyWeb::User

Inherits:
Resource show all
Defined in:
lib/spotify_web/user.rb

Overview

Represents an unauthorized user on Spotify

Direct Known Subclasses

AuthorizedUser

Constant Summary

Constants inherited from Resource

Resource::BASE62_CHARS

Instance Attribute Summary collapse

Attributes inherited from Resource

#gid, #id, #uri, #uri_id

Instance Method Summary collapse

Methods inherited from Resource

#==, attribute, #attributes=, from_search_result, #hash, #initialize, #load, #load_metadata, #loaded?, #metadata=, #metadata_uri, #pretty_print, #pretty_print_instance_variables

Methods included from Assertions

#assert_valid_keys, #assert_valid_values

Constructor Details

This class inherits a constructor from SpotifyWeb::Resource

Instance Attribute Details

#usernameString (readonly)

The username the user registered with on Spotify.

Returns:

  • (String)


8
# File 'lib/spotify_web/user.rb', line 8

attribute :username

Instance Method Details

#playlist(attributes = {}) ⇒ SpotifyWeb::Playlist

Builds a playlist with the given attributes.

Examples:

user.playlist(:starred)                                           # => #<SpotifyWeb::Playlist ...>
user.playlist(:uri => "spotify:user:benzelano:playlist:starred")  # => #<SpotifyWeb::Playlist ...>

Parameters:

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

    The attributes identifying the playlist

Returns:



42
43
44
45
46
47
48
# File 'lib/spotify_web/user.rb', line 42

def playlist(attributes = {})
  if attributes == :starred
    attributes = {:uri_id => 'starred'}
  end

  Playlist.new(client, attributes.merge(:user => self))
end

#playlists(options = {}) ⇒ Array<SpotifyWeb::Playlist>

Gets the playlists managed by the user.

Examples:

user.playlists    # => [#<SpotifyWeb::Playlist ...>, ...]

Parameters:

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

    The search options

Options Hash (options):

  • :limit (Fixnum) — default: 100

    The total number of playlists to get

  • :skip (Fixnum) — default: 0

    The number of playlists to skip when loading the list

  • :include_starred (Boolean) — default: false

    Whether to include the playlist for songs the user starred

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spotify_web/user.rb', line 19

def playlists(options = {})
  options = {:limit => 100, :skip => 0, :include_starred => false}.merge(options)

  response = api('request',
    :uri => "hm://playlist/user/#{username}/rootlist?from=#{options[:skip]}&length=#{options[:limit]}",
    :response_schema => Schema::Playlist4::SelectedListContent
  )

  playlists = response['result'].contents.items.map do |item|
    playlist(:uri => item.uri)
  end
  playlists << playlist(:starred) if options[:include_starred]

  ResourceCollection.new(client, playlists)
end