Class: Yt::Collections::Reports

Inherits:
Base
  • Object
show all
Defined in:
lib/yt/collections/reports.rb

Constant Summary collapse

DIMENSIONS =
Hash.new({name: 'day', parse: ->(day) {Date.iso8601 day} }).tap do |hash|
  hash[:traffic_source] = {name: 'insightTrafficSourceType', parse: ->(type) {TRAFFIC_SOURCES.key type} }
  hash[:playback_location] = {name: 'insightPlaybackLocationType', parse: ->(type) {PLAYBACK_LOCATIONS.key type} }
  hash[:embedded_player_location] = {name: 'insightPlaybackLocationDetail', parse: ->(url) {url} }
  hash[:related_video] = {name: 'insightTrafficSourceDetail', parse: ->(video_id) { Yt::Video.new id: video_id, auth: @auth } }
  hash[:video] = {name: 'video', parse: ->(video_id) { Yt::Video.new id: video_id, auth: @auth } }
  hash[:playlist] = {name: 'playlist', parse: ->(playlist_id) { Yt::Playlist.new id: playlist_id, auth: @auth } }
  hash[:device_type] = {name: 'deviceType', parse: ->(type) { type.downcase.to_sym } }
  hash[:gender_age_group] = {name: 'gender,ageGroup', parse: ->(gender) { gender.downcase.to_sym }}
  hash[:gender] = {name: 'gender', parse: ->(gender) { gender.downcase.to_sym } }
  hash[:age_group] = {name: 'ageGroup', parse: ->(age_group) { age_group[3..-1] } }
end
TRAFFIC_SOURCES =
Note:

EXT_APP is also returned but it’s not in the documentation above!

{
  advertising: 'ADVERTISING',
  annotation: 'ANNOTATION',
  external_app: 'EXT_APP',
  external_url: 'EXT_URL',
  embedded: 'NO_LINK_EMBEDDED',
  other: 'NO_LINK_OTHER',
  playlist: 'PLAYLIST',
  promoted: 'PROMOTED',
  related_video: 'RELATED_VIDEO',
  subscriber: 'SUBSCRIBER',
  channel: 'YT_CHANNEL',
  other_page: 'YT_OTHER_PAGE',
  search: 'YT_SEARCH',
}
PLAYBACK_LOCATIONS =
{
  channel: 'CHANNEL',
  watch: 'WATCH',
  embedded: 'EMBEDDED',
  other: 'YT_OTHER',
  external_app: 'EXTERNAL_APP',
  mobile: 'MOBILE' # only present for data < September 10, 2013
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#includes, #initialize, of, #where

Methods included from Actions::List

#first!

Constructor Details

This class inherits a constructor from Yt::Collections::Base

Instance Attribute Details

#metric=(value) ⇒ Object (writeonly)

Sets the attribute metric



47
48
49
# File 'lib/yt/collections/reports.rb', line 47

def metric=(value)
  @metric = value
end

Instance Method Details

#within(days_range, dimension, try_again = true) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/yt/collections/reports.rb', line 49

def within(days_range, dimension, try_again = true)
  @days_range = days_range
  @dimension = dimension
  if dimension == :gender_age_group # array of array
    Hash.new{|h,k| h[k] = Hash.new 0.0}.tap do |hash|
      each{|gender, age_group, value| hash[gender][age_group[3..-1]] = value}
    end
  else
    Hash[*flat_map{|value| [value.first, value.last]}]
  end
# NOTE: Once in a while, YouTube responds with 400 Error and the message
# "Invalid query. Query did not conform to the expectations."; in this
# case running the same query after one second fixes the issue. This is
# not documented by YouTube and hardly testable, but trying again the
# same query is a workaround that works and can hardly cause any damage.
# Similarly, once in while YouTube responds with a random 503 error.
rescue Yt::Error => e
  try_again && rescue?(e) ? sleep(3) && within(days_range, dimension, false) : raise
end