Class: SimpleSpotify::Client
- Inherits:
-
Object
- Object
- SimpleSpotify::Client
- Defined in:
- lib/simplespotify/client.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#market ⇒ Object
Returns the value of attribute market.
-
#secret ⇒ Object
Returns the value of attribute secret.
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
-
#initialize(client_id, client_secret, default: true) ⇒ Client
constructor
A new instance of Client.
- #search(type, term, market: nil, limit: 20, offset: 0, filters: {}) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(client_id, client_secret, default: true) ⇒ Client
Returns a new instance of Client.
11 12 13 14 15 16 17 |
# File 'lib/simplespotify/client.rb', line 11 def initialize client_id, client_secret, default: true @id = client_id @secret = client_secret @session = nil @market = nil SimpleSpotify.default_client = self if default end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/simplespotify/client.rb', line 4 def id @id end |
#market ⇒ Object
Returns the value of attribute market.
4 5 6 |
# File 'lib/simplespotify/client.rb', line 4 def market @market end |
#secret ⇒ Object
Returns the value of attribute secret.
4 5 6 |
# File 'lib/simplespotify/client.rb', line 4 def secret @secret end |
#session ⇒ Object
Returns the value of attribute session.
4 5 6 |
# File 'lib/simplespotify/client.rb', line 4 def session @session end |
Instance Method Details
#search(type, term, market: nil, limit: 20, offset: 0, filters: {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/simplespotify/client.rb', line 28 def search type, term, market: nil, limit: 20, offset: 0, filters: {} type = type.to_s.gsub(/s$/, '').to_sym types = [:album, :artist, :playlist, :track] raise "Can't search for <#{type}> only: #{types.join(',')}" unless types.include?(type) params = {q: term} params[:market] = market || @market params[:limit] = limit if limit params[:offset] = offset if offset filters = filters.map { |filter, value| value = value.join(',') if value.is_a?(Array) value = [value.min, value.max].map(&:to_s).join('-') if value.is_a?(Range) %{#{filter}:"#{value}"} } params[:q] = ([term]+filters).join(' ') params[:type] = type params = params.reject {|k,v| v.nil?}.to_h response = get "search", params Model::Collection.of(type, response.body["#{type}s".to_sym]) end |
#to_h ⇒ Object
54 55 56 57 58 59 |
# File 'lib/simplespotify/client.rb', line 54 def to_h { client_secret: secret, client_id: id } end |