Class: Yt::PlaylistItem

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

Overview

Provides methods to interact with YouTube playlist items.

Instance Attribute Summary collapse

Attributes inherited from Resource

#data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#id, #initialize, #inspect, #select, where

Constructor Details

This class inherits a constructor from Yt::Resource

Instance Attribute Details

#channel_idString (readonly)

Returns the ID of the channel that the playlist belongs to.

Returns:

  • (String)

    the ID of the channel that the playlist belongs to.



23
# File 'lib/yt/playlist_item.rb', line 23

has_attribute :channel_id, in: :snippet

#channel_titleString (readonly)

Returns the title of the channel that the playlist belongs to.

Returns:

  • (String)

    the title of the channel that the playlist belongs to.



27
# File 'lib/yt/playlist_item.rb', line 27

has_attribute :channel_title, in: :snippet

#descriptionString (readonly)

Returns the item’s description.

Returns:

  • (String)

    the item’s description.



# File 'lib/yt/playlist_item.rb', line 9

#playlist_idString (readonly)

Returns the ID of the playlist that the item belongs to.

Returns:

  • (String)

    the ID of the playlist that the item belongs to.



31
# File 'lib/yt/playlist_item.rb', line 31

has_attribute :playlist_id, in: :snippet

#positionInteger (readonly)

Returns the order in which the item appears in the playlist. The value uses a zero-based index so the first item has a position of 0.

Returns:

  • (Integer)

    the order in which the item appears in the playlist. The value uses a zero-based index so the first item has a position of 0.



36
# File 'lib/yt/playlist_item.rb', line 36

has_attribute :position, in: :snippet, type: Integer

#privacy_statusString (readonly)

Returns the item’s privacy status. Valid values are: “private”, “public”, and “unlisted”.

Returns:

  • (String)

    the item’s privacy status. Valid values are: “private”, “public”, and “unlisted”.



45
# File 'lib/yt/playlist_item.rb', line 45

has_attribute :privacy_status, in: :status

#published_atTime (readonly)

Returns the date and time that the item was added to the playlist.

Returns:

  • (Time)

    the date and time that the item was added to the playlist.



15
# File 'lib/yt/playlist_item.rb', line 15

has_attribute :published_at, in: :snippet, type: Time

#thumbnailsHash<String, Hash> (readonly)

Returns the thumbnails associated with the item.

Returns:

  • (Hash<String, Hash>)

    the thumbnails associated with the item.



19
# File 'lib/yt/playlist_item.rb', line 19

has_attribute :thumbnails, in: :snippet

#titleString (readonly)

Returns the item’s title.

Returns:

  • (String)

    the item’s title.



7
# File 'lib/yt/playlist_item.rb', line 7

has_attribute :title, in: :snippet

#video_idString (readonly)

Returns the ID of the video that the item refers to.

Returns:

  • (String)

    the ID of the video that the item refers to.



40
# File 'lib/yt/playlist_item.rb', line 40

has_attribute :video_id, in: %i(snippet resource_id)

Class Method Details

.insert(playlist_id:, video_id:) ⇒ Yt::PlaylistItem

Returns the item created by appending the given video to the given playlist.

Returns:

  • (Yt::PlaylistItem)

    the item created by appending the given video to the given playlist.



61
62
63
64
65
66
67
68
69
70
# File 'lib/yt/playlist_item.rb', line 61

def self.insert(playlist_id:, video_id:)
  parts = %i(id snippet)
  items = -> (body) { [body] } # the response body only includes one item
  resource_id = {kind: 'youtube#video', videoId: video_id}
  snippet = {playlistId: playlist_id, resourceId: resource_id}

  Relation.new(self, parts: parts, extract_items: items) do |options|
    post '/youtube/v3/playlistItems', {part: 'snippet'}, {snippet: snippet}
  end.first
end

Instance Method Details

#deleteBoolean

Returns whether the item was removed from the playlist.

Returns:

  • (Boolean)

    whether the item was removed from the playlist.



73
74
75
76
77
78
79
# File 'lib/yt/playlist_item.rb', line 73

def delete
  items = -> (body) { [{}] } # the response body is empty

  Relation.new(PlaylistItem, id: id, extract_items: items) do |options|
    delete '/youtube/v3/playlistItems', id: options[:id]
  end.any?
end

#thumbnail_url(size = :default) ⇒ String?

Returns the URL of the item’s thumbnail.

Parameters:

  • size (Symbol, String) (defaults to: :default)

    The size of the item’s thumbnail.

Returns:

  • (String)

    if size is :default, the URL of a 120x90px image.

  • (String)

    if size is :medium, the URL of a 320x180px image.

  • (String)

    if size is :high, the URL of a 480x360px image.

  • (String)

    if size is :standard, the URL of a 640x480px image.

  • (String)

    if size is :maxres, the URL of a 1280x720px image.

  • (nil)

    if the size is none of the above.



55
56
57
# File 'lib/yt/playlist_item.rb', line 55

def thumbnail_url(size = :default)
  thumbnails.fetch(size.to_s, {})['url']
end