Class: Yt::Models::Playlist

Inherits:
Resource show all
Defined in:
lib/yt/models/playlist.rb

Overview

Provides methods to interact with YouTube playlists.

Instance Attribute Summary collapse

Attributes inherited from Resource

#auth

Instance Method Summary collapse

Methods inherited from Resource

#initialize, #kind, #username

Methods included from Yt::Modules::Associations

#has_many, #has_one

Constructor Details

This class inherits a constructor from Yt::Models::Resource

Instance Attribute Details

#playlist_itemsYt::Collections::PlaylistItems (readonly)

Returns the playlist’s items.

Returns:



12
# File 'lib/yt/models/playlist.rb', line 12

has_many :playlist_items

Instance Method Details

#add_video(video_id) ⇒ Yt::PlaylistItem

Adds a video to the playlist Does not raise an error if the video cannot be added (e.g., unknown).

This method requires auth to return an authenticated instance of Account with permissions to update the playlist.

Returns:

Raises:



56
57
58
# File 'lib/yt/models/playlist.rb', line 56

def add_video(video_id)
  playlist_items.insert video_params(video_id), ignore_errors: true
end

#add_video!(video_id) ⇒ Object



60
61
62
# File 'lib/yt/models/playlist.rb', line 60

def add_video!(video_id)
  playlist_items.insert video_params(video_id)
end

#add_videos(video_ids = []) ⇒ Object



64
65
66
# File 'lib/yt/models/playlist.rb', line 64

def add_videos(video_ids = [])
  video_ids.map{|video_id| add_video video_id}
end

#add_videos!(video_ids = []) ⇒ Object



68
69
70
# File 'lib/yt/models/playlist.rb', line 68

def add_videos!(video_ids = [])
  video_ids.map{|video_id| add_video! video_id}
end

#delete(options = {}) ⇒ Boolean

Deletes the playlist.

This method requires auth to return an authenticated instance of Account with permissions to delete the playlist.

Returns:

  • (Boolean)

    whether the playlist does not exist anymore.

Raises:



21
22
23
24
# File 'lib/yt/models/playlist.rb', line 21

def delete(options = {})
  do_delete {@id = nil}
  !exists?
end

#delete_playlist_items(attrs = {}) ⇒ Object



72
73
74
# File 'lib/yt/models/playlist.rb', line 72

def delete_playlist_items(attrs = {})
  playlist_items.delete_all attrs
end

#exists?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/yt/models/playlist.rb', line 44

def exists?
  !@id.nil?
end

#update(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/yt/models/playlist.rb', line 26

def update(options = {})
  options[:title] ||= title
  options[:description] ||= description
  options[:tags] ||= tags
  options[:privacy_status] ||= privacy_status

  snippet = options.slice :title, :description, :tags
  status = {privacyStatus: options[:privacy_status]}
  body = {id: @id, snippet: snippet, status: status}

  do_update(params: {part: 'snippet,status'}, body: body) do |data|
    @id = data['id']
    @snippet = Snippet.new data: data['snippet'] if data['snippet']
    @status = Status.new data: data['status'] if data['status']
    true
  end
end