yt-andrewroth 0.25.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +27 -0
- data/.rspec +3 -0
- data/.travis.yml +9 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +732 -0
- data/Gemfile +4 -0
- data/MIT-LICENSE +20 -0
- data/README.md +489 -0
- data/Rakefile +11 -0
- data/YOUTUBE_IT.md +835 -0
- data/bin/yt +30 -0
- data/gemfiles/Gemfile.activesupport-3.x +4 -0
- data/gemfiles/Gemfile.activesupport-4.x +4 -0
- data/lib/yt.rb +21 -0
- data/lib/yt/actions/base.rb +32 -0
- data/lib/yt/actions/delete.rb +19 -0
- data/lib/yt/actions/delete_all.rb +32 -0
- data/lib/yt/actions/insert.rb +42 -0
- data/lib/yt/actions/list.rb +139 -0
- data/lib/yt/actions/modify.rb +37 -0
- data/lib/yt/actions/patch.rb +19 -0
- data/lib/yt/actions/update.rb +19 -0
- data/lib/yt/associations/has_attribute.rb +55 -0
- data/lib/yt/associations/has_authentication.rb +214 -0
- data/lib/yt/associations/has_many.rb +22 -0
- data/lib/yt/associations/has_one.rb +22 -0
- data/lib/yt/associations/has_reports.rb +320 -0
- data/lib/yt/collections/advertising_options_sets.rb +34 -0
- data/lib/yt/collections/annotations.rb +62 -0
- data/lib/yt/collections/assets.rb +58 -0
- data/lib/yt/collections/authentications.rb +47 -0
- data/lib/yt/collections/base.rb +62 -0
- data/lib/yt/collections/channels.rb +31 -0
- data/lib/yt/collections/claim_histories.rb +34 -0
- data/lib/yt/collections/claims.rb +56 -0
- data/lib/yt/collections/content_details.rb +30 -0
- data/lib/yt/collections/content_owner_details.rb +34 -0
- data/lib/yt/collections/content_owners.rb +32 -0
- data/lib/yt/collections/device_flows.rb +23 -0
- data/lib/yt/collections/file_details.rb +30 -0
- data/lib/yt/collections/ids.rb +27 -0
- data/lib/yt/collections/live_streaming_details.rb +30 -0
- data/lib/yt/collections/ownerships.rb +34 -0
- data/lib/yt/collections/partnered_channels.rb +28 -0
- data/lib/yt/collections/players.rb +30 -0
- data/lib/yt/collections/playlist_items.rb +53 -0
- data/lib/yt/collections/playlists.rb +28 -0
- data/lib/yt/collections/policies.rb +28 -0
- data/lib/yt/collections/ratings.rb +23 -0
- data/lib/yt/collections/references.rb +46 -0
- data/lib/yt/collections/related_playlists.rb +43 -0
- data/lib/yt/collections/reports.rb +161 -0
- data/lib/yt/collections/resources.rb +57 -0
- data/lib/yt/collections/resumable_sessions.rb +51 -0
- data/lib/yt/collections/snippets.rb +27 -0
- data/lib/yt/collections/statistics_sets.rb +30 -0
- data/lib/yt/collections/statuses.rb +27 -0
- data/lib/yt/collections/subscribed_channels.rb +46 -0
- data/lib/yt/collections/subscribers.rb +33 -0
- data/lib/yt/collections/subscriptions.rb +50 -0
- data/lib/yt/collections/user_infos.rb +36 -0
- data/lib/yt/collections/video_categories.rb +35 -0
- data/lib/yt/collections/videos.rb +137 -0
- data/lib/yt/config.rb +54 -0
- data/lib/yt/errors/forbidden.rb +13 -0
- data/lib/yt/errors/missing_auth.rb +81 -0
- data/lib/yt/errors/no_items.rb +13 -0
- data/lib/yt/errors/request_error.rb +74 -0
- data/lib/yt/errors/server_error.rb +13 -0
- data/lib/yt/errors/unauthorized.rb +50 -0
- data/lib/yt/models/account.rb +216 -0
- data/lib/yt/models/advertising_options_set.rb +38 -0
- data/lib/yt/models/annotation.rb +132 -0
- data/lib/yt/models/asset.rb +111 -0
- data/lib/yt/models/asset_metadata.rb +38 -0
- data/lib/yt/models/asset_snippet.rb +46 -0
- data/lib/yt/models/authentication.rb +83 -0
- data/lib/yt/models/base.rb +32 -0
- data/lib/yt/models/channel.rb +302 -0
- data/lib/yt/models/claim.rb +156 -0
- data/lib/yt/models/claim_event.rb +67 -0
- data/lib/yt/models/claim_history.rb +29 -0
- data/lib/yt/models/configuration.rb +70 -0
- data/lib/yt/models/content_detail.rb +65 -0
- data/lib/yt/models/content_owner.rb +48 -0
- data/lib/yt/models/content_owner_detail.rb +18 -0
- data/lib/yt/models/description.rb +58 -0
- data/lib/yt/models/device_flow.rb +16 -0
- data/lib/yt/models/file_detail.rb +21 -0
- data/lib/yt/models/id.rb +9 -0
- data/lib/yt/models/iterator.rb +16 -0
- data/lib/yt/models/live_streaming_detail.rb +23 -0
- data/lib/yt/models/match_policy.rb +34 -0
- data/lib/yt/models/ownership.rb +75 -0
- data/lib/yt/models/player.rb +18 -0
- data/lib/yt/models/playlist.rb +218 -0
- data/lib/yt/models/playlist_item.rb +112 -0
- data/lib/yt/models/policy.rb +36 -0
- data/lib/yt/models/policy_rule.rb +124 -0
- data/lib/yt/models/rating.rb +37 -0
- data/lib/yt/models/reference.rb +172 -0
- data/lib/yt/models/resource.rb +136 -0
- data/lib/yt/models/resumable_session.rb +52 -0
- data/lib/yt/models/right_owner.rb +58 -0
- data/lib/yt/models/snippet.rb +50 -0
- data/lib/yt/models/statistics_set.rb +26 -0
- data/lib/yt/models/status.rb +32 -0
- data/lib/yt/models/subscription.rb +38 -0
- data/lib/yt/models/timestamp.rb +13 -0
- data/lib/yt/models/url.rb +90 -0
- data/lib/yt/models/user_info.rb +26 -0
- data/lib/yt/models/video.rb +630 -0
- data/lib/yt/models/video_category.rb +12 -0
- data/lib/yt/request.rb +278 -0
- data/lib/yt/version.rb +3 -0
- data/spec/collections/claims_spec.rb +30 -0
- data/spec/collections/playlist_items_spec.rb +44 -0
- data/spec/collections/playlists_spec.rb +27 -0
- data/spec/collections/policies_spec.rb +30 -0
- data/spec/collections/references_spec.rb +30 -0
- data/spec/collections/reports_spec.rb +30 -0
- data/spec/collections/subscriptions_spec.rb +25 -0
- data/spec/collections/videos_spec.rb +43 -0
- data/spec/errors/forbidden_spec.rb +10 -0
- data/spec/errors/missing_auth_spec.rb +24 -0
- data/spec/errors/no_items_spec.rb +10 -0
- data/spec/errors/request_error_spec.rb +44 -0
- data/spec/errors/server_error_spec.rb +10 -0
- data/spec/errors/unauthorized_spec.rb +10 -0
- data/spec/models/account_spec.rb +138 -0
- data/spec/models/annotation_spec.rb +180 -0
- data/spec/models/asset_spec.rb +20 -0
- data/spec/models/channel_spec.rb +127 -0
- data/spec/models/claim_event_spec.rb +62 -0
- data/spec/models/claim_history_spec.rb +27 -0
- data/spec/models/claim_spec.rb +211 -0
- data/spec/models/configuration_spec.rb +44 -0
- data/spec/models/content_detail_spec.rb +45 -0
- data/spec/models/content_owner_detail_spec.rb +6 -0
- data/spec/models/description_spec.rb +94 -0
- data/spec/models/file_detail_spec.rb +13 -0
- data/spec/models/live_streaming_detail_spec.rb +6 -0
- data/spec/models/ownership_spec.rb +59 -0
- data/spec/models/player_spec.rb +13 -0
- data/spec/models/playlist_item_spec.rb +120 -0
- data/spec/models/playlist_spec.rb +138 -0
- data/spec/models/policy_rule_spec.rb +63 -0
- data/spec/models/policy_spec.rb +41 -0
- data/spec/models/rating_spec.rb +12 -0
- data/spec/models/reference_spec.rb +249 -0
- data/spec/models/request_spec.rb +163 -0
- data/spec/models/resource_spec.rb +57 -0
- data/spec/models/right_owner_spec.rb +71 -0
- data/spec/models/snippet_spec.rb +13 -0
- data/spec/models/statistics_set_spec.rb +13 -0
- data/spec/models/status_spec.rb +13 -0
- data/spec/models/subscription_spec.rb +30 -0
- data/spec/models/url_spec.rb +78 -0
- data/spec/models/video_category_spec.rb +21 -0
- data/spec/models/video_spec.rb +669 -0
- data/spec/requests/as_account/account_spec.rb +125 -0
- data/spec/requests/as_account/authentications_spec.rb +139 -0
- data/spec/requests/as_account/channel_spec.rb +259 -0
- data/spec/requests/as_account/channels_spec.rb +18 -0
- data/spec/requests/as_account/playlist_item_spec.rb +56 -0
- data/spec/requests/as_account/playlist_spec.rb +244 -0
- data/spec/requests/as_account/resource_spec.rb +18 -0
- data/spec/requests/as_account/thumbnail.jpg +0 -0
- data/spec/requests/as_account/video.mp4 +0 -0
- data/spec/requests/as_account/video_spec.rb +408 -0
- data/spec/requests/as_content_owner/account_spec.rb +25 -0
- data/spec/requests/as_content_owner/advertising_options_set_spec.rb +15 -0
- data/spec/requests/as_content_owner/asset_spec.rb +20 -0
- data/spec/requests/as_content_owner/channel_spec.rb +1934 -0
- data/spec/requests/as_content_owner/claim_history_spec.rb +20 -0
- data/spec/requests/as_content_owner/content_owner_spec.rb +241 -0
- data/spec/requests/as_content_owner/match_policy_spec.rb +17 -0
- data/spec/requests/as_content_owner/ownership_spec.rb +25 -0
- data/spec/requests/as_content_owner/playlist_spec.rb +782 -0
- data/spec/requests/as_content_owner/video_spec.rb +1239 -0
- data/spec/requests/as_server_app/channel_spec.rb +74 -0
- data/spec/requests/as_server_app/playlist_item_spec.rb +30 -0
- data/spec/requests/as_server_app/playlist_spec.rb +53 -0
- data/spec/requests/as_server_app/video_spec.rb +58 -0
- data/spec/requests/as_server_app/videos_spec.rb +40 -0
- data/spec/requests/unauthenticated/video_spec.rb +22 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/support/fail_matcher.rb +15 -0
- data/spec/support/global_hooks.rb +48 -0
- data/yt.gemspec +32 -0
- metadata +416 -0
@@ -0,0 +1,214 @@
|
|
1
|
+
module Yt
|
2
|
+
module Associations
|
3
|
+
# @private
|
4
|
+
# Provides authentication methods to YouTube resources, which allows to
|
5
|
+
# access to content detail set-specific methods like `access_token`.
|
6
|
+
#
|
7
|
+
# YouTube resources with authentication are: {Yt::Models::Account accounts}.
|
8
|
+
module HasAuthentication
|
9
|
+
def has_authentication
|
10
|
+
require 'yt/collections/authentications'
|
11
|
+
require 'yt/collections/device_flows'
|
12
|
+
require 'yt/errors/missing_auth'
|
13
|
+
require 'yt/errors/no_items'
|
14
|
+
require 'yt/errors/unauthorized'
|
15
|
+
|
16
|
+
include Associations::Authenticable
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# @private
|
21
|
+
module Authenticable
|
22
|
+
delegate :access_token, :refresh_token, :expires_at, to: :authentication
|
23
|
+
|
24
|
+
def initialize(options = {})
|
25
|
+
@access_token = options[:access_token]
|
26
|
+
@refresh_token = options[:refresh_token]
|
27
|
+
@device_code = options[:device_code]
|
28
|
+
@expires_at = options[:expires_at]
|
29
|
+
@authorization_code = options[:authorization_code]
|
30
|
+
@redirect_uri = options[:redirect_uri]
|
31
|
+
@force = options[:force]
|
32
|
+
@scopes = options[:scopes]
|
33
|
+
@authentication = options[:authentication]
|
34
|
+
end
|
35
|
+
|
36
|
+
def auth
|
37
|
+
self
|
38
|
+
end
|
39
|
+
|
40
|
+
def authentication
|
41
|
+
@authentication = current_authentication
|
42
|
+
@authentication ||= use_refresh_token! if @refresh_token
|
43
|
+
@authentication ||= use_authorization_code! if @authorization_code
|
44
|
+
@authentication ||= use_device_code! if @device_code
|
45
|
+
@authentication ||= raise_missing_authentication!
|
46
|
+
end
|
47
|
+
|
48
|
+
def authentication_url
|
49
|
+
host = 'accounts.google.com'
|
50
|
+
path = '/o/oauth2/auth'
|
51
|
+
query = authentication_url_params.to_param
|
52
|
+
URI::HTTPS.build(host: host, path: path, query: query).to_s
|
53
|
+
end
|
54
|
+
|
55
|
+
# Obtains a new access token.
|
56
|
+
# Returns true if the new access token is different from the previous one
|
57
|
+
def refreshed_access_token?
|
58
|
+
old_access_token = authentication.access_token
|
59
|
+
@authentication = @access_token = @refreshed_authentications = nil
|
60
|
+
old_access_token != authentication.access_token
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def current_authentication
|
66
|
+
@authentication ||= Yt::Authentication.new current_data if @access_token
|
67
|
+
@authentication unless @authentication.nil? || @authentication.expired?
|
68
|
+
end
|
69
|
+
|
70
|
+
def current_data
|
71
|
+
{}.tap do |data|
|
72
|
+
data['access_token'] = @access_token
|
73
|
+
data['expires_at'] = @expires_at
|
74
|
+
data['refresh_token'] = @refresh_token
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Tries to obtain an access token using the authorization code (which
|
79
|
+
# can only be used once). On failure, raise an error.
|
80
|
+
def use_authorization_code!
|
81
|
+
new_authentications.first!
|
82
|
+
rescue Errors::NoItems => error
|
83
|
+
raise Errors::Unauthorized, error.to_param
|
84
|
+
end
|
85
|
+
|
86
|
+
# Tries to obtain an access token using the refresh token (which can
|
87
|
+
# be used multiple times). On failure, raise an error.
|
88
|
+
def use_refresh_token!
|
89
|
+
refreshed_authentications.first!
|
90
|
+
rescue Errors::NoItems => error
|
91
|
+
raise Errors::Unauthorized, error.to_param
|
92
|
+
end
|
93
|
+
|
94
|
+
# Tries to obtain an access token using the device code (which must be
|
95
|
+
# confirmed by the user with the user_code). On failure, raise an error.
|
96
|
+
def use_device_code!
|
97
|
+
device_code_authentications.first!.tap do |auth|
|
98
|
+
raise Errors::MissingAuth, pending_device_code_message if auth.pending?
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def raise_missing_authentication!
|
103
|
+
error_message = case
|
104
|
+
when @redirect_uri && @scopes then missing_authorization_code_message
|
105
|
+
when @scopes then pending_device_code_message
|
106
|
+
end
|
107
|
+
raise Errors::MissingAuth, error_message
|
108
|
+
end
|
109
|
+
|
110
|
+
def pending_device_code_message
|
111
|
+
@device_flow ||= device_flows.first!
|
112
|
+
@device_code ||= @device_flow.device_code
|
113
|
+
{}.tap do |params|
|
114
|
+
params[:scopes] = @scopes
|
115
|
+
params[:user_code] = @device_flow.user_code
|
116
|
+
params[:verification_url] = @device_flow.verification_url
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def missing_authorization_code_message
|
121
|
+
{}.tap do |params|
|
122
|
+
params[:scopes] = @scopes
|
123
|
+
params[:authentication_url] = authentication_url
|
124
|
+
params[:redirect_uri] = @redirect_uri
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def new_authentications
|
129
|
+
@new_authentications ||= Collections::Authentications.of(self).tap do |auth|
|
130
|
+
auth.auth_params = new_authentication_params
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def refreshed_authentications
|
135
|
+
@refreshed_authentications ||= Collections::Authentications.of(self).tap do |auth|
|
136
|
+
auth.auth_params = refreshed_authentication_params
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def device_code_authentications
|
141
|
+
Collections::Authentications.of(self).tap do |auth|
|
142
|
+
auth.auth_params = device_code_authentication_params
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def device_flows
|
147
|
+
@device_flows ||= Collections::DeviceFlows.of(self).tap do |auth|
|
148
|
+
auth.auth_params = device_flow_params
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def authentication_url_params
|
153
|
+
{}.tap do |params|
|
154
|
+
params[:client_id] = client_id
|
155
|
+
params[:scope] = authentication_scope
|
156
|
+
params[:redirect_uri] = @redirect_uri
|
157
|
+
params[:response_type] = :code
|
158
|
+
params[:access_type] = :offline
|
159
|
+
params[:approval_prompt] = @force ? :force : :auto
|
160
|
+
# params[:include_granted_scopes] = true
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def authentication_scope
|
165
|
+
@scopes.map do |scope|
|
166
|
+
"https://www.googleapis.com/auth/#{scope}"
|
167
|
+
end.join(' ') if @scopes.is_a?(Array)
|
168
|
+
end
|
169
|
+
|
170
|
+
def new_authentication_params
|
171
|
+
{}.tap do |params|
|
172
|
+
params[:client_id] = client_id
|
173
|
+
params[:client_secret] = client_secret
|
174
|
+
params[:code] = @authorization_code
|
175
|
+
params[:redirect_uri] = @redirect_uri
|
176
|
+
params[:grant_type] = :authorization_code
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def refreshed_authentication_params
|
181
|
+
{}.tap do |params|
|
182
|
+
params[:client_id] = client_id
|
183
|
+
params[:client_secret] = client_secret
|
184
|
+
params[:refresh_token] = @refresh_token
|
185
|
+
params[:grant_type] = :refresh_token
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def device_code_authentication_params
|
190
|
+
{}.tap do |params|
|
191
|
+
params[:client_id] = client_id
|
192
|
+
params[:client_secret] = client_secret
|
193
|
+
params[:code] = @device_code
|
194
|
+
params[:grant_type] = 'http://oauth.net/grant_type/device/1.0'
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
def device_flow_params
|
199
|
+
{}.tap do |params|
|
200
|
+
params[:client_id] = client_id
|
201
|
+
params[:scope] = authentication_scope
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def client_id
|
206
|
+
Yt.configuration.client_id
|
207
|
+
end
|
208
|
+
|
209
|
+
def client_secret
|
210
|
+
Yt.configuration.client_secret
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Yt
|
2
|
+
module Associations
|
3
|
+
# @private
|
4
|
+
# Associations are a set of macro-like class methods to express
|
5
|
+
# relationship between YouTube resources like "Channel has many Videos" or
|
6
|
+
# "Account has one Id". They are inspired by ActiveRecord::Associations.
|
7
|
+
module HasMany
|
8
|
+
# @example Adds the +videos+ method to the Channel resource.
|
9
|
+
# class Channel < Resource
|
10
|
+
# has_many :videos
|
11
|
+
# end
|
12
|
+
def has_many(attributes)
|
13
|
+
require 'active_support' # does not load anything by default
|
14
|
+
require 'active_support/core_ext/string/inflections' # for camelize ...
|
15
|
+
require "yt/collections/#{attributes}"
|
16
|
+
collection_name = attributes.to_s.sub(/.*\./, '').camelize.pluralize
|
17
|
+
collection = "Yt::Collections::#{collection_name}".constantize
|
18
|
+
define_memoized_method(attributes) { collection.of self }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Yt
|
2
|
+
module Associations
|
3
|
+
# @private
|
4
|
+
# Associations are a set of macro-like class methods to express
|
5
|
+
# relationship between YouTube resources like "Channel has many Videos" or
|
6
|
+
# "Account has one Id". They are inspired by ActiveRecord::Associations.
|
7
|
+
module HasOne
|
8
|
+
# @example Adds the +status+ method to the Video resource.
|
9
|
+
# class Video < Resource
|
10
|
+
# has_one :status
|
11
|
+
# end
|
12
|
+
def has_one(attribute)
|
13
|
+
require 'yt/associations/has_many'
|
14
|
+
extend Associations::HasMany
|
15
|
+
|
16
|
+
attributes = attribute.to_s.pluralize
|
17
|
+
has_many attributes
|
18
|
+
define_memoized_method(attribute) { send(attributes).first! }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,320 @@
|
|
1
|
+
module Yt
|
2
|
+
module Associations
|
3
|
+
# @private
|
4
|
+
# Provides methods to access the analytics reports of a resource.
|
5
|
+
module HasReports
|
6
|
+
# @!macro [new] reports
|
7
|
+
# Returns the reports for the given metrics grouped by the given dimension.
|
8
|
+
# @!method reports(options = {})
|
9
|
+
# @param [Hash] options the metrics, time-range and dimensions for the reports.
|
10
|
+
# @option options [Array<Symbol>] :only The metrics to generate reports
|
11
|
+
# for.
|
12
|
+
# @option options [Symbol] :by (:day) The dimension to collect metrics
|
13
|
+
# by. Accepted values are: +:day+, +:week+, +:month+.
|
14
|
+
# @option options [#to_date] :since The first day of the time-range.
|
15
|
+
# Also aliased as +:from+.
|
16
|
+
# @option options [#to_date] :until The last day of the time-range.
|
17
|
+
# Also aliased as +:to+.
|
18
|
+
# @return [Hash<Symbol, Hash>] the reports for each metric specified.
|
19
|
+
# @example Get the views and estimated minutes watched by day for last week:
|
20
|
+
# resource.reports only: [:views, :estimated_minutes_watched] since: 1.week.ago, by: :day
|
21
|
+
# # => {views: {Wed, 8 May 2014 => 12, Thu, 9 May 2014 => 34, …}, estimated_minutes_watched: {Wed, 8 May 2014 => 9, Thu, 9 May 2014 => 6, …}}
|
22
|
+
|
23
|
+
# @!macro [new] report
|
24
|
+
# Returns the $1 grouped by the given dimension.
|
25
|
+
# @!method $1(options = {})
|
26
|
+
# @param [Hash] options the time-range and dimensions for the $1.
|
27
|
+
# @option options [#to_date] :since The first day of the time-range.
|
28
|
+
# Also aliased as +:from+.
|
29
|
+
# @option options [#to_date] :until The last day of the time-range.
|
30
|
+
# Also aliased as +:to+.
|
31
|
+
|
32
|
+
# @!macro [new] report_with_day
|
33
|
+
# @return [Hash<Date, $2>] if grouped by day, the $1
|
34
|
+
# for each day in the time-range.
|
35
|
+
# @example Get the $1 for each day of last week:
|
36
|
+
# resource.$1 since: 2.weeks.ago, until: 1.week.ago, by: :day
|
37
|
+
# # => {Wed, 8 May 2014 => 12.0, Thu, 9 May 2014 => 34.0, …}
|
38
|
+
# @return [Hash<Range<Date, Date>, $2>] if grouped by month, the $1
|
39
|
+
# for each month in the time-range.
|
40
|
+
# @example Get the $1 for this and last month:
|
41
|
+
# resource.$1 since: 1.month.ago, by: :month
|
42
|
+
# # => {Wed, 01 Apr 2014..Thu, 30 Apr 2014 => 12.0, Fri, 01 May 2014..Sun, 31 May 2014 => 34.0, …}
|
43
|
+
# @return [Hash<Range<Date, Date>, $2>] if grouped by week, the $1
|
44
|
+
# for each week in the time-range.
|
45
|
+
# @example Get the $1 for this and last week:
|
46
|
+
# resource.$1 since: 1.week.ago, by: :week
|
47
|
+
# # => {Wed, 01 Apr 2014..Tue, 07 Apr 2014 => 20.0, Wed, 08 Apr 2014..Tue, 14 Apr 2014 => 13.0, …}
|
48
|
+
# @macro report
|
49
|
+
|
50
|
+
# @!macro [new] report_with_range
|
51
|
+
# @return [Hash<Symbol, $2>] if grouped by range, the $1
|
52
|
+
# for the entire time-range (under the key +:total+).
|
53
|
+
# @example Get the $1 for the whole last week:
|
54
|
+
# resource.$1 since: 2.weeks.ago, until: 1.week.ago, by: :range
|
55
|
+
# # => {total: 564.0}
|
56
|
+
|
57
|
+
# @!macro [new] report_with_country
|
58
|
+
# @option options [<String, Hash>] :in The country to limit the $1
|
59
|
+
# to. Can either be the two-letter ISO-3166-1 code of a country, such
|
60
|
+
# as +"US"+, or a Hash with the code in the +:country+ key, such
|
61
|
+
# as +{country: "US"}+.
|
62
|
+
# @example Get the $1 for the whole last week in France only:
|
63
|
+
# resource.$1 since: 2.weeks.ago, until: 1.week.ago, by: :range, in: 'FR'
|
64
|
+
# # => {total: 44.0}
|
65
|
+
|
66
|
+
# @!macro [new] report_with_country_and_state
|
67
|
+
# @option options [<String, Hash>] :in The location to limit the $1
|
68
|
+
# to. Can either be the two-letter ISO-3166-1 code of a country, such
|
69
|
+
# as +"US"+, or a Hash that either contains the +:country+ key, such
|
70
|
+
# as +{country: "US"}+ or the +:state+ key, such as +{state: "TX"}+.
|
71
|
+
# Note that YouTube API only provides data for US states.
|
72
|
+
# @example Get the $1 for the whole last week in Texas only:
|
73
|
+
# resource.$1 since: 2.weeks.ago, until: 1.week.ago, by: :range, in: {state: 'TX'}
|
74
|
+
# # => {total: 19.0}
|
75
|
+
|
76
|
+
# @!macro [new] report_by_day
|
77
|
+
# @option options [Symbol] :by (:day) The dimension to collect $1 by.
|
78
|
+
# Accepted values are: +:day+, +:week+, +:month+.
|
79
|
+
# @macro report_with_day
|
80
|
+
|
81
|
+
# @!macro [new] report_by_day_and_country
|
82
|
+
# @option options [Symbol] :by (:day) The dimension to collect $1 by.
|
83
|
+
# Accepted values are: +:day+, +:week+, +:month+, :+range+.
|
84
|
+
# @macro report_with_day
|
85
|
+
# @macro report_with_range
|
86
|
+
# @macro report_with_country
|
87
|
+
|
88
|
+
# @!macro [new] report_by_day_and_state
|
89
|
+
# @option options [Symbol] :by (:day) The dimension to collect $1 by.
|
90
|
+
# Accepted values are: +:day+, +:week+, +:month+, :+range+.
|
91
|
+
# @macro report_with_day
|
92
|
+
# @macro report_with_range
|
93
|
+
# @macro report_with_country_and_state
|
94
|
+
|
95
|
+
# @!macro [new] report_with_video_dimensions
|
96
|
+
# @return [Hash<Symbol, $2>] if grouped by playback location, the
|
97
|
+
# $1 for each traffic playback location.
|
98
|
+
# @example Get yesterday’s $1 grouped by playback location:
|
99
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago, by: :playback_location
|
100
|
+
# # => {embedded: 53.0, watch: 467.0, …}
|
101
|
+
# @return [Hash<Yt::Video, $2>] if grouped by related video, the
|
102
|
+
# $1 for each related video.
|
103
|
+
# @example Get yesterday’s $1 by related video, eager-loading the snippet of each video::
|
104
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago, by: :related_video, includes: [:snippet]
|
105
|
+
# # => {#<Yt::Video @id=…> => 33.0, #<Yt::Video @id=…> => 12.0, …}
|
106
|
+
# @return [Hash<Symbol, $2>] if grouped by device type, the
|
107
|
+
# $1 for each device type.
|
108
|
+
# @example Get yesterday’s $1 by search term:
|
109
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago, by: :search_term
|
110
|
+
# # => {"fullscreen" => 33.0, "good music" => 12.0, …}
|
111
|
+
# @return [Hash<String, $2>] if grouped by search term, the
|
112
|
+
# $1 for each search term that led viewers to the content.
|
113
|
+
# @example Get yesterday’s $1 by URL that referred to the resource:
|
114
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago, by: :referrer
|
115
|
+
# # => {"Google Search" => 33.0, "ytimg.com" => 12.0, …}
|
116
|
+
# @return [Hash<String, $2>] if grouped by search term, the
|
117
|
+
# $1 for each search term that led viewers to the content.
|
118
|
+
# @example Get yesterday’s $1 by device type:
|
119
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago, by: :device_type
|
120
|
+
# # => {mobile: 133.0, tv: 412.0, …}
|
121
|
+
# @return [Hash<Symbol, $2>] if grouped by traffic source, the
|
122
|
+
# $1 for each traffic source.
|
123
|
+
# @example Get yesterday’s $1 by traffic source:
|
124
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago, by: :traffic_source
|
125
|
+
# # => {advertising: 543.0, playlist: 92.0, …}
|
126
|
+
# @macro report_with_day
|
127
|
+
# @macro report_with_range
|
128
|
+
|
129
|
+
# @!macro [new] report_by_video_dimensions
|
130
|
+
# @option options [Symbol] :by (:day) The dimension to collect $1 by.
|
131
|
+
# Accepted values are: +:day+, +:week+, +:month+, +:range+,
|
132
|
+
# +:traffic_source+,+:search_term+, +:playback_location+,
|
133
|
+
# +:related_video+, +:embedded_player_location+.
|
134
|
+
# @option options [Array<Symbol>] :includes ([:id]) if grouped by
|
135
|
+
# related video, the parts of each video to eager-load. Accepted
|
136
|
+
# values are: +:id+, +:snippet+, +:status+, +:statistics+,
|
137
|
+
# +:content_details+.
|
138
|
+
# @return [Hash<Symbol, $2>] if grouped by embedded player location,
|
139
|
+
# the $1 for each embedded player location.
|
140
|
+
# @example Get yesterday’s $1 by embedded player location:
|
141
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago, by: :embedded_player_location
|
142
|
+
# # => {"fullscreen.net" => 92.0, "yahoo.com" => 14.0, …}
|
143
|
+
# @macro report_with_video_dimensions
|
144
|
+
# @macro report_with_country_and_state
|
145
|
+
|
146
|
+
# @!macro [new] report_with_channel_dimensions
|
147
|
+
# @option options [Array<Symbol>] :includes ([:id]) if grouped by
|
148
|
+
# video, related video, or playlist, the parts of each video or
|
149
|
+
# playlist to eager-load. Accepted values are: +:id+, +:snippet+,
|
150
|
+
# +:status+, +:statistics+, +:content_details+.
|
151
|
+
# @return [Hash<Yt::Video, $2>] if grouped by video, the
|
152
|
+
# $1 for each video.
|
153
|
+
# @example Get yesterday’s $1 by video, eager-loading the status and statistics of each video:
|
154
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago, by: :video, includes: [:status, :statistics]
|
155
|
+
# # => {#<Yt::Video @id=…> => 33.0, #<Yt::Video @id=…> => 12.0, …}
|
156
|
+
# @return [Hash<Yt::Playlist, $2>] if grouped by playlist, the
|
157
|
+
# $1 for each playlist.
|
158
|
+
# @example Get yesterday’s $1 by playlist:
|
159
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago, by: :playlist
|
160
|
+
# # => {#<Yt::Playlist @id=…> => 33.0, #<Yt::Playlist @id=…> => 12.0, …}
|
161
|
+
# @macro report_with_video_dimensions
|
162
|
+
|
163
|
+
# @!macro [new] report_by_channel_dimensions
|
164
|
+
# @option options [Symbol] :by (:day) The dimension to collect $1 by.
|
165
|
+
# Accepted values are: +:day+, +:week+, +:month+, +:range+,
|
166
|
+
# +:traffic_source+, +:search_term+, +:playback_location+, +:video+,
|
167
|
+
# +:related_video+, +:playlist+, +:embedded_player_location+.
|
168
|
+
# @return [Hash<Symbol, $2>] if grouped by embedded player location,
|
169
|
+
# the $1 for each embedded player location.
|
170
|
+
# @example Get yesterday’s $1 by embedded player location:
|
171
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago, by: :embedded_player_location
|
172
|
+
# # => {"fullscreen.net" => 92.0, "yahoo.com" => 14.0, …}
|
173
|
+
# @macro report_with_channel_dimensions
|
174
|
+
# @macro report_with_country_and_state
|
175
|
+
|
176
|
+
# @!macro [new] report_by_playlist_dimensions
|
177
|
+
# @option options [Symbol] :by (:day) The dimension to collect $1 by.
|
178
|
+
# Accepted values are: +:day+, +:week+, +:month+, +:range+,
|
179
|
+
# +:traffic_source+, +:playback_location+, +:related_video+, +:video+,
|
180
|
+
# +:playlist+.
|
181
|
+
# @macro report_with_channel_dimensions
|
182
|
+
# @macro report_with_country_and_state
|
183
|
+
|
184
|
+
# @!macro [new] report_by_gender_and_age_group
|
185
|
+
# @option options [Symbol] :by (:gender_age_group) The dimension to
|
186
|
+
# show viewer percentage by.
|
187
|
+
# Accepted values are: +:gender+, +:age_group+, +:gender_age_group+.
|
188
|
+
# @return [Hash<Symbol, $2>] if grouped by gender, the
|
189
|
+
# viewer percentage by gender.
|
190
|
+
# @example Get yesterday’s viewer percentage by gender:
|
191
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago, by: :gender
|
192
|
+
# # => {female: 53.0, male: 47.0}
|
193
|
+
# @return [Hash<String, $2>] if grouped by age group, the
|
194
|
+
# viewer percentage by age group.
|
195
|
+
# @example Get yesterday’s $1 grouped by age group:
|
196
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago, by: :age_group
|
197
|
+
# # => {"18-24" => 4.54, "35-24" => 12.31, "45-34" => 8.92, …}
|
198
|
+
# @return [Hash<Symbol, [Hash<String, $2>]>] if grouped by gender
|
199
|
+
# and age group, the viewer percentage by gender/age group.
|
200
|
+
# @example Get yesterday’s $1 by gender and age group:
|
201
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago
|
202
|
+
# # => {female: {"18-24" => 12.12, "25-34" => 16.16, …}, male:…}
|
203
|
+
# @example Get yesterday’s $1 by gender and age group in France only:
|
204
|
+
# resource.$1 since: 1.day.ago, until: 1.day.ago, in: 'FR'
|
205
|
+
# # => {female: {"18-24" => 16.12, "25-34" => 13.16, …}, male:…}
|
206
|
+
# @macro report
|
207
|
+
# @macro report_with_country_and_state
|
208
|
+
|
209
|
+
# Defines two public instance methods to access the reports of a
|
210
|
+
# resource for a specific metric.
|
211
|
+
# @param [Symbol] metric the metric to access the reports of.
|
212
|
+
# @param [Class] type The class to cast the returned values to.
|
213
|
+
# @example Adds +comments+ and +comments_on+ on a Channel resource.
|
214
|
+
# class Channel < Resource
|
215
|
+
# has_report :comments, Integer
|
216
|
+
# end
|
217
|
+
def has_report(metric, type)
|
218
|
+
require 'yt/collections/reports'
|
219
|
+
|
220
|
+
define_metric_on_method metric
|
221
|
+
define_metric_method metric
|
222
|
+
define_reports_method metric, type
|
223
|
+
define_range_metric_method metric
|
224
|
+
define_all_metric_method metric, type
|
225
|
+
end
|
226
|
+
|
227
|
+
private
|
228
|
+
|
229
|
+
def define_metric_on_method(metric)
|
230
|
+
define_method "#{metric}_on" do |date|
|
231
|
+
send(metric, from: date, to: date, by: :day).values.first
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
def define_reports_method(metric, type)
|
236
|
+
(@metrics ||= {})[metric] = type
|
237
|
+
define_method :reports do |options = {}|
|
238
|
+
from = options[:since] || options[:from] || (options[:by].in?([:day, :week, :month]) ? 5.days.ago : '2005-02-01')
|
239
|
+
to = options[:until] || options[:to] || Date.today
|
240
|
+
location = options[:in]
|
241
|
+
country = location.is_a?(Hash) ? location[:country] : location
|
242
|
+
state = location[:state] if location.is_a?(Hash)
|
243
|
+
dimension = options[:by] || (metric == :viewer_percentage ? :gender_age_group : :range)
|
244
|
+
videos = options[:videos]
|
245
|
+
if dimension == :month
|
246
|
+
from = from.to_date.beginning_of_month
|
247
|
+
to = to.to_date.beginning_of_month
|
248
|
+
end
|
249
|
+
date_range = Range.new *[from, to].map(&:to_date)
|
250
|
+
|
251
|
+
only = options.fetch :only, []
|
252
|
+
reports = Collections::Reports.of(self).tap do |reports|
|
253
|
+
reports.metrics = self.class.instance_variable_get(:@metrics).select{|k, v| k.in? only}
|
254
|
+
end
|
255
|
+
reports.within date_range, country, state, dimension, videos
|
256
|
+
end unless defined?(reports)
|
257
|
+
end
|
258
|
+
|
259
|
+
def define_metric_method(metric)
|
260
|
+
define_method metric do |options = {}|
|
261
|
+
from = options[:since] || options[:from] || (options[:by].in?([:day, :week, :month]) ? 5.days.ago : '2005-02-01')
|
262
|
+
to = options[:until] || options[:to] || Date.today
|
263
|
+
location = options[:in]
|
264
|
+
country = location.is_a?(Hash) ? location[:country] : location
|
265
|
+
state = location[:state] if location.is_a?(Hash)
|
266
|
+
dimension = options[:by] || (metric == :viewer_percentage ? :gender_age_group : :range)
|
267
|
+
videos = options[:videos]
|
268
|
+
if dimension == :month
|
269
|
+
from = from.to_date.beginning_of_month
|
270
|
+
to = to.to_date.beginning_of_month
|
271
|
+
end
|
272
|
+
range = Range.new *[from, to].map(&:to_date)
|
273
|
+
|
274
|
+
ivar = instance_variable_get "@#{metric}_#{dimension}_#{country}_#{state}"
|
275
|
+
instance_variable_set "@#{metric}_#{dimension}_#{country}_#{state}", ivar || {}
|
276
|
+
results = case dimension
|
277
|
+
when :day
|
278
|
+
Hash[*range.flat_map do |date|
|
279
|
+
[date, instance_variable_get("@#{metric}_#{dimension}_#{country}_#{state}")[date] ||= send("range_#{metric}", range, dimension, country, state, videos)[date]]
|
280
|
+
end]
|
281
|
+
else
|
282
|
+
instance_variable_get("@#{metric}_#{dimension}_#{country}_#{state}")[range] ||= send("range_#{metric}", range, dimension, country, state, videos)
|
283
|
+
end
|
284
|
+
lookup_class = case options[:by]
|
285
|
+
when :video, :related_video then Yt::Collections::Videos
|
286
|
+
when :playlist then Yt::Collections::Playlists
|
287
|
+
end
|
288
|
+
if lookup_class
|
289
|
+
includes = options.fetch(:includes, [:id]).join(',').camelize(:lower)
|
290
|
+
items = lookup_class.new(auth: auth).where(part: includes, id: results.keys.join(','))
|
291
|
+
results.transform_keys{|id| items.find{|item| item.id == id}}.reject{|k, _| k.nil?}
|
292
|
+
else
|
293
|
+
results
|
294
|
+
end
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
def define_range_metric_method(metric)
|
299
|
+
define_method "range_#{metric}" do |date_range, dimension, country, state, videos|
|
300
|
+
ivar = instance_variable_get "@range_#{metric}_#{dimension}_#{country}_#{state}"
|
301
|
+
instance_variable_set "@range_#{metric}_#{dimension}_#{country}_#{state}", ivar || {}
|
302
|
+
instance_variable_get("@range_#{metric}_#{dimension}_#{country}_#{state}")[date_range] ||= send("all_#{metric}").within date_range, country, state, dimension, videos
|
303
|
+
end
|
304
|
+
private "range_#{metric}"
|
305
|
+
end
|
306
|
+
|
307
|
+
def define_all_metric_method(metric, type)
|
308
|
+
define_method "all_#{metric}" do
|
309
|
+
# @note Asking for the "earnings" metric of a day in which a channel
|
310
|
+
# made 0 USD returns the wrong "nil". But adding to the request the
|
311
|
+
# "estimatedMinutesWatched" metric returns the correct value 0.
|
312
|
+
metrics = {metric => type}
|
313
|
+
metrics[:estimated_minutes_watched] = Integer if metric == :earnings
|
314
|
+
Collections::Reports.of(self).tap{|reports| reports.metrics = metrics}
|
315
|
+
end
|
316
|
+
private "all_#{metric}"
|
317
|
+
end
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|