Class: Spotify

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/clients/spotify.rb

Overview

A spotify-api API client.

Class Method Summary collapse

Class Method Details

.albums(name, artist = nil) ⇒ Object



30
31
32
33
34
# File 'lib/clients/spotify.rb', line 30

def self.albums(name, artist=nil)
  q = { :name => name }
  q.merge!(:artist=>artist) if artist
  get_or_bail("/albums", q)
end

.artists(name) ⇒ Object



20
21
22
# File 'lib/clients/spotify.rb', line 20

def self.artists(name)
  get_or_bail("/artists", {:name => name})
end

.create_playlist(name, track_ids = []) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/clients/spotify.rb', line 53

def self.create_playlist(name, track_ids=[])
  resp = post("/playlists", :body => {      
     :name => name,
     :tracks => track_ids.map { |id| { 'id' => id } }
   }.to_json)
   
  if resp.code == 201
    location = resp.headers['location']
    "201 created (#{location})"
  else
    raise resp.inspect
  end
end

.get_or_bail(path, params = {}) ⇒ Object



14
15
16
17
18
# File 'lib/clients/spotify.rb', line 14

def self.get_or_bail(path, params={})
  resp = get(path, :query=>params)
  raise resp.inspect if resp["status"] != "OK"
  resp["result"]
end

.playlist(id) ⇒ Object



36
37
38
# File 'lib/clients/spotify.rb', line 36

def self.playlist(id)
  get_or_bail("/playlists", :id=>id)
end

.playlistsObject



40
41
42
# File 'lib/clients/spotify.rb', line 40

def self.playlists
  get_or_bail("/playlists")
end

.resolve(tracks) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/clients/spotify.rb', line 67

def self.resolve(tracks)
  tracks.map do |(title, artist)|
    begin
      Spotify.tracks(title, artist).first
    rescue RuntimeError
      nil
      #raise
    end
  end.flatten.compact
end

.tracks(name, artist = nil) ⇒ Object



24
25
26
27
28
# File 'lib/clients/spotify.rb', line 24

def self.tracks(name, artist=nil)
  q = { :name => name }
  q.merge!(:artist=>artist) if artist    
  get_or_bail("/tracks", q)
end

.update_playlist(playlist_id, name = nil, track_ids = []) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/clients/spotify.rb', line 44

def self.update_playlist(playlist_id, name=nil, track_ids=[])  
  data = {}
  data["tracks"] = track_ids.map { |t_id| { 'id' => t_id } } unless track_ids.empty?
  data["name"]   = name if name
  
  resp = put("/playlists/#{playlist_id}", :body => data.to_json)
  raise resp.inspect if resp['status'] != 'OK'
end