Class: Yt::Models::Channel

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

Overview

A channel resource contains information about a YouTube channel.

Instance Attribute Summary collapse

Attributes inherited from Resource

#auth

Instance Method Summary collapse

Methods inherited from Resource

#kind, #update, #username

Methods included from Associations::HasReports

#has_report

Methods included from Associations::HasViewerPercentages

#has_viewer_percentages

Methods included from Associations::HasOne

#has_one

Methods included from Associations::HasMany

#has_many

Methods included from Associations::HasAuthentication

#has_authentication

Constructor Details

#initialize(options = {}) ⇒ Channel

Override Resource’s new to set statistics as well if the response includes them



92
93
94
95
96
97
98
99
100
# File 'lib/yt/models/channel.rb', line 92

def initialize(options = {})
  super options
  if options[:statistics]
    @statistics_set = StatisticsSet.new data: options[:statistics]
  end
  if options[:viewer_percentages]
    @viewer_percentages = options[:viewer_percentages]
  end
end

Instance Attribute Details

#content_owner_detailYt::Models::ContentOwnerDetail (readonly)

Returns the video’s content owner details.

Returns:



73
# File 'lib/yt/models/channel.rb', line 73

has_one :content_owner_detail

#playlistsYt::Collections::Playlists (readonly)

Returns the channel’s playlists.

Returns:



14
# File 'lib/yt/models/channel.rb', line 14

has_many :playlists

#statistics_setYt::Models::StatisticsSet (readonly)

Returns the statistics for the video.

Returns:



66
# File 'lib/yt/models/channel.rb', line 66

has_one :statistics_set

#subscribed_channelsYt::Collections::SubscribedChannels (readonly)

Returns the channels that the channel is subscribed to.

Returns:

Raises:

  • (Yt::Errors::Forbidden)

    if the owner of the channel has explicitly select the option to keep all subscriptions private.



80
# File 'lib/yt/models/channel.rb', line 80

has_many :subscribed_channels

#subscriptionYt::Models::Subscription (readonly)

Returns the channel’s subscription by auth.

Returns:

Raises:



88
# File 'lib/yt/models/channel.rb', line 88

has_one :subscription

#videosYt::Collections::Videos (readonly)

Returns the channel’s videos.

Returns:



10
# File 'lib/yt/models/channel.rb', line 10

has_many :videos

Instance Method Details

#average_view_duration(options = {}) ⇒ Hash<Date, Float>

Returns the average_view_duration for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the average_view_duration for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the average_view_duration for a range of a days.



50
# File 'lib/yt/models/channel.rb', line 50

has_report :average_view_duration

#average_view_duration_on(date) ⇒ Float

Returns the average_view_duration for a single day.

Parameters:

  • date (#to_date)

    The single day to get the average_view_duration for.

Returns:

  • (Float)

    the average_view_duration for a single day.



50
# File 'lib/yt/models/channel.rb', line 50

has_report :average_view_duration

#average_view_percentage(options = {}) ⇒ Hash<Date, Float>

Returns the average_view_percentage for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the average_view_percentage for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the average_view_percentage for a range of a days.



53
# File 'lib/yt/models/channel.rb', line 53

has_report :average_view_percentage

#average_view_percentage_on(date) ⇒ Float

Returns the average_view_percentage for a single day.

Parameters:

  • date (#to_date)

    The single day to get the average_view_percentage for.

Returns:

  • (Float)

    the average_view_percentage for a single day.



53
# File 'lib/yt/models/channel.rb', line 53

has_report :average_view_percentage

#comments(options = {}) ⇒ Hash<Date, Float>

Returns the comments for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the comments for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the comments for a range of a days.



23
# File 'lib/yt/models/channel.rb', line 23

has_report :comments

#comments_on(date) ⇒ Float

Returns the comments for a single day.

Parameters:

  • date (#to_date)

    The single day to get the comments for.

Returns:

  • (Float)

    the comments for a single day.



23
# File 'lib/yt/models/channel.rb', line 23

has_report :comments

#delete_playlists(attrs = {}) ⇒ Object



170
171
172
# File 'lib/yt/models/channel.rb', line 170

def delete_playlists(attrs = {})
  playlists.delete_all attrs
end

#dislikes(options = {}) ⇒ Hash<Date, Float>

Returns the dislikes for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the dislikes for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the dislikes for a range of a days.



29
# File 'lib/yt/models/channel.rb', line 29

has_report :dislikes

#dislikes_on(date) ⇒ Float

Returns the dislikes for a single day.

Parameters:

  • date (#to_date)

    The single day to get the dislikes for.

Returns:

  • (Float)

    the dislikes for a single day.



29
# File 'lib/yt/models/channel.rb', line 29

has_report :dislikes

#earnings(options = {}) ⇒ Hash<Date, Float>

Returns the earnings for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the earnings for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the earnings for a range of a days.



17
# File 'lib/yt/models/channel.rb', line 17

has_report :earnings

#earnings_on(date) ⇒ Float

Returns the earnings for a single day.

Parameters:

  • date (#to_date)

    The single day to get the earnings for.

Returns:

  • (Float)

    the earnings for a single day.



17
# File 'lib/yt/models/channel.rb', line 17

has_report :earnings

#estimated_minutes_watched(options = {}) ⇒ Hash<Date, Float>

Returns the estimated_minutes_watched for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the estimated_minutes_watched for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the estimated_minutes_watched for a range of a days.



47
# File 'lib/yt/models/channel.rb', line 47

has_report :estimated_minutes_watched

#estimated_minutes_watched_on(date) ⇒ Float

Returns the estimated_minutes_watched for a single day.

Parameters:

  • date (#to_date)

    The single day to get the estimated_minutes_watched for.

Returns:

  • (Float)

    the estimated_minutes_watched for a single day.



47
# File 'lib/yt/models/channel.rb', line 47

has_report :estimated_minutes_watched

#favorites_added(options = {}) ⇒ Hash<Date, Float>

Returns the favorites_added for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the favorites_added for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the favorites_added for a range of a days.



41
# File 'lib/yt/models/channel.rb', line 41

has_report :favorites_added

#favorites_added_on(date) ⇒ Float

Returns the favorites_added for a single day.

Parameters:

  • date (#to_date)

    The single day to get the favorites_added for.

Returns:

  • (Float)

    the favorites_added for a single day.



41
# File 'lib/yt/models/channel.rb', line 41

has_report :favorites_added

#favorites_removed(options = {}) ⇒ Hash<Date, Float>

Returns the favorites_removed for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the favorites_removed for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the favorites_removed for a range of a days.



44
# File 'lib/yt/models/channel.rb', line 44

has_report :favorites_removed

#favorites_removed_on(date) ⇒ Float

Returns the favorites_removed for a single day.

Parameters:

  • date (#to_date)

    The single day to get the favorites_removed for.

Returns:

  • (Float)

    the favorites_removed for a single day.



44
# File 'lib/yt/models/channel.rb', line 44

has_report :favorites_removed

#impressions(options = {}) ⇒ Hash<Date, Float>

Returns the impressions for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the impressions for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the impressions for a range of a days.



56
# File 'lib/yt/models/channel.rb', line 56

has_report :impressions

#impressions_on(date) ⇒ Float

Returns the impressions for a single day.

Parameters:

  • date (#to_date)

    The single day to get the impressions for.

Returns:

  • (Float)

    the impressions for a single day.



56
# File 'lib/yt/models/channel.rb', line 56

has_report :impressions

#likes(options = {}) ⇒ Hash<Date, Float>

Returns the likes for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the likes for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the likes for a range of a days.



26
# File 'lib/yt/models/channel.rb', line 26

has_report :likes

#likes_on(date) ⇒ Float

Returns the likes for a single day.

Parameters:

  • date (#to_date)

    The single day to get the likes for.

Returns:

  • (Float)

    the likes for a single day.



26
# File 'lib/yt/models/channel.rb', line 26

has_report :likes

#monetized_playbacks(options = {}) ⇒ Hash<Date, Float>

Returns the monetized_playbacks for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the monetized_playbacks for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the monetized_playbacks for a range of a days.



59
# File 'lib/yt/models/channel.rb', line 59

has_report :monetized_playbacks

#monetized_playbacks_on(date) ⇒ Float

Returns the monetized_playbacks for a single day.

Parameters:

  • date (#to_date)

    The single day to get the monetized_playbacks for.

Returns:

  • (Float)

    the monetized_playbacks for a single day.



59
# File 'lib/yt/models/channel.rb', line 59

has_report :monetized_playbacks

#shares(options = {}) ⇒ Hash<Date, Float>

Returns the shares for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the shares for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the shares for a range of a days.



32
# File 'lib/yt/models/channel.rb', line 32

has_report :shares

#shares_on(date) ⇒ Float

Returns the shares for a single day.

Parameters:

  • date (#to_date)

    The single day to get the shares for.

Returns:

  • (Float)

    the shares for a single day.



32
# File 'lib/yt/models/channel.rb', line 32

has_report :shares

#subscribeObject

Subscribes the authenticated account to the channel. Does not raise an error if the account was already subscribed.

This method requires auth to return an authenticated instance of Account.

Raises:



163
164
165
166
167
168
# File 'lib/yt/models/channel.rb', line 163

def subscribe
  subscriptions.insert(ignore_errors: true).tap do |subscription|
    throttle_subscriptions
    @subscription = subscription
  end
end

#subscribe!Object

Subscribes the authenticated account to the channel. Raises an error if the account was already subscribed.

This method requires auth to return an authenticated instance of Account.

Raises:



149
150
151
152
153
154
# File 'lib/yt/models/channel.rb', line 149

def subscribe!
  subscriptions.insert.tap do |subscription|
    throttle_subscriptions
    @subscription = subscription
  end
end

#subscribed?Boolean

Returns whether the authenticated account is subscribed to the channel.

This method requires auth to return an authenticated instance of Account.

Returns:

  • (Boolean)

    whether the account is subscribed to the channel.

Raises:



109
110
111
112
113
114
# File 'lib/yt/models/channel.rb', line 109

def subscribed?
  sleep [(@subscriptions_updated_at || Time.now) - Time.now, 0].max
  subscription.exists?
rescue Errors::NoItems
  false
end

#subscribers_gained(options = {}) ⇒ Hash<Date, Float>

Returns the subscribers_gained for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the subscribers_gained for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the subscribers_gained for a range of a days.



35
# File 'lib/yt/models/channel.rb', line 35

has_report :subscribers_gained

#subscribers_gained_on(date) ⇒ Float

Returns the subscribers_gained for a single day.

Parameters:

  • date (#to_date)

    The single day to get the subscribers_gained for.

Returns:

  • (Float)

    the subscribers_gained for a single day.



35
# File 'lib/yt/models/channel.rb', line 35

has_report :subscribers_gained

#subscribers_lost(options = {}) ⇒ Hash<Date, Float>

Returns the subscribers_lost for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the subscribers_lost for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the subscribers_lost for a range of a days.



38
# File 'lib/yt/models/channel.rb', line 38

has_report :subscribers_lost

#subscribers_lost_on(date) ⇒ Float

Returns the subscribers_lost for a single day.

Parameters:

  • date (#to_date)

    The single day to get the subscribers_lost for.

Returns:

  • (Float)

    the subscribers_lost for a single day.



38
# File 'lib/yt/models/channel.rb', line 38

has_report :subscribers_lost

#unsubscribeObject

Unsubscribes the authenticated account from the channel. Does not raise an error if the account was not subscribed.

This method requires auth to return an authenticated instance of Account.

Raises:



136
137
138
# File 'lib/yt/models/channel.rb', line 136

def unsubscribe
  unsubscribe! if subscribed?
end

#unsubscribe!Object

Unsubscribes the authenticated account from the channel. Raises an error if the account was not subscribed.

This method requires auth to return an authenticated instance of Account.

Raises:



125
126
127
# File 'lib/yt/models/channel.rb', line 125

def unsubscribe!
  subscription.delete.tap{ throttle_subscriptions }
end

#viewer_percentage(filters = {}) ⇒ Float

Returns the percentage of viewer matching the filters.

Examples:

Return the % of male viewers of a video

channel.viewer_percentage(gender: :male) #=> 52.02

Parameters:

  • filters (Hash) (defaults to: {})

    The type of viewers to be accounted for.

Options Hash (filters):

  • :gender (Symbol)

    Either :male or :female.

Returns:

  • (Float)

    the percentage of viewer matching the filters.



62
# File 'lib/yt/models/channel.rb', line 62

has_viewer_percentages

#viewer_percentagesHash<Symbol,Hash<String,Float>>

Returns the viewer percentages. The first-level hash identifies the genres (:female, :male). The second-level hash identifies the age ranges (‘18-24’, ‘25-34’, ‘35-44’, ‘45-54’, ‘55-64’, ‘65-’).

Examples:

Return the % of male viewers of a channel older than 64

channel.viewer_percentages[:male]['65-'] #=> 12.02

Returns:

  • (Hash<Symbol,Hash<String,Float>>)

    the viewer percentages. The first-level hash identifies the genres (:female, :male). The second-level hash identifies the age ranges (‘18-24’, ‘25-34’, ‘35-44’, ‘45-54’, ‘55-64’, ‘65-’)



62
# File 'lib/yt/models/channel.rb', line 62

has_viewer_percentages

#views(options = {}) ⇒ Hash<Date, Float>

Returns the views for a range of a days.

Parameters:

  • options (Hash) (defaults to: {})

    the range of days to get the views for.

Options Hash (options):

  • :since (#to_date)

    The first day of the range. Also aliased as :from.

  • :until (#to_date)

    The last day of the range. Also aliased as :to.

Returns:

  • (Hash<Date, Float>)

    the views for a range of a days.



20
# File 'lib/yt/models/channel.rb', line 20

has_report :views

#views_on(date) ⇒ Float

Returns the views for a single day.

Parameters:

  • date (#to_date)

    The single day to get the views for.

Returns:

  • (Float)

    the views for a single day.



20
# File 'lib/yt/models/channel.rb', line 20

has_report :views