class Napster::Models::Playlist

Playlist model

Constants

ATTRIBUTES

Attributes

client[RW]

Public Class Methods

collection(arg) click to toggle source
# File lib/napster/models/playlist.rb, line 33
def self.collection(arg)
  arg[:data].map do |playlist|
    Playlist.new(data: playlist, client: @client)
  end
end
new(arg) click to toggle source
# File lib/napster/models/playlist.rb, line 24
def initialize(arg)
  @client = arg[:client] if arg[:client]
  return unless arg[:data]

  ATTRIBUTES.each do |attribute|
    send("#{attribute}=", arg[:data][attribute.to_s.camel_case_lower])
  end
end

Public Instance Methods

add_tracks(playlist_id, tracks) click to toggle source
# File lib/napster/models/playlist.rb, line 181
def add_tracks(playlist_id, tracks)
  tracks = tracks.map { |track| { 'id' => track } }
  body = Oj.dump('tracks' => tracks)
  path = "/me/library/playlists/#{playlist_id}/tracks"
  options = {
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  @client.post(path, body, options)
end
all(params) click to toggle source

/me

# File lib/napster/models/playlist.rb, line 80
def all(params)
  get_options = {
    params: params,
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  response = @client.get('/me/library/playlists', get_options)
  Playlist.collection(data: response['playlists'])
end
authenticated_find(playlist_id) click to toggle source
# File lib/napster/models/playlist.rb, line 93
def authenticated_find(playlist_id)
  path = "/me/library/playlists/#{playlist_id}"
  get_options = {
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  response = @client.get(path, get_options)
  return nil if response['playlists'].empty?

  Playlist.new(data: response['playlists'].first, client: @client)
end
authenticated_tags() click to toggle source
# File lib/napster/models/playlist.rb, line 195
def authenticated_tags
  return [] if @id
  path = "/me/library/playlists/#{playlist_id}/tags"
  options = {
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  response = @client.get(path, options)
  return [] if response['tags']
  Tag.collection(data: response['tags'], client: @client)
end
authenticated_tracks(params) click to toggle source
# File lib/napster/models/playlist.rb, line 108
def authenticated_tracks(params)
  path = "/me/library/playlists/#{@id}/tracks"
  options = {
    params: params,
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  response = @client.get(path, options)
  return [] if response['tracks'].empty?

  Track.collection(data: response['tracks'], client: @client)
end
create(playlist_hash) click to toggle source
# File lib/napster/models/playlist.rb, line 124
def create(playlist_hash)
  body = Oj.dump('playlists' => playlist_hash)
  path = '/me/library/playlists'
  options = {
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  response = @client.post(path, body, options)
  Playlist.new(data: response['playlists'].first, client: @client)
end
delete(playlist_id) click to toggle source
# File lib/napster/models/playlist.rb, line 152
def delete(playlist_id)
  path = "/me/library/playlists/#{playlist_id}"
  options = {
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  @client.delete(path, options)
end
find(id) click to toggle source
# File lib/napster/models/playlist.rb, line 51
def find(id)
  e = 'Invalid playlist id'
  raise ArgumentError, e unless Napster::Moniker.check(id, :playlist)

  return authenticated_find(id) if @client.access_token

  response = @client.get("/playlists/#{id}")
  Playlist.new(data: response['playlists'].first, client: @client)
end
playlists_of_the_day(params) click to toggle source

Top level methods

# File lib/napster/models/playlist.rb, line 41
def playlists_of_the_day(params)
  response = @client.get('/playlists', params: params)
  Playlist.collection(data: response['playlists'], client: @client)
end
set_private(playlist_id, boolean) click to toggle source
# File lib/napster/models/playlist.rb, line 164
def set_private(playlist_id, boolean)
  e = 'The argument should be a boolean value.'
  raise ArgumentError, e unless [true, false].include?(boolean)

  privacy_value = boolean ? 'private' : 'public'
  body = Oj.dump('privacy' => privacy_value)
  path = "/me/library/playlists/#{playlist_id}/privacy"
  options = {
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  @client.put(path, body, options)
end
sourced_by(sourced, params) click to toggle source
# File lib/napster/models/playlist.rb, line 242
def sourced_by(sourced, params)
  sourced_error = 'sourced argument should be a string'
  params_error = 'params argument should be a hash'
  raise ArgumentError, sourced_error unless sourced.class == String
  raise ArgumentError, params_error unless params.class == Hash

  path = '/me/search/playlists'
  options = {
    params: params,
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  options[:params][:source] = sourced
  response = @client.get(path, options)
  Playlist.collection(data: response['playlists'], client: @client)
end
tags() click to toggle source
# File lib/napster/models/playlist.rb, line 71
def tags
  return authenticated_tags if @client.access_token

  response = @client.get("/playlists/#{@id}/tags")
  Tag.collection(data: response['tags'], client: @client)
end
tracks(params) click to toggle source

Instance methods

# File lib/napster/models/playlist.rb, line 63
def tracks(params)
  return authenticated_tracks(params) if @client.access_token

  hash = { params: params }
  response = @client.get("/playlists/#{@id}/tracks", hash)
  Track.collection(data: response['tracks'], client: @client)
end
update(playlist_id, playlist_hash) click to toggle source
# File lib/napster/models/playlist.rb, line 138
def update(playlist_id, playlist_hash)
  body = Oj.dump('playlists' => playlist_hash)
  path = "/me/library/playlists/#{playlist_id}"
  options = {
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  response = @client.put(path, body, options)
  Playlist.new(data: response['playlists'].first, client: @client)
end
uploaded_images(options) click to toggle source
# File lib/napster/models/playlist.rb, line 223
def uploaded_images(options)
  return uploaded_images_with_size(options) if options.class == Fixnum
  e = 'Playlist ID is missing.'
  playlist_id = options[:id] ? options[:id] : @id
  raise ArgumentError, e unless playlist_id

  path = "/me/library/playlists/#{playlist_id}/images"
  options = {
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  options[:params] = { size: options[:size] } if options[:size]
  response = @client.get(path, options)
  UploadedImage.collection(data: response['images'], client: @client)
end

Private Instance Methods

uploaded_images_with_size(size) click to toggle source
# File lib/napster/models/playlist.rb, line 264
def uploaded_images_with_size(size)
  path = "/me/library/playlists/#{@id}/images"
  options = {
    params: { size: size },
    headers: {
      Authorization: 'Bearer ' + @client.access_token,
      'Content-Type' => 'application/json',
      'Accept-Version' => '2.0.0'
    }
  }
  response = @client.get(path, options)
  UploadedImage.collection(data: response['images'], client: @client)
end