Class: SpotifyWeb::ResourceCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/spotify_web/resource_collection.rb

Overview

Represents a collection of related resources (of the same type) on Spotify

Instance Method Summary collapse

Constructor Details

#initialize(client, *args) ⇒ ResourceCollection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes this collection with the given resources. This will continue to call the superclass's constructor with any additional arguments that get specified.


10
11
12
13
14
15
16
17
18
19
# File 'lib/spotify_web/resource_collection.rb', line 10

def initialize(client, *args)
  @client = client
  @loaded = false
  super(*args)

  # Load all resources if attempted for a single one
  each do |resource|
    resource. = lambda { load unless loaded? }
  end
end

Instance Method Details

#loadtrue Also known as: reload

Loads the attributes for these resources from Spotify. By default this is a no-op and just marks the resource as loaded.


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/spotify_web/resource_collection.rb', line 25

def load
  if count == 1
    # Remove the metadata loader / load directly from the resource
    first. = nil
    first.load
  else
    # Load each resource's metadata
    .each_with_index do |result, index|
      self[index]. = result
    end

    true
  end

  @loaded = true
end

#loaded?Boolean

Determines whether the current collection has been loaded from Spotify.


46
47
48
# File 'lib/spotify_web/resource_collection.rb', line 46

def loaded?
  @loaded
end

#metadataArray

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Looks up the metadata associated with all of the resources in this collection.


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/spotify_web/resource_collection.rb', line 55

def 
  if any? && 
    response = api('request',
      :uri => "hm://metadata/#{resource_name}s",
      :batch => true,
      :payload => map {|resource| {:uri => resource.}},
      :response_schema => 
    )
    response['result']
  else
    []
  end
end