class Yt::Models::Playlist
Provides methods to interact with YouTube playlists. @see developers.google.com/youtube/v3/docs/playlists
Public Class Methods
@private Override Resource's new to set content details as well if the response includes them
# File lib/yt/models/playlist.rb, line 190 def initialize(options = {}) super options if options[:content_details] @content_detail = ContentDetail.new data: options[:content_details] end end
Public Instance Methods
Adds a video to the playlist. Unlike {#add_video!}, does not raise an error if video can’t be added. @param [String] video_id the video to add to the playlist. @param [Hash] options the options on how to add the video. @option options [Integer] :position where to add video in the playlist. @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
authenticated Yt::Account with permissions to update the playlist.
@return [Yt::PlaylistItem] the item added to the playlist.
# File lib/yt/models/playlist.rb, line 95 def add_video(video_id, options = {}) playlist_item_params = playlist_item_params(video_id, options) playlist_items.insert playlist_item_params, ignore_errors: true end
Adds a video to the playlist. Unlike {#add_video}, raises an error if video can’t be added. @param [String] video_id the video ID to add to the playlist. @param [Hash] options the options on how to add the video. @option options [Integer] :position where to add video in the playlist. @raise [Yt::Errors::RequestError] if video can’t be added. @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
authenticated Yt::Account with permissions to update the playlist.
@return [Yt::PlaylistItem] the item added to the playlist.
# File lib/yt/models/playlist.rb, line 109 def add_video!(video_id, options = {}) playlist_item_params = playlist_item_params(video_id, options) playlist_items.insert playlist_item_params end
Adds multiple videos to the playlist. Unlike {#add_videos!}, does not raise an error if videos can’t be added. @param [Array<String>] video_ids the video IDs to add to the playlist. @param [Hash] options the options on how to add the videos. @option options [Integer] :position where to add videos in the playlist. @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
authenticated Yt::Account with permissions to update the playlist.
@return [Array<Yt::PlaylistItem>] the items added to the playlist.
# File lib/yt/models/playlist.rb, line 122 def add_videos(video_ids = [], options = {}) video_ids.map{|video_id| add_video video_id, options} end
Adds multiple videos to the playlist. Unlike {#add_videos}, raises an error if videos can’t be added. @param [Array<String>] video_ids the video IDs to add to the playlist. @param [Hash] options the options on how to add the videos. @option options [Integer] :position where to add videos in the playlist. @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
authenticated Yt::Account with permissions to update the playlist.
@return [Array<Yt::PlaylistItem>] the items added to the playlist.
# File lib/yt/models/playlist.rb, line 134 def add_videos!(video_ids = [], options = {}) video_ids.map{|video_id| add_video! video_id, options} end
Deletes the playlist. @return [Boolean] whether the playlist does not exist anymore. @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
authenticated Yt::Account with permissions to delete the playlist.
# File lib/yt/models/playlist.rb, line 58 def delete(options = {}) do_delete {@id = nil} !exists? end
Deletes the playlist’s items matching all the given attributes. @return [Array<Boolean>] whether each playlist item matching the given
attributes was deleted.
@raise [Yt::Errors::RequestError] if {Resource#auth auth} is not an
authenticated Yt::Account with permissions to update the playlist.
@param [Hash] attributes the attributes to match the items by. @option attributes [<String, Regexp>] :title The item’s title.
Pass a String for perfect match or a Regexp for advanced match.
@option attributes [<String, Regexp>] :description The item’s
description. Pass a String (perfect match) or a Regexp (advanced).
@option attributes [String] :privacy_status The item’s privacy status. @option attributes [String] :video_id The item’s video ID.
# File lib/yt/models/playlist.rb, line 150 def delete_playlist_items(attributes = {}) playlist_items.delete_all attributes end
@private
# File lib/yt/models/playlist.rb, line 214 def exists? !@id.nil? end
@private Tells `has_reports` to retrieve the reports from YouTube Analytics API either as a Channel
or as a Content Owner. @see developers.google.com/youtube/analytics/channel_reports @see developers.google.com/youtube/analytics/content_owner_reports
# File lib/yt/models/playlist.rb, line 202 def reports_params {}.tap do |params| if auth.owner_name params[:ids] = "contentOwner==#{auth.owner_name}" else params[:ids] = "channel==#{channel_id}" end params[:filters] = "playlist==#{id};isCurated==1" end end
Updates the attributes of a playlist. @return [Boolean] whether the playlist was successfully updated. @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
authenticated Yt::Account with permissions to update the playlist.
@param [Hash] attributes the attributes to update. @option attributes [String] :title The new playlist’s title.
Cannot have more than 100 characters. Can include the characters < and >, which are replaced to ‹ › in order to be accepted by YouTube.
@option attributes [String] :description The new playlist’s description.
Cannot have more than 5000 bytes. Can include the characters < and >, which are replaced to ‹ › in order to be accepted by YouTube.
@option attributes [Array<String>] :tags The new playlist’s tags.
Cannot have more than 500 characters. Can include the characters < and >, which are replaced to ‹ › in order to be accepted by YouTube.
@option attributes [String] :privacy_status The new playlist’s privacy
status. Must be one of: private, unscheduled, public.
@example Update title and description of a playlist.
playlist.update title: 'New title', description: 'New description'
@example Update tags and status of a playlist.
playlist.update tags: ['new', 'tags'], privacy_status: 'public'
# File lib/yt/models/playlist.rb, line 83 def update(attributes = {}) super end
Private Instance Methods
@todo: extend camelize to also camelize the nested hashes, so we
don’t have to write videoId
# File lib/yt/models/playlist.rb, line 230 def playlist_item_params(video_id, params = {}) params.dup.tap do |params| params[:resource_id] ||= {kind: 'youtube#video', videoId: video_id} end end
For updating playlist with content owner auth. @see developers.google.com/youtube/v3/docs/playlists/update
# File lib/yt/models/playlist.rb, line 238 def update_params params = super params[:params] ||= {} params[:params].merge! auth.update_playlist_params params end
@see developers.google.com/youtube/v3/docs/playlists/update
# File lib/yt/models/playlist.rb, line 221 def update_parts keys = [:title, :description, :tags] snippet = {keys: keys, required: true, sanitize_brackets: true} status = {keys: [:privacy_status]} {snippet: snippet, status: status} end