Class: RSpotify::Base
- Inherits:
-
Object
- Object
- RSpotify::Base
- Defined in:
- lib/rspotify/base.rb
Instance Attribute Summary collapse
-
#external_urls ⇒ Hash
Known external URLs for object.
-
#href ⇒ String
A link to the Web API endpoint.
-
#id ⇒ String
The Spotify ID for the object.
-
#type ⇒ String
The object type (artist, album, etc.).
-
#uri ⇒ String
The Spotify URI for the object.
Class Method Summary collapse
-
.find(ids, type) ⇒ Album, ...
Returns RSpotify object(s) with id(s) and type provided.
-
.search(query, types, limit: 20, offset: 0, market: nil) ⇒ Array<Album>, ...
Returns array of RSpotify objects matching the query, ordered by popularity.
Instance Method Summary collapse
-
#complete! ⇒ Object
When an object is obtained undirectly, Spotify usually returns a simplified version of it.
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
-
#method_missing(method_name, *args) ⇒ Object
Used internally to retrieve an object’s instance variable.
-
#respond_to?(method_name, include_private_methods = false) ⇒ Boolean
Overrides Object#respond_to? to also consider methods dynamically generated by #method_missing.
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
91 92 93 94 95 96 97 |
# File 'lib/rspotify/base.rb', line 91 def initialize( = {}) @external_urls = ['external_urls'] @href = ['href'] @id = ['id'] @type = ['type'] @uri = ['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.
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/rspotify/base.rb', line 123 def method_missing(method_name, *args) attr = "@#{method_name}" super unless instance_variable_defined? attr attr_value = instance_variable_get attr return attr_value if !attr_value.nil? || @id.nil? complete! instance_variable_get attr end |
Instance Attribute Details
#external_urls ⇒ Hash
Known external URLs for object
8 9 10 |
# File 'lib/rspotify/base.rb', line 8 def external_urls @external_urls end |
#href ⇒ String
A link to the Web API endpoint
8 9 10 |
# File 'lib/rspotify/base.rb', line 8 def href @href end |
#id ⇒ String
The Spotify ID for the object
8 9 10 |
# File 'lib/rspotify/base.rb', line 8 def id @id end |
#type ⇒ String
The object type (artist, album, etc.)
8 9 10 |
# File 'lib/rspotify/base.rb', line 8 def type @type end |
#uri ⇒ String
The Spotify URI for the object
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
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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) find_many(ids, type) when String id = ids find_one(id, type) end end |
.search(query, types, limit: 20, offset: 0, market: nil) ⇒ Array<Album>, ...
Returns array of RSpotify objects matching the query, ordered by popularity
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rspotify/base.rb', line 70 def self.search(query, types, limit: 20, offset: 0, market: nil) query = URI::encode query types.gsub!(/\s+/, '') url = "search?q=#{query}&type=#{types}"\ "&limit=#{limit}&offset=#{offset}" json = if market.is_a? Hash url << '&market=from_token' User.oauth_get(market[:from].id, url) else url << "&market=#{market}" if market RSpotify.get(url) end types.split(',').flat_map do |type| type_class = RSpotify.const_get(type.capitalize) json["#{type}s"]['items'].map { |i| type_class.new i } end end |
Instance Method Details
#complete! ⇒ Object
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.
109 110 111 |
# File 'lib/rspotify/base.rb', line 109 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
135 136 137 138 139 |
# File 'lib/rspotify/base.rb', line 135 def respond_to?(method_name, include_private_methods = false) attr = "@#{method_name}" return true if instance_variable_defined? attr super end |