Module: SimpleSpotify::Actions::Playlists
- Defined in:
- lib/simplespotify/actions/playlists.rb
Instance Method Summary collapse
- #playlist(owner, playlist = nil, fields: {}, market: nil) ⇒ Object
- #playlist_change_details(owner, playlist = nil, name: nil, public: nil) ⇒ Object
-
#playlist_create(user, name, public: true) ⇒ Object
————– Creation/Update ————–.
-
#playlist_follow(owner, playlist = nil, public: true) ⇒ Object
————– Follows ————–.
-
#playlist_tracks(owner, playlist = nil, fields: {}, limit: 20, offset: 0, market: nil) ⇒ Object
————– Tracks ————–.
- #playlist_tracks_add(owner, playlist = nil, tracks: [], position: nil) ⇒ Object
- #playlist_tracks_remove(owner, playlist = nil, tracks: nil, positions: nil, snapshot_id: nil) ⇒ Object
- #playlist_tracks_reorder(owner, playlist = nil, start: 0, length: 1, before: nil, snapshot_id: nil) ⇒ Object
- #playlist_tracks_replace(owner, playlist = nil, tracks: []) ⇒ Object
- #playlist_unfollow(owner, playlist = nil) ⇒ Object
- #playlists(owner, limit: 20, offset: 0) ⇒ Object
Instance Method Details
#playlist(owner, playlist = nil, fields: {}, market: nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/simplespotify/actions/playlists.rb', line 6 def playlist owner, playlist=nil, fields:{}, market: nil fields = fields.join(',') if fields.is_a? Array = { fields: fields } if owner.is_a? Model::Playlist playlist = owner owner = playlist.owner end res = get "users/#{id_for(owner)}/playlists/#{id_for(playlist)}", (market, ) Model::Playlist.new(res.body) end |
#playlist_change_details(owner, playlist = nil, name: nil, public: nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/simplespotify/actions/playlists.rb', line 36 def playlist_change_details owner, playlist=nil, name: nil, public: nil data = {} data[:name] = name if name data[:public] = public if public if owner.is_a? Model::Playlist playlist = owner owner = playlist.owner end put "users/#{id_for(owner)}/playlists/#{id_for(playlist)}", data end |
#playlist_create(user, name, public: true) ⇒ Object
Creation/Update
30 31 32 33 |
# File 'lib/simplespotify/actions/playlists.rb', line 30 def playlist_create user, name, public: true res = post "users/#{id_for(user)}/playlists", {name: name, public: public} Model::Playlist.new(res.body) end |
#playlist_follow(owner, playlist = nil, public: true) ⇒ Object
Follows
168 169 170 171 172 173 174 |
# File 'lib/simplespotify/actions/playlists.rb', line 168 def playlist_follow owner, playlist=nil, public: true if owner.is_a? Model::Playlist playlist = owner owner = playlist.owner end put "users/#{id_for(owner)}/playlists/#{id_for(playlist)}/followers", {public: public} end |
#playlist_tracks(owner, playlist = nil, fields: {}, limit: 20, offset: 0, market: nil) ⇒ Object
Tracks
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/simplespotify/actions/playlists.rb', line 53 def playlist_tracks owner, playlist=nil, fields: {}, limit: 20, offset: 0, market: nil fields = fields.join(',') if fields.is_a? Array = { fields: fields, limit: limit, offset: offset } if owner.is_a? Model::Playlist playlist = owner owner = playlist.owner end res = get "users/#{id_for(owner)}/playlists/#{id_for(playlist)}/tracks", (market, ) Model::Playlist.new(res.body) end |
#playlist_tracks_add(owner, playlist = nil, tracks: [], position: nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/simplespotify/actions/playlists.rb', line 71 def playlist_tracks_add owner, playlist=nil, tracks: [], position: nil if owner.is_a? Model::Playlist playlist = owner owner = playlist.owner end tracks = case tracks when Array then tracks.map {|t| t.is_a?(Model::Track) ? t.id : t } when String then tracks.split(',') end.map {|t| "spotify:track:#{t.split(':').last}" } data = {} data[:position] = position if position data[:uris] = tracks res = post "users/#{id_for(owner)}/playlists/#{id_for(playlist)}/tracks", data res.body[:snapshot_id] end |
#playlist_tracks_remove(owner, playlist = nil, tracks: nil, positions: nil, snapshot_id: nil) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/simplespotify/actions/playlists.rb', line 94 def playlist_tracks_remove owner, playlist=nil, tracks: nil, positions: nil, snapshot_id: nil if owner.is_a? Model::Playlist playlist = owner owner = playlist.owner end data = {} tracks = case tracks when Array then tracks.map {|t| case t when Model::Track then {uri: t.uri} when String then {uri: "spotify:track:#{t.split(':').last}"} when Hash t[:positions] = [t[:positions]].flatten if t[:positions] t[:uri] = "spotify:track:#{t[:uri].split(':').last}" end } when String then tracks.split(',') else nil end data[:tracks] = tracks if tracks data[:positions] = [positions].flatten if positions data[:snapshot_id] = snapshot_id if snapshot_id puts data res = delete "users/#{id_for(owner)}/playlists/#{id_for(playlist)}/tracks", data res.body[:snapshot_id] end |
#playlist_tracks_reorder(owner, playlist = nil, start: 0, length: 1, before: nil, snapshot_id: nil) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/simplespotify/actions/playlists.rb', line 126 def playlist_tracks_reorder owner, playlist=nil, start: 0, length: 1, before: nil, snapshot_id: nil if owner.is_a? Model::Playlist playlist = owner owner = playlist.owner end raise "Must set `before` option when reordering playlist tracks" unless before data = { range_start: start, range_length: length, insert_before: before } data[:snapshot_id] = snapshot_id unless snapshot_id.nil? res = put "users/#{id_for(owner)}/playlists/#{id_for(playlist)}/tracks", data res.body[:snapshot_id] end |
#playlist_tracks_replace(owner, playlist = nil, tracks: []) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/simplespotify/actions/playlists.rb', line 146 def playlist_tracks_replace owner, playlist=nil, tracks: [] if owner.is_a? Model::Playlist playlist = owner owner = playlist.owner end data[:uris] = tracks.map { |t| case t when Model::Track then t.uri when String then "spotify:track:#{t.split(':').last}" end } res = put "users/#{id_for(owner)}/playlists/#{id_for(playlist)}/tracks", data res.body[:snapshot_id] end |
#playlist_unfollow(owner, playlist = nil) ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/simplespotify/actions/playlists.rb', line 177 def playlist_unfollow owner, playlist=nil if owner.is_a? Model::Playlist playlist = owner owner = playlist.owner end delete "users/#{id_for(owner)}/playlists/#{id_for(playlist)}/followers" end |
#playlists(owner, limit: 20, offset: 0) ⇒ Object
22 23 24 |
# File 'lib/simplespotify/actions/playlists.rb', line 22 def playlists owner, limit: 20, offset: 0 Model::Collection.for(:playlists, get("users/#{id_for(owner)}/playlists", {limit: limit, offset: offset})) end |