Module: Yt::Associations::Earnings

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

Overview

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

Instance Method Summary collapse

Instance Method Details

#earnings(options = {}) ⇒ Hash

Note:

options is aliased as options

Note:

options is aliased as options

Return the estimated earnings 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 a Float, representing estimated earnings in USD.

Parameters:

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

    the range of days to get the earnings 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 estimated earnings by day. Each :key is a Date



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

def earnings(options = {})
  from = options[:since] || options[:from] || 6.days.ago
  to = options[:until] || options[:to] || 2.days.ago
  range = Range.new *[from, to].map(&:to_date)

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

#earnings_on(date) ⇒ Float

Return the estimated earnings for one specific day.

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

Parameters:

  • date (Date or Time or DateTime or String)

    The date to obtain

Returns:

  • (Float)

    The estimated earnings in USD.



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

def earnings_on(date)
  earnings(from: date, to: date).values.first
end