-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
claudiob
committed
May 30, 2014
1 parent
4e4fa50
commit 090e9fc
Showing
9 changed files
with
127 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
require 'yt/config' | ||
require 'yt/models/account' | ||
require 'yt/models/account' | ||
require 'yt/models/content_owner' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'yt/collections/earnings' | ||
|
||
module Yt | ||
module Associations | ||
# Provides the `has_many :earnings` method to YouTube resources, which | ||
# allows to invoke earning-related methods, such as .earnings_on. | ||
# YouTube resources with earning are: channels. | ||
module Earnings | ||
def earnings_on(date) | ||
earnings_within(date, date).values.first | ||
end | ||
|
||
def earnings_since(from) | ||
# NOTE: Today's earnings are not available, and hardly yesterday's | ||
earnings_within from, 1.day.ago | ||
end | ||
|
||
def earnings_within(from, to) | ||
date_range = Range.new *[from, to].map(&:to_date) | ||
Hash[*date_range.flat_map do |date| | ||
[date, (@earnings ||= {})[date] ||= range_earnings(date_range)[date]] | ||
end] | ||
end | ||
|
||
private | ||
|
||
def range_earnings(date_range) | ||
(@range_earnings ||= {})[date_range] ||= earnings.within date_range | ||
end | ||
|
||
def earnings | ||
Collections::Earnings.of self | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'yt/collections/base' | ||
|
||
module Yt | ||
module Collections | ||
class Earnings < Base | ||
|
||
def within(days_range) | ||
@days_range = sorted days_range | ||
Hash[*flat_map{|daily_earning| daily_earning}] | ||
end | ||
|
||
private | ||
|
||
def new_item(data) | ||
# NOTE: could use column headers to be more precise | ||
[Date.iso8601(data.first), data.last] | ||
end | ||
|
||
def list_params | ||
super.tap do |params| | ||
params[:path] = '/youtube/analytics/v1/reports' | ||
params[:params] = {}.tap do |params| | ||
params['ids'] = "contentOwner==#{@auth.id}" | ||
params['filters'] = "channel==#{@parent.id}" | ||
params['start-date'] = @days_range.begin | ||
params['end-date'] = @days_range.end | ||
params['metrics'] = :earnings | ||
params['dimensions'] = :day | ||
end | ||
end | ||
end | ||
|
||
def items_key | ||
'rows' | ||
end | ||
|
||
def sorted(range) | ||
range.min ? range : (range.end..range.begin) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,11 @@ | ||
require 'yt/models/base' | ||
require 'yt/models/authenticable' | ||
|
||
module Yt | ||
module Models | ||
# Provides methods to access a YouTube account. | ||
class Account < Base | ||
has_one :authentication, delegate: [:access_token, :refresh_token, :expires_at] | ||
class Account < Authenticable | ||
has_one :channel, delegate: [:videos, :playlists, :create_playlist, :delete_playlists, :update_playlists] | ||
has_one :user_info, delegate: [:id, :email, :has_verified_email?, :gender, :name, :given_name, :family_name, :profile_url, :avatar_url, :locale, :hd] | ||
|
||
def initialize(options = {}) | ||
@access_token = options[:access_token] | ||
@refresh_token = options[:refresh_token] | ||
@expires_at = options[:expires_at] | ||
@authorization_code = options[:authorization_code] | ||
@redirect_uri = options[:redirect_uri] | ||
@scopes = options[:scopes] | ||
end | ||
|
||
def auth | ||
self | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'yt/models/base' | ||
|
||
module Yt | ||
module Models | ||
class Authenticable < Base | ||
has_one :authentication, delegate: [:access_token, :refresh_token, :expires_at] | ||
|
||
def initialize(options = {}) | ||
@access_token = options[:access_token] | ||
@refresh_token = options[:refresh_token] | ||
@expires_at = options[:expires_at] | ||
@authorization_code = options[:authorization_code] | ||
@redirect_uri = options[:redirect_uri] | ||
@scopes = options[:scopes] | ||
end | ||
|
||
def auth | ||
self | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'yt/models/authenticable' | ||
|
||
module Yt | ||
module Models | ||
# Provides methods to access a YouTube content_owner. | ||
class ContentOwner < Authenticable | ||
attr_reader :id | ||
|
||
def initialize(options = {}) | ||
@id = options[:id] | ||
super options | ||
end | ||
end | ||
end | ||
end |