Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from Fullscreen/add-viewer-percentages
Add viewer percentages
- Loading branch information
Showing
17 changed files
with
157 additions
and
13 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,3 +1,4 @@ | ||
--no-private | ||
lib/yt/associations/has_reports.rb | ||
lib/yt/associations/has_viewer_percentages.rb | ||
lib/**/*.rb |
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,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
yt (0.7.9) | ||
yt (0.7.10) | ||
activesupport | ||
|
||
GEM | ||
|
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
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,39 @@ | ||
module Yt | ||
module Associations | ||
# Provides methods to access the viewer percentage reports of a resource. | ||
# | ||
# YouTube resources with viewer percentage reports are: | ||
# {Yt::Models::Channel channels} and {Yt::Models::Channel videos}. | ||
module HasViewerPercentages | ||
# @!macro has_viewer_percentages | ||
# @!method viewer_percentages | ||
# @return [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-') | ||
# @example Return the % of male viewers of a channel older than 64 | ||
# channel.viewer_percentages[:male]['65-'] #=> 12.02 | ||
|
||
# Defines a public instance methods to access the viewer percentages of | ||
# a resource for a specific metric. | ||
# @example Adds +viewer_percentages+ on a Channel resource. | ||
# class Channel < Resource | ||
# has_viewer_percentages | ||
# end | ||
def has_viewer_percentages | ||
require 'yt/collections/viewer_percentages' | ||
|
||
define_viewer_percentages_method | ||
end | ||
|
||
private | ||
|
||
def define_viewer_percentages_method | ||
# @todo: add options like start and end date | ||
define_method :viewer_percentages do | ||
@viewer_percentages ||= Collections::ViewerPercentages.of(self).all | ||
end | ||
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,46 @@ | ||
require 'yt/collections/base' | ||
|
||
module Yt | ||
module Collections | ||
class ViewerPercentages < Base | ||
|
||
def all | ||
Hash.new{|h,k| h[k] = Hash.new(0.0)}.tap do |hash| | ||
each{|item| hash[item.gender][item.age_range] = item.value} | ||
end | ||
end | ||
|
||
private | ||
|
||
# @note could use column headers to be more precise | ||
def new_item(data) | ||
Struct.new(:gender, :age_range, :value).new.tap do |item| | ||
item.gender = data.first.to_sym | ||
item.age_range = data.second.gsub /^age/, '' | ||
item.value = data.last | ||
end | ||
end | ||
|
||
def list_params | ||
super.tap do |params| | ||
params[:path] = '/youtube/analytics/v1/reports' | ||
params[:params] = @parent.reports_params.merge reports_params | ||
end | ||
end | ||
|
||
def reports_params | ||
{}.tap do |params| | ||
params['start-date'] = 3.months.ago.to_date | ||
params['end-date'] = Date.today.to_date | ||
params['metrics'] = :viewerPercentage | ||
params['dimensions'] = 'gender,ageGroup' | ||
params['sort'] = 'gender,ageGroup' | ||
end | ||
end | ||
|
||
def items_key | ||
'rows' | ||
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Yt | ||
VERSION = '0.7.9' | ||
VERSION = '0.7.10' | ||
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
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