Class: RSpotify::Base

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

Direct Known Subclasses

Album, Artist, Playlist, Track, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



100
101
102
103
104
105
106
# File 'lib/rspotify/base.rb', line 100

def initialize(options = {})
  @external_urls = options['external_urls']
  @href          = options['href']
  @id            = options['id']
  @type          = options['type']
  @uri           = options['uri']
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Used internally to retrieve an object’s instance variable. If instance variable equals nil, calls #complete! on object and retrieve it again.

Examples:

user.id #=> "wizzler"

track = artist.tracks.first
track.instance_variable_get("@popularity") #=> nil
track.popularity #=> 62
track.instance_variable_get("@popularity") #=> 62


132
133
134
135
136
137
138
139
140
141
# File 'lib/rspotify/base.rb', line 132

def method_missing(method_name, *args)
  attr = "@#{method_name}"
  super unless instance_variable_defined? attr

  attr_value = instance_variable_get attr
  return attr_value unless attr_value.nil?

  complete!
  instance_variable_get attr
end

Instance Attribute Details

#external_urlsHash

Known external URLs for object

Returns:

  • (Hash)

    the current value of external_urls



8
9
10
# File 'lib/rspotify/base.rb', line 8

def external_urls
  @external_urls
end

#hrefString

A link to the Web API endpoint

Returns:

  • (String)

    the current value of href



8
9
10
# File 'lib/rspotify/base.rb', line 8

def href
  @href
end

#idString

The Spotify ID for the object

Returns:

  • (String)

    the current value of id



8
9
10
# File 'lib/rspotify/base.rb', line 8

def id
  @id
end

#typeString

The object type (artist, album, etc.)

Returns:

  • (String)

    the current value of type



8
9
10
# File 'lib/rspotify/base.rb', line 8

def type
  @type
end

#uriString

The Spotify URI for the object

Returns:

  • (String)

    the current value of uri



8
9
10
# File 'lib/rspotify/base.rb', line 8

def uri
  @uri
end

Class Method Details

.find(ids, type) ⇒ Album, ...

Returns RSpotify object(s) with id(s) and type provided

Examples:

user = RSpotify::Base.find('wizzler', 'user')
user.class #=> RSpotify::User
user.id    #=> "wizzler"

ids = %w(2UzMpPKPhbcC8RbsmuURAZ 7Jzsc04YpkRwB1zeyM39wE)
tracks = RSpotify::Base.find(ids, 'track')
tracks.class       #=> Array
tracks.first.class #=> RSpotify::Track

Parameters:

  • ids (String, Array)
  • type (String)

Returns:



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

def self.find(ids, type)
  case ids
  when Array
    if type == 'user'
      warn 'Spotify API does not support finding several users simultaneously'
      return false
    end
    limit = (type == 'album' ? 20 : 50)
    if ids.size > limit
      warn "Too many ids requested. Maximum: #{limit} for #{type}"
      return false
    end
    find_many(ids, type)
  when String
    id = ids
    find_one(id, type)
  end
end

.search(query, types, limit = 20, offset = 0) ⇒ Array<Base>

Returns array of RSpotify objects matching the query, ordered by popularity

Examples:

artists = RSpotify::Base.search('Arctic', 'artist')
artists.size        #=> 20
artists.first.class #=> RSpotify::Artist
artists.first.name  #=> "Arctic Monkeys"

albums = RSpotify::Base.search('AM', 'album', 10)
albums.size #=> 10

Parameters:

  • query (String)

    The search query’s keywords. See the q description in here for details.

  • type (String)

    Valid types are: album, artist and track. Separate multiple types with commas.

  • limit (Integer) (defaults to: 20)

    Maximum number of objects to return. Minimum: 1. Maximum: 50.

  • offset (Integer) (defaults to: 0)

    The index of the first object to return. Use with limit to get the next set of objects.

Returns:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rspotify/base.rb', line 78

def self.search(query, types, limit = 20, offset = 0)
  if limit < 1 || limit > 50
    warn 'Limit must be between 1 and 50'
    return false
  end

  types.gsub!(/\s+/, '')

  json = RSpotify.get 'search',
    params: {
      q:      query,
      type:   types,
      limit:  limit,
      offset: offset
    }

  types.split(',').flat_map do |type|
    type_class = RSpotify.const_get(type.capitalize)
    json["#{type}s"]['items'].map { |item| type_class.new item }
  end
end

Instance Method Details

#complete!Object

Note:

It is seldom necessary to use this method explicitly, since RSpotify takes care of it automatically when needed (see #method_missing)

When an object is obtained undirectly, Spotify usually returns a simplified version of it. This method updates it into a full object, with all attributes filled.

Examples:

track = artist.tracks.first
track.instance_variable_get("@popularity") #=> nil
track.complete!
track.instance_variable_get("@popularity") #=> 62


118
119
120
# File 'lib/rspotify/base.rb', line 118

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

#respond_to?(method_name, include_private_methods = false) ⇒ Boolean

Overrides Object#respond_to? to also consider methods dynamically generated by #method_missing

Returns:

  • (Boolean)


144
145
146
147
148
# File 'lib/rspotify/base.rb', line 144

def respond_to?(method_name, include_private_methods = false)
  attr = "@#{method_name}"
  return true if instance_variable_defined? attr
  super
end