yt-andrewroth 0.25.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. data/.gitignore +27 -0
  2. data/.rspec +3 -0
  3. data/.travis.yml +9 -0
  4. data/.yardopts +5 -0
  5. data/CHANGELOG.md +732 -0
  6. data/Gemfile +4 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +489 -0
  9. data/Rakefile +11 -0
  10. data/YOUTUBE_IT.md +835 -0
  11. data/bin/yt +30 -0
  12. data/gemfiles/Gemfile.activesupport-3.x +4 -0
  13. data/gemfiles/Gemfile.activesupport-4.x +4 -0
  14. data/lib/yt.rb +21 -0
  15. data/lib/yt/actions/base.rb +32 -0
  16. data/lib/yt/actions/delete.rb +19 -0
  17. data/lib/yt/actions/delete_all.rb +32 -0
  18. data/lib/yt/actions/insert.rb +42 -0
  19. data/lib/yt/actions/list.rb +139 -0
  20. data/lib/yt/actions/modify.rb +37 -0
  21. data/lib/yt/actions/patch.rb +19 -0
  22. data/lib/yt/actions/update.rb +19 -0
  23. data/lib/yt/associations/has_attribute.rb +55 -0
  24. data/lib/yt/associations/has_authentication.rb +214 -0
  25. data/lib/yt/associations/has_many.rb +22 -0
  26. data/lib/yt/associations/has_one.rb +22 -0
  27. data/lib/yt/associations/has_reports.rb +320 -0
  28. data/lib/yt/collections/advertising_options_sets.rb +34 -0
  29. data/lib/yt/collections/annotations.rb +62 -0
  30. data/lib/yt/collections/assets.rb +58 -0
  31. data/lib/yt/collections/authentications.rb +47 -0
  32. data/lib/yt/collections/base.rb +62 -0
  33. data/lib/yt/collections/channels.rb +31 -0
  34. data/lib/yt/collections/claim_histories.rb +34 -0
  35. data/lib/yt/collections/claims.rb +56 -0
  36. data/lib/yt/collections/content_details.rb +30 -0
  37. data/lib/yt/collections/content_owner_details.rb +34 -0
  38. data/lib/yt/collections/content_owners.rb +32 -0
  39. data/lib/yt/collections/device_flows.rb +23 -0
  40. data/lib/yt/collections/file_details.rb +30 -0
  41. data/lib/yt/collections/ids.rb +27 -0
  42. data/lib/yt/collections/live_streaming_details.rb +30 -0
  43. data/lib/yt/collections/ownerships.rb +34 -0
  44. data/lib/yt/collections/partnered_channels.rb +28 -0
  45. data/lib/yt/collections/players.rb +30 -0
  46. data/lib/yt/collections/playlist_items.rb +53 -0
  47. data/lib/yt/collections/playlists.rb +28 -0
  48. data/lib/yt/collections/policies.rb +28 -0
  49. data/lib/yt/collections/ratings.rb +23 -0
  50. data/lib/yt/collections/references.rb +46 -0
  51. data/lib/yt/collections/related_playlists.rb +43 -0
  52. data/lib/yt/collections/reports.rb +161 -0
  53. data/lib/yt/collections/resources.rb +57 -0
  54. data/lib/yt/collections/resumable_sessions.rb +51 -0
  55. data/lib/yt/collections/snippets.rb +27 -0
  56. data/lib/yt/collections/statistics_sets.rb +30 -0
  57. data/lib/yt/collections/statuses.rb +27 -0
  58. data/lib/yt/collections/subscribed_channels.rb +46 -0
  59. data/lib/yt/collections/subscribers.rb +33 -0
  60. data/lib/yt/collections/subscriptions.rb +50 -0
  61. data/lib/yt/collections/user_infos.rb +36 -0
  62. data/lib/yt/collections/video_categories.rb +35 -0
  63. data/lib/yt/collections/videos.rb +137 -0
  64. data/lib/yt/config.rb +54 -0
  65. data/lib/yt/errors/forbidden.rb +13 -0
  66. data/lib/yt/errors/missing_auth.rb +81 -0
  67. data/lib/yt/errors/no_items.rb +13 -0
  68. data/lib/yt/errors/request_error.rb +74 -0
  69. data/lib/yt/errors/server_error.rb +13 -0
  70. data/lib/yt/errors/unauthorized.rb +50 -0
  71. data/lib/yt/models/account.rb +216 -0
  72. data/lib/yt/models/advertising_options_set.rb +38 -0
  73. data/lib/yt/models/annotation.rb +132 -0
  74. data/lib/yt/models/asset.rb +111 -0
  75. data/lib/yt/models/asset_metadata.rb +38 -0
  76. data/lib/yt/models/asset_snippet.rb +46 -0
  77. data/lib/yt/models/authentication.rb +83 -0
  78. data/lib/yt/models/base.rb +32 -0
  79. data/lib/yt/models/channel.rb +302 -0
  80. data/lib/yt/models/claim.rb +156 -0
  81. data/lib/yt/models/claim_event.rb +67 -0
  82. data/lib/yt/models/claim_history.rb +29 -0
  83. data/lib/yt/models/configuration.rb +70 -0
  84. data/lib/yt/models/content_detail.rb +65 -0
  85. data/lib/yt/models/content_owner.rb +48 -0
  86. data/lib/yt/models/content_owner_detail.rb +18 -0
  87. data/lib/yt/models/description.rb +58 -0
  88. data/lib/yt/models/device_flow.rb +16 -0
  89. data/lib/yt/models/file_detail.rb +21 -0
  90. data/lib/yt/models/id.rb +9 -0
  91. data/lib/yt/models/iterator.rb +16 -0
  92. data/lib/yt/models/live_streaming_detail.rb +23 -0
  93. data/lib/yt/models/match_policy.rb +34 -0
  94. data/lib/yt/models/ownership.rb +75 -0
  95. data/lib/yt/models/player.rb +18 -0
  96. data/lib/yt/models/playlist.rb +218 -0
  97. data/lib/yt/models/playlist_item.rb +112 -0
  98. data/lib/yt/models/policy.rb +36 -0
  99. data/lib/yt/models/policy_rule.rb +124 -0
  100. data/lib/yt/models/rating.rb +37 -0
  101. data/lib/yt/models/reference.rb +172 -0
  102. data/lib/yt/models/resource.rb +136 -0
  103. data/lib/yt/models/resumable_session.rb +52 -0
  104. data/lib/yt/models/right_owner.rb +58 -0
  105. data/lib/yt/models/snippet.rb +50 -0
  106. data/lib/yt/models/statistics_set.rb +26 -0
  107. data/lib/yt/models/status.rb +32 -0
  108. data/lib/yt/models/subscription.rb +38 -0
  109. data/lib/yt/models/timestamp.rb +13 -0
  110. data/lib/yt/models/url.rb +90 -0
  111. data/lib/yt/models/user_info.rb +26 -0
  112. data/lib/yt/models/video.rb +630 -0
  113. data/lib/yt/models/video_category.rb +12 -0
  114. data/lib/yt/request.rb +278 -0
  115. data/lib/yt/version.rb +3 -0
  116. data/spec/collections/claims_spec.rb +30 -0
  117. data/spec/collections/playlist_items_spec.rb +44 -0
  118. data/spec/collections/playlists_spec.rb +27 -0
  119. data/spec/collections/policies_spec.rb +30 -0
  120. data/spec/collections/references_spec.rb +30 -0
  121. data/spec/collections/reports_spec.rb +30 -0
  122. data/spec/collections/subscriptions_spec.rb +25 -0
  123. data/spec/collections/videos_spec.rb +43 -0
  124. data/spec/errors/forbidden_spec.rb +10 -0
  125. data/spec/errors/missing_auth_spec.rb +24 -0
  126. data/spec/errors/no_items_spec.rb +10 -0
  127. data/spec/errors/request_error_spec.rb +44 -0
  128. data/spec/errors/server_error_spec.rb +10 -0
  129. data/spec/errors/unauthorized_spec.rb +10 -0
  130. data/spec/models/account_spec.rb +138 -0
  131. data/spec/models/annotation_spec.rb +180 -0
  132. data/spec/models/asset_spec.rb +20 -0
  133. data/spec/models/channel_spec.rb +127 -0
  134. data/spec/models/claim_event_spec.rb +62 -0
  135. data/spec/models/claim_history_spec.rb +27 -0
  136. data/spec/models/claim_spec.rb +211 -0
  137. data/spec/models/configuration_spec.rb +44 -0
  138. data/spec/models/content_detail_spec.rb +45 -0
  139. data/spec/models/content_owner_detail_spec.rb +6 -0
  140. data/spec/models/description_spec.rb +94 -0
  141. data/spec/models/file_detail_spec.rb +13 -0
  142. data/spec/models/live_streaming_detail_spec.rb +6 -0
  143. data/spec/models/ownership_spec.rb +59 -0
  144. data/spec/models/player_spec.rb +13 -0
  145. data/spec/models/playlist_item_spec.rb +120 -0
  146. data/spec/models/playlist_spec.rb +138 -0
  147. data/spec/models/policy_rule_spec.rb +63 -0
  148. data/spec/models/policy_spec.rb +41 -0
  149. data/spec/models/rating_spec.rb +12 -0
  150. data/spec/models/reference_spec.rb +249 -0
  151. data/spec/models/request_spec.rb +163 -0
  152. data/spec/models/resource_spec.rb +57 -0
  153. data/spec/models/right_owner_spec.rb +71 -0
  154. data/spec/models/snippet_spec.rb +13 -0
  155. data/spec/models/statistics_set_spec.rb +13 -0
  156. data/spec/models/status_spec.rb +13 -0
  157. data/spec/models/subscription_spec.rb +30 -0
  158. data/spec/models/url_spec.rb +78 -0
  159. data/spec/models/video_category_spec.rb +21 -0
  160. data/spec/models/video_spec.rb +669 -0
  161. data/spec/requests/as_account/account_spec.rb +125 -0
  162. data/spec/requests/as_account/authentications_spec.rb +139 -0
  163. data/spec/requests/as_account/channel_spec.rb +259 -0
  164. data/spec/requests/as_account/channels_spec.rb +18 -0
  165. data/spec/requests/as_account/playlist_item_spec.rb +56 -0
  166. data/spec/requests/as_account/playlist_spec.rb +244 -0
  167. data/spec/requests/as_account/resource_spec.rb +18 -0
  168. data/spec/requests/as_account/thumbnail.jpg +0 -0
  169. data/spec/requests/as_account/video.mp4 +0 -0
  170. data/spec/requests/as_account/video_spec.rb +408 -0
  171. data/spec/requests/as_content_owner/account_spec.rb +25 -0
  172. data/spec/requests/as_content_owner/advertising_options_set_spec.rb +15 -0
  173. data/spec/requests/as_content_owner/asset_spec.rb +20 -0
  174. data/spec/requests/as_content_owner/channel_spec.rb +1934 -0
  175. data/spec/requests/as_content_owner/claim_history_spec.rb +20 -0
  176. data/spec/requests/as_content_owner/content_owner_spec.rb +241 -0
  177. data/spec/requests/as_content_owner/match_policy_spec.rb +17 -0
  178. data/spec/requests/as_content_owner/ownership_spec.rb +25 -0
  179. data/spec/requests/as_content_owner/playlist_spec.rb +782 -0
  180. data/spec/requests/as_content_owner/video_spec.rb +1239 -0
  181. data/spec/requests/as_server_app/channel_spec.rb +74 -0
  182. data/spec/requests/as_server_app/playlist_item_spec.rb +30 -0
  183. data/spec/requests/as_server_app/playlist_spec.rb +53 -0
  184. data/spec/requests/as_server_app/video_spec.rb +58 -0
  185. data/spec/requests/as_server_app/videos_spec.rb +40 -0
  186. data/spec/requests/unauthenticated/video_spec.rb +22 -0
  187. data/spec/spec_helper.rb +20 -0
  188. data/spec/support/fail_matcher.rb +15 -0
  189. data/spec/support/global_hooks.rb +48 -0
  190. data/yt.gemspec +32 -0
  191. metadata +416 -0
@@ -0,0 +1,161 @@
1
+ require 'yt/collections/base'
2
+
3
+ module Yt
4
+ module Collections
5
+ # @private
6
+ class Reports < Base
7
+ DIMENSIONS = Hash.new({name: 'day', parse: ->(day, *values) { @metrics.keys.zip(values.map{|v| {Date.iso8601(day) => v}}).to_h} }).tap do |hash|
8
+ hash[:month] = {name: 'month', parse: ->(month, *values) { @metrics.keys.zip(values.map{|v| {Range.new(Date.strptime(month, '%Y-%m').beginning_of_month, Date.strptime(month, '%Y-%m').end_of_month) => v} }).to_h} }
9
+ hash[:week] = {name: '7DayTotals', parse: ->(last_day_of_week, *values) { @metrics.keys.zip(values.map{|v| {Range.new(Date.strptime(last_day_of_week) - 6, Date.strptime(last_day_of_week)) => v} }).to_h} }
10
+ hash[:range] = {parse: ->(*values) { @metrics.keys.zip(values.map{|v| {total: v}}).to_h } }
11
+ hash[:traffic_source] = {name: 'insightTrafficSourceType', parse: ->(source, *values) { @metrics.keys.zip(values.map{|v| {TRAFFIC_SOURCES.key(source) => v}}).to_h} }
12
+ hash[:playback_location] = {name: 'insightPlaybackLocationType', parse: ->(location, *values) { @metrics.keys.zip(values.map{|v| {PLAYBACK_LOCATIONS.key(location) => v}}).to_h} }
13
+ hash[:embedded_player_location] = {name: 'insightPlaybackLocationDetail', parse: ->(url, *values) {@metrics.keys.zip(values.map{|v| {url => v}}).to_h} }
14
+ hash[:related_video] = {name: 'insightTrafficSourceDetail', parse: ->(video_id, *values) { @metrics.keys.zip(values.map{|v| {video_id => v}}).to_h} }
15
+ hash[:search_term] = {name: 'insightTrafficSourceDetail', parse: ->(search_term, *values) {@metrics.keys.zip(values.map{|v| {search_term => v}}).to_h} }
16
+ hash[:referrer] = {name: 'insightTrafficSourceDetail', parse: ->(url, *values) {@metrics.keys.zip(values.map{|v| {url => v}}).to_h} }
17
+ hash[:video] = {name: 'video', parse: ->(video_id, *values) { @metrics.keys.zip(values.map{|v| {video_id => v}}).to_h} }
18
+ hash[:playlist] = {name: 'playlist', parse: ->(playlist_id, *values) { @metrics.keys.zip(values.map{|v| {playlist_id => v}}).to_h} }
19
+ hash[:device_type] = {name: 'deviceType', parse: ->(type, *values) {@metrics.keys.zip(values.map{|v| {type.downcase.to_sym => v}}).to_h} }
20
+ hash[:country] = {name: 'country', parse: ->(country_code, *values) { @metrics.keys.zip(values.map{|v| {country_code => v}}).to_h} }
21
+ hash[:state] = {name: 'province', parse: ->(country_and_state_code, *values) { @metrics.keys.zip(values.map{|v| {country_and_state_code[3..-1] => v}}).to_h} }
22
+ hash[:gender_age_group] = {name: 'gender,ageGroup', parse: ->(gender, *values) { [gender.downcase.to_sym, *values] }}
23
+ hash[:gender] = {name: 'gender', parse: ->(gender, *values) {@metrics.keys.zip(values.map{|v| {gender.downcase.to_sym => v}}).to_h} }
24
+ hash[:age_group] = {name: 'ageGroup', parse: ->(age_group, *values) {@metrics.keys.zip(values.map{|v| {age_group[3..-1] => v}}).to_h} }
25
+ end
26
+
27
+ # @see https://developers.google.com/youtube/analytics/v1/dimsmets/dims#Traffic_Source_Dimensions
28
+ # @note EXT_APP is also returned but it’s not in the documentation above!
29
+ TRAFFIC_SOURCES = {
30
+ advertising: 'ADVERTISING',
31
+ annotation: 'ANNOTATION',
32
+ external_app: 'EXT_APP',
33
+ external_url: 'EXT_URL',
34
+ embedded: 'NO_LINK_EMBEDDED',
35
+ other: 'NO_LINK_OTHER',
36
+ playlist: 'PLAYLIST',
37
+ promoted: 'PROMOTED',
38
+ related_video: 'RELATED_VIDEO',
39
+ subscriber: 'SUBSCRIBER',
40
+ channel: 'YT_CHANNEL',
41
+ other_page: 'YT_OTHER_PAGE',
42
+ search: 'YT_SEARCH',
43
+ google: 'GOOGLE_SEARCH',
44
+ notification: 'NOTIFICATION',
45
+ info_card: 'INFO_CARD'
46
+ }
47
+
48
+ # @see https://developers.google.com/youtube/analytics/v1/dimsmets/dims#Playback_Location_Dimensions
49
+ PLAYBACK_LOCATIONS = {
50
+ channel: 'CHANNEL',
51
+ watch: 'WATCH',
52
+ embedded: 'EMBEDDED',
53
+ other: 'YT_OTHER',
54
+ external_app: 'EXTERNAL_APP',
55
+ mobile: 'MOBILE' # only present for data < September 10, 2013
56
+ }
57
+
58
+ attr_writer :metrics
59
+
60
+ def within(days_range, country, state, dimension, videos, try_again = true)
61
+ @days_range = days_range
62
+ @dimension = dimension
63
+ @country = country
64
+ @state = state
65
+ @videos = videos
66
+ if dimension == :gender_age_group # array of array
67
+ Hash.new{|h,k| h[k] = Hash.new 0.0}.tap do |hash|
68
+ each{|gender, age_group, value| hash[gender][age_group[3..-1]] = value}
69
+ end
70
+ else
71
+ hash = flat_map do |hashes|
72
+ hashes.map do |metric, values|
73
+ [metric, values.transform_values{|v| type_cast(v, @metrics[metric])}]
74
+ end.to_h
75
+ end
76
+ hash = hash.inject(@metrics.transform_values{|v| {}}) do |result, hash|
77
+ result.deep_merge hash
78
+ end
79
+ if dimension == :month
80
+ hash = hash.transform_values{|h| h.sort_by{|range, v| range.first}.to_h}
81
+ elsif dimension == :week
82
+ hash = hash.transform_values do |h|
83
+ h.select{|range, v| range.last.wday == days_range.last.wday}.
84
+ sort_by{|range, v| range.first}.to_h
85
+ end
86
+ elsif dimension == :day
87
+ hash = hash.transform_values{|h| h.sort_by{|day, v| day}.to_h}
88
+ elsif dimension.in? [:traffic_source, :country, :state, :playback_location, :device_type]
89
+ hash = hash.transform_values{|h| h.sort_by{|range, v| -v}.to_h}
90
+ end
91
+ (@metrics.one? || @metrics.keys == [:earnings, :estimated_minutes_watched]) ? hash[@metrics.keys.first] : hash
92
+ end
93
+ # NOTE: Once in a while, YouTube responds with 400 Error and the message
94
+ # "Invalid query. Query did not conform to the expectations."; in this
95
+ # case running the same query after one second fixes the issue. This is
96
+ # not documented by YouTube and hardly testable, but trying again the
97
+ # same query is a workaround that works and can hardly cause any damage.
98
+ # Similarly, once in while YouTube responds with a random 503 error.
99
+ rescue Yt::Error => e
100
+ try_again && rescue?(e) ? sleep(3) && within(days_range, country, state, dimension, videos, false) : raise
101
+ end
102
+
103
+ private
104
+
105
+ def type_cast(value, type)
106
+ case [type]
107
+ when [Integer] then value.to_i if value
108
+ when [Float] then value.to_f if value
109
+ end
110
+ end
111
+
112
+ def new_item(data)
113
+ instance_exec *data, &DIMENSIONS[@dimension][:parse]
114
+ end
115
+
116
+ # @see https://developers.google.com/youtube/analytics/v1/content_owner_reports
117
+ def list_params
118
+ super.tap do |params|
119
+ params[:path] = '/youtube/analytics/v1/reports'
120
+ params[:params] = reports_params
121
+ params[:camelize_params] = false
122
+ end
123
+ end
124
+
125
+ def reports_params
126
+ @parent.reports_params.tap do |params|
127
+ params['start-date'] = @days_range.begin
128
+ params['end-date'] = @days_range.end
129
+ params['metrics'] = @metrics.keys.join(',').to_s.camelize(:lower)
130
+ params['dimensions'] = DIMENSIONS[@dimension][:name] unless @dimension == :range
131
+ params['max-results'] = 50 if @dimension.in? [:playlist, :video]
132
+ params['max-results'] = 25 if @dimension.in? [:embedded_player_location, :related_video, :search_term, :referrer]
133
+ if @dimension.in? [:video, :playlist, :embedded_player_location, :related_video, :search_term, :referrer]
134
+ if @metrics.keys == [:earnings, :estimated_minutes_watched]
135
+ params['sort'] = '-earnings'
136
+ else
137
+ params['sort'] = "-#{@metrics.keys.join(',').to_s.camelize(:lower)}"
138
+ end
139
+ end
140
+ params[:filters] = "video==#{@videos.join ','}" if @videos
141
+ params[:filters] = ((params[:filters] || '').split(';') + ["country==US"]).compact.uniq.join(';') if @dimension == :state && !@state
142
+ params[:filters] = ((params[:filters] || '').split(';') + ["country==#{@country}"]).compact.uniq.join(';') if @country && !@state
143
+ params[:filters] = ((params[:filters] || '').split(';') + ["province==US-#{@state}"]).compact.uniq.join(';') if @state
144
+ params[:filters] = ((params[:filters] || '').split(';') + ['isCurated==1']).compact.uniq.join(';') if @dimension == :playlist
145
+ params[:filters] = ((params[:filters] || '').split(';') + ['insightPlaybackLocationType==EMBEDDED']).compact.uniq.join(';') if @dimension == :embedded_player_location
146
+ params[:filters] = ((params[:filters] || '').split(';') + ['insightTrafficSourceType==RELATED_VIDEO']).compact.uniq.join(';') if @dimension == :related_video
147
+ params[:filters] = ((params[:filters] || '').split(';') + ['insightTrafficSourceType==YT_SEARCH']).compact.uniq.join(';') if @dimension == :search_term
148
+ params[:filters] = ((params[:filters] || '').split(';') + ['insightTrafficSourceType==EXT_URL']).compact.uniq.join(';') if @dimension == :referrer
149
+ end
150
+ end
151
+
152
+ def items_key
153
+ 'rows'
154
+ end
155
+
156
+ def rescue?(error)
157
+ 'badRequest'.in?(error.reasons) && error.to_s =~ /did not conform/
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,57 @@
1
+ # encoding: UTF-8
2
+ require 'yt/collections/base'
3
+
4
+ module Yt
5
+ module Collections
6
+ # @private
7
+ class Resources < Base
8
+ def delete_all(params = {})
9
+ do_delete_all params
10
+ end
11
+
12
+ def insert(attributes = {}, options = {}) #
13
+ underscore_keys! attributes
14
+ body = build_insert_body attributes
15
+ params = {part: body.keys.join(',')}
16
+ do_insert(params: params, body: body)
17
+ end
18
+
19
+ private
20
+
21
+ def attributes_for_new_item(data)
22
+ {id: data['id'], snippet: data['snippet'], status: data['status'], auth: @auth}
23
+ end
24
+
25
+ def resources_params
26
+ {max_results: 50, part: 'snippet,status'}
27
+ end
28
+
29
+ def build_insert_body(attributes = {})
30
+ {}.tap do |body|
31
+ insert_parts.each do |name, part|
32
+ if should_include_part_in_insert? part, attributes
33
+ body[name] = build_insert_body_part part, attributes
34
+ sanitize_brackets! body[name] if part[:sanitize_brackets]
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def build_insert_body_part(part, attributes = {})
41
+ {}.tap do |body_part|
42
+ part[:keys].map do |key|
43
+ body_part[camelize key] = attributes[key] if attributes[key]
44
+ end
45
+ end
46
+ end
47
+
48
+ def should_include_part_in_insert?(part, attributes = {})
49
+ (part[:keys] & attributes.keys).any?
50
+ end
51
+
52
+ def camelize(value)
53
+ value.to_s.camelize(:lower).to_sym
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,51 @@
1
+ require 'yt/collections/base'
2
+ require 'yt/models/resumable_session'
3
+
4
+ module Yt
5
+ module Collections
6
+ # @private
7
+ # Provides methods to upload videos with the resumable upload protocol.
8
+ #
9
+ # Resources with resumable sessions are: {Yt::Models::Account accounts}.
10
+ #
11
+ # @see https://developers.google.com/youtube/v3/guides/using_resumable_upload_protocol
12
+ class ResumableSessions < Base
13
+
14
+ # Starts a resumable session by sending to YouTube the metadata of the
15
+ # object to upload. If the request succeeds, YouTube returns a unique
16
+ # URL to upload the object file (and eventually resume the upload).
17
+ # @param [Integer] content_length the size (bytes) of the object to upload.
18
+ # @param [Hash] body the metadata to add to the uploaded object.
19
+ def insert(content_length, body = {})
20
+ @headers = headers_for content_length
21
+ do_insert body: body, headers: @headers
22
+ end
23
+
24
+ private
25
+
26
+ def attributes_for_new_item(data)
27
+ {url: data['Location'], headers: @headers, auth: @auth}
28
+ end
29
+
30
+ def insert_params
31
+ super.tap do |params|
32
+ params[:response_format] = nil
33
+ params[:path] = @parent.upload_path
34
+ params[:params] = @parent.upload_params.merge uploadType: 'resumable'
35
+ end
36
+ end
37
+
38
+ def headers_for(content_length)
39
+ {}.tap do |headers|
40
+ headers['x-upload-content-length'] = content_length
41
+ headers['X-Upload-Content-Type'] = @parent.upload_content_type
42
+ end
43
+ end
44
+
45
+ # The result is not in the body but in the headers
46
+ def extract_data_from(response)
47
+ response.header
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,27 @@
1
+ require 'yt/collections/base'
2
+ require 'yt/models/snippet'
3
+
4
+ module Yt
5
+ module Collections
6
+ # @private
7
+ class Snippets < Base
8
+
9
+ private
10
+
11
+ def attributes_for_new_item(data)
12
+ {data: data['snippet'], auth: @auth}
13
+ end
14
+
15
+ # @return [Hash] the parameters to submit to YouTube to get the
16
+ # snippet of a resource, for instance a channel.
17
+ # @see https://developers.google.com/youtube/v3/docs/channels#resource
18
+ def list_params
19
+ endpoint = @parent.kind.pluralize.camelize :lower
20
+ super.tap do |params|
21
+ params[:path] = "/youtube/v3/#{endpoint}"
22
+ params[:params] = {id: @parent.id, part: 'snippet'}
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,30 @@
1
+ require 'yt/collections/base'
2
+ require 'yt/models/statistics_set'
3
+
4
+ module Yt
5
+ module Collections
6
+ # @private
7
+ class StatisticsSets < Base
8
+
9
+ private
10
+
11
+ def attributes_for_new_item(data)
12
+ {data: data['statistics']}
13
+ end
14
+
15
+ # @return [Hash] the parameters to submit to YouTube to get the
16
+ # statistics of a resource, for instance a video.
17
+ # @see https://developers.google.com/youtube/v3/docs/videos#resource
18
+ def list_params
19
+ super.tap do |params|
20
+ params[:path] = "/youtube/v3/#{@parent.kind.pluralize}"
21
+ params[:params] = statistics_sets_params
22
+ end
23
+ end
24
+
25
+ def statistics_sets_params
26
+ {max_results: 50, part: 'statistics', id: @parent.id}
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,27 @@
1
+ require 'yt/collections/base'
2
+ require 'yt/models/status'
3
+
4
+ module Yt
5
+ module Collections
6
+ # @private
7
+ class Statuses < Base
8
+
9
+ private
10
+
11
+ def attributes_for_new_item(data)
12
+ {data: data['status']}
13
+ end
14
+
15
+ # @return [Hash] the parameters to submit to YouTube to get the status
16
+ # of a resource, for instance a channel.
17
+ # @see https://developers.google.com/youtube/v3/docs/channels/list
18
+ def list_params
19
+ endpoint = @parent.kind.pluralize.camelize :lower
20
+ super.tap do |params|
21
+ params[:path] = "/youtube/v3/#{endpoint}"
22
+ params[:params] = {id: @parent.id, part: 'status'}
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,46 @@
1
+ require 'yt/collections/channels'
2
+
3
+ module Yt
4
+ module Collections
5
+ # Provides methods to interact with the list of channels a resource is
6
+ # subscribed to.
7
+ #
8
+ # Resources with subscribed channels are: {Yt::Models::Channel channels}.
9
+ #
10
+ class SubscribedChannels < Channels
11
+
12
+ private
13
+
14
+ def attributes_for_new_item(data)
15
+ snippet = data.fetch 'snippet', {}
16
+ resource = snippet.fetch 'resourceId', {}
17
+ {id: resource['channelId'], snippet: snippet, auth: @auth}
18
+ end
19
+
20
+ # @return [Hash] the parameters to submit to YouTube to list subscribers.
21
+ # @see https://developers.google.com/youtube/v3/docs/subscriptions/list
22
+ def list_params
23
+ super.tap{|params| params[:path] = '/youtube/v3/subscriptions'}
24
+ end
25
+
26
+ # @private
27
+ # @note Subscribers overwrites +channel_params+ since the query
28
+ # is slightly different.
29
+ def channels_params
30
+ {}.tap do |params|
31
+ params[:max_results] = 50
32
+ params[:part] = 'snippet'
33
+ params[:channel_id] = @parent.id
34
+ apply_where_params! params
35
+ end
36
+ end
37
+
38
+ # @private
39
+ # @note Subscribers overwrites +list_resources+ since the objects to
40
+ # instatiate belongs to Channel class not Subscriber.
41
+ def resource_class
42
+ Yt::Models::Channel
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,33 @@
1
+ require 'yt/collections/subscribed_channels'
2
+
3
+ module Yt
4
+ module Collections
5
+ # Provides methods to interact with subscribers of a YouTube resource.
6
+ #
7
+ # Resources with subscribers are: {Yt::Models::Account accounts}.
8
+ #
9
+ # Confusingly, YouTube API provides the +same+ endpoint to either
10
+ # retrieve the channels that you are subscribed to or the channels
11
+ # that are subscribed to you. The difference relies in the setting the
12
+ # +mySubscribers+ parameter to true and in reading the information
13
+ # from the +subscriberSnippet+ part, not the +snippet+ part.
14
+ # @see https://developers.google.com/youtube/v3/docs/subscriptions/list
15
+ class Subscribers < SubscribedChannels
16
+
17
+ private
18
+
19
+ def attributes_for_new_item(data)
20
+ snippet = data.fetch 'subscriberSnippet', {}
21
+ {id: snippet['channelId'], snippet: snippet, auth: @auth}
22
+ end
23
+
24
+ def channels_params
25
+ {}.tap do |params|
26
+ params[:max_results] = 50
27
+ params[:part] = 'subscriberSnippet'
28
+ params[:my_subscribers] = true
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,50 @@
1
+ require 'yt/collections/base'
2
+ require 'yt/models/subscription'
3
+
4
+ module Yt
5
+ module Collections
6
+ # @private
7
+ class Subscriptions < Base
8
+
9
+ def insert(options = {})
10
+ do_insert
11
+ rescue Yt::Error => error
12
+ ignorable_error = error.reasons.include? 'subscriptionDuplicate'
13
+ ignorable_error ||= (@parent.id == @auth.channel.id) if @auth
14
+ raise error unless options[:ignore_errors] && ignorable_error
15
+ end
16
+
17
+ private
18
+
19
+ def attributes_for_new_item(data)
20
+ {id: data['id'], auth: @auth}
21
+ end
22
+
23
+ # @return [Hash] the parameters to submit to YouTube to list subscriptions.
24
+ # @see https://developers.google.com/youtube/v3/docs/subscriptions/list
25
+ def list_params
26
+ super.tap do |params|
27
+ params[:params] = subscriptions_params
28
+ end
29
+ end
30
+
31
+ def subscriptions_params
32
+ {}.tap do |params|
33
+ params[:max_results] = 50
34
+ params[:for_channel_id] = @parent.id
35
+ params[:mine] = true
36
+ params[:part] = 'snippet'
37
+ end
38
+ end
39
+
40
+ # @return [Hash] the parameters to submit to YouTube to add a subscriptions.
41
+ # @see https://developers.google.com/youtube/v3/docs/subscriptions/insert
42
+ def insert_params
43
+ super.tap do |params|
44
+ params[:params] = {part: 'snippet'}
45
+ params[:body] = {snippet: {resourceId: {channelId: @parent.id}}}
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end