Module: Yt::Associations::Views

Included in:
Models::Channel
Defined in:
lib/yt/associations/views.rb

Overview

Provides the ‘has_many :views` method to YouTube resources, which allows to invoke view-related methods, such as .views. YouTube resources with views are: channels.

Instance Method Summary collapse

Instance Method Details

#views(options = {}) ⇒ Hash

Note:

options is aliased as options

Note:

options is aliased as options

Return the view count for a range of days.

of the days range. If String, must be Date-parseable. of the days range. If String, must be Date-parseable. and each :value is an Integer or nil, representing the number of views.

Parameters:

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

    the range of days to get the views for.

Options Hash (options):

  • :since (Date or Time or DateTime or String)

    The start

  • :until (Date or Time or DateTime or String)

    The end

Returns:

  • (Hash)

    The view count by day. Each :key is a Date



31
32
33
34
35
36
37
38
39
# File 'lib/yt/associations/views.rb', line 31

def views(options = {})
  from = options[:since] || options[:from] || 5.days.ago
  to = options[:until] || options[:to] || 1.day.ago
  range = Range.new *[from, to].map(&:to_date)

  Hash[*range.flat_map do |date|
    [date, (@views ||= {})[date] ||= range_views(range)[date]]
  end]
end

#views_on(date) ⇒ Integer or Nil

Return the view count for one specific day.

the estimated views for. If String, must be Date-parseable.

Parameters:

  • date (Date or Time or DateTime or String)

    The date to obtain

Returns:

  • (Integer or Nil)

    The estimated views.



15
16
17
# File 'lib/yt/associations/views.rb', line 15

def views_on(date)
  views(from: date, to: date).values.first
end