Class: SimpleSpotify::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/simplespotify/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/simplespotify/client.rb', line 4

def id
  @id
end

#marketObject

Returns the value of attribute market.



4
5
6
# File 'lib/simplespotify/client.rb', line 4

def market
  @market
end

#secretObject

Returns the value of attribute secret.



4
5
6
# File 'lib/simplespotify/client.rb', line 4

def secret
  @secret
end

#sessionObject

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_hObject



54
55
56
57
58
59
# File 'lib/simplespotify/client.rb', line 54

def to_h
  {
    client_secret: secret,
    client_id: id
  }
end