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,58 @@
1
+ require 'yt/models/url'
2
+
3
+ module Yt
4
+ module Models
5
+ # Encapsulates information about the description of a resource, for
6
+ # instance a channel.
7
+ # @see https://developers.google.com/youtube/v3/docs/channels#resource
8
+ #
9
+ # The value has a maximum length of 1000 characters.
10
+ class Description < String
11
+ # @return [Boolean] whether the description includes a link to a video
12
+ # @example
13
+ # description = Yt::Models::Description.new 'Link to video: youtube.com/watch?v=MESycYJytkU'
14
+ # description.has_link_to_video? #=> true
15
+ #
16
+ # @todo add an option to match the link to a specific video
17
+ def has_link_to_video?
18
+ links.any?{|link| link.kind == :video}
19
+ end
20
+
21
+ # @return [Boolean] whether the description includes a link to a channel
22
+ # @example
23
+ # description = Yt::Description.new 'Link to channel: youtube.com/fullscreen'
24
+ # description.has_link_to_channel? #=> true
25
+ #
26
+ # @todo add an option to match the link to a specific channel
27
+ def has_link_to_channel?(options = {})
28
+ links.any?{|link| link.kind == :channel}
29
+ end
30
+
31
+ # @return [Boolean] whether the description includes a link to subscribe
32
+ # @example
33
+ # description = Yt::Description.new 'Link to subscribe: youtube.com/subscription_center?add_user=fullscreen'
34
+ # description.has_link_to_subscribe? #=> true
35
+ #
36
+ # @todo add an option to match the link to subscribe to a specific channel
37
+ def has_link_to_subscribe?(options = {})
38
+ links.any?{|link| link.kind == :subscription}
39
+ end
40
+
41
+ # @return [Boolean] whether the description includes a link to a playlist
42
+ # @example
43
+ # description = Yt::Description.new 'Link to playlist: youtube.com/playlist?list=LLxO1tY8h1AhOz0T4ENwmpow'
44
+ # description.has_link_to_playlist? #=> true
45
+ #
46
+ # @todo add an option to match the link to a specific playlist
47
+ def has_link_to_playlist?
48
+ links.any?{|link| link.kind == :playlist}
49
+ end
50
+
51
+ private
52
+
53
+ def links
54
+ @links ||= self.split(' ').map{|word| URL.new word}
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,16 @@
1
+ require 'yt/models/base'
2
+
3
+ module Yt
4
+ module Models
5
+ # @private
6
+ class DeviceFlow < Base
7
+ def initialize(options = {})
8
+ @data = options[:data]
9
+ end
10
+
11
+ has_attribute :device_code, camelize: false
12
+ has_attribute :user_code, camelize: false
13
+ has_attribute :verification_url, camelize: false
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ require 'yt/models/base'
2
+
3
+ module Yt
4
+ module Models
5
+ # @private
6
+ # Encapsulates basic information about the video file itself,
7
+ # including the file name, size, type, and container.
8
+ # @see https://developers.google.com/youtube/v3/docs/videos#fileDetails
9
+ class FileDetail < Base
10
+ attr_reader :data
11
+
12
+ def initialize(options = {})
13
+ @data = options[:data] || {}
14
+ end
15
+
16
+ has_attribute :file_size, type: Integer
17
+ has_attribute :file_type
18
+ has_attribute :container
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,9 @@
1
+ module Yt
2
+ module Models
3
+ # @private
4
+ # Encapsulates information about the ID that YouTube uses to uniquely
5
+ # identify a resource.
6
+ class Id < String
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ module Yt
2
+ module Models
3
+ # @private
4
+ # If we dropped support for Ruby 1.9.3, then we could simply use Enumerator
5
+ # which takes a `size` parameter in Ruby >= 2.
6
+ class Iterator < Enumerator
7
+ def initialize(size=nil, &block)
8
+ RUBY_VERSION < '2' ? super(&block) : super(size, &block)
9
+ end
10
+
11
+ def size
12
+ RUBY_VERSION < '2' ? count : super
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ require 'yt/models/base'
2
+
3
+ module Yt
4
+ module Models
5
+ # @private
6
+ # Encapsulates information about a live video broadcast.
7
+ # The object will only be present in a video resource if the video is an
8
+ # upcoming, live, or completed live broadcast.
9
+ # @see https://developers.google.com/youtube/v3/docs/videos#resource
10
+ class LiveStreamingDetail < Base
11
+
12
+ def initialize(options = {})
13
+ @data = options[:data] || {}
14
+ end
15
+
16
+ has_attribute :actual_start_time, type: Time
17
+ has_attribute :actual_end_time, type: Time
18
+ has_attribute :scheduled_start_time, type: Time
19
+ has_attribute :scheduled_end_time, type: Time
20
+ has_attribute :concurrent_viewers, type: Integer
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,34 @@
1
+ require 'yt/models/base'
2
+
3
+ module Yt
4
+ module Models
5
+ # Provides methods to interact with YouTube ContentID asset match policies,
6
+ # which YouTube applies to user-uploaded videos that match the asset.
7
+ # @see https://developers.google.com/youtube/partner/docs/v1/assetMatchPolicy
8
+ class MatchPolicy < Base
9
+ def initialize(options = {})
10
+ @asset_id = options[:asset_id]
11
+ @auth = options[:auth]
12
+ end
13
+
14
+ def update(attributes = {})
15
+ underscore_keys! attributes
16
+ do_patch body: attributes.slice(:policy_id)
17
+ true
18
+ end
19
+
20
+ private
21
+
22
+ # @return [Hash] the parameters to submit to YouTube to patch an asset
23
+ # match policy.
24
+ # @see https://developers.google.com/youtube/partner/docs/v1/assetMatchPolicy/patch
25
+ def patch_params
26
+ super.tap do |params|
27
+ params[:path] = "/youtube/partner/v1/assets/#{@asset_id}/matchPolicy"
28
+ params[:params] = {on_behalf_of_content_owner: @auth.owner_name}
29
+ params[:expected_response] = Net::HTTPOK
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,75 @@
1
+ require 'yt/models/base'
2
+ require 'yt/models/right_owner'
3
+
4
+ module Yt
5
+ module Models
6
+ # Provides methods to interact with YouTube ContentID asset ownership,
7
+ # which provide ownership information for the specified asset.
8
+ # @see https://developers.google.com/youtube/partner/docs/v1/ownership
9
+ class Ownership < Base
10
+ def initialize(options = {})
11
+ @data = options[:data] || {}
12
+ @auth = options[:auth]
13
+ @asset_id = options[:asset_id]
14
+ end
15
+
16
+ def update(attributes = {})
17
+ underscore_keys! attributes
18
+ do_update body: attributes
19
+ true
20
+ end
21
+
22
+ # Assigns 100% of the general ownership of the asset to @auth.
23
+ def obtain!
24
+ update general: [{ratio: 100, owner: @auth.owner_name, type: :exclude}]
25
+ end
26
+
27
+ # Releases 100% of the general ownership of the asset from @auth.
28
+ def release!
29
+ update general: [{ratio: 0, owner: @auth.owner_name, type: :exclude}]
30
+ end
31
+
32
+ # @return [Array<RightOwner>] a list that identifies the owners of an
33
+ # asset and the territories where each owner has ownership.
34
+ # General asset ownership is used for all types of assets and is the
35
+ # only type of ownership data that can be provided for assets that are
36
+ # not compositions.
37
+ has_attribute :general_owners, from: :general do |data|
38
+ as_owners data
39
+ end
40
+
41
+ # @return [Array<RightOwner>] a list that identifies owners of the
42
+ # performance rights for a composition asset.
43
+ has_attribute :performance_owners, from: :performance do |data|
44
+ as_owners data
45
+ end
46
+
47
+ # @return [Array<RightOwner>] a list that identifies owners of the
48
+ # synchronization rights for a composition asset.
49
+ has_attribute :synchronization_owners, from: :synchronization do |data|
50
+ as_owners data
51
+ end
52
+
53
+ # @return [Array<RightOwner>] a list that identifies owners of the
54
+ # mechanical rights for a composition asset.
55
+ has_attribute :mechanical_owners, from: :mechanical do |data|
56
+ as_owners data
57
+ end
58
+
59
+ private
60
+
61
+ # @see https://developers.google.com/youtube/partner/docs/v1/ownership/update
62
+ def update_params
63
+ super.tap do |params|
64
+ params[:expected_response] = Net::HTTPOK
65
+ params[:path] = "/youtube/partner/v1/assets/#{@asset_id}/ownership"
66
+ params[:params] = {on_behalf_of_content_owner: @auth.owner_name}
67
+ end
68
+ end
69
+
70
+ def as_owners(data)
71
+ (data || []).map{|owner_data| Yt::RightOwner.new data: owner_data}
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,18 @@
1
+ require 'yt/models/base'
2
+
3
+ module Yt
4
+ module Models
5
+ # @private
6
+ # Encapsulates information about a video player.
7
+ # @see https://developers.google.com/youtube/v3/docs/videos#player
8
+ class Player < Base
9
+ attr_reader :data
10
+
11
+ def initialize(options = {})
12
+ @data = options[:data]
13
+ end
14
+
15
+ has_attribute :embed_html
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,218 @@
1
+ require 'yt/models/resource'
2
+
3
+ module Yt
4
+ module Models
5
+ # Provides methods to interact with YouTube playlists.
6
+ # @see https://developers.google.com/youtube/v3/docs/playlists
7
+ class Playlist < Resource
8
+
9
+ ### SNIPPET ###
10
+
11
+ # @!attribute [r] title
12
+ # @return [String] the playlist’s title.
13
+ delegate :title, to: :snippet
14
+
15
+ # @!attribute [r] description
16
+ # @return [String] the playlist’s description.
17
+ delegate :description, to: :snippet
18
+
19
+ # @!method thumbnail_url(size = :default)
20
+ # Returns the URL of the playlist’s thumbnail.
21
+ # @param [Symbol, String] size The size of the playlist’s thumbnail.
22
+ # @return [String] if +size+ is +default+, the URL of a 120x90px image.
23
+ # @return [String] if +size+ is +medium+, the URL of a 320x180px image.
24
+ # @return [String] if +size+ is +high+, the URL of a 480x360px image.
25
+ # @return [nil] if the +size+ is not +default+, +medium+ or +high+.
26
+ delegate :thumbnail_url, to: :snippet
27
+
28
+ # @!attribute [r] published_at
29
+ # @return [Time] the date and time that the playlist was created.
30
+ delegate :published_at, to: :snippet
31
+
32
+ # @!attribute [r] channel_id
33
+ # @return [String] the ID of the channel that the playlist belongs to.
34
+ delegate :channel_id, to: :snippet
35
+
36
+ # @!attribute [r] channel_title
37
+ # @return [String] the title of the channel that the playlist belongs to.
38
+ delegate :channel_title, to: :snippet
39
+
40
+ # @!attribute [r] tags
41
+ # @return [Array<String>] the list of tags attached to the playlist.
42
+ delegate :tags, to: :snippet
43
+
44
+ ### ACTIONS (UPLOAD, UPDATE, DELETE) ###
45
+
46
+ # Deletes the playlist.
47
+ # @return [Boolean] whether the playlist does not exist anymore.
48
+ # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
49
+ # authenticated Yt::Account with permissions to delete the playlist.
50
+ def delete(options = {})
51
+ do_delete {@id = nil}
52
+ !exists?
53
+ end
54
+
55
+ # Updates the attributes of a playlist.
56
+ # @return [Boolean] whether the playlist was successfully updated.
57
+ # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
58
+ # authenticated Yt::Account with permissions to update the playlist.
59
+ # @param [Hash] attributes the attributes to update.
60
+ # @option attributes [String] :title The new playlist’s title.
61
+ # Cannot have more than 100 characters. Can include the characters
62
+ # < and >, which are replaced to ‹ › in order to be accepted by YouTube.
63
+ # @option attributes [String] :description The new playlist’s description.
64
+ # Cannot have more than 5000 bytes. Can include the characters
65
+ # < and >, which are replaced to ‹ › in order to be accepted by YouTube.
66
+ # @option attributes [Array<String>] :tags The new playlist’s tags.
67
+ # Cannot have more than 500 characters. Can include the characters
68
+ # < and >, which are replaced to ‹ › in order to be accepted by YouTube.
69
+ # @option attributes [String] :privacy_status The new playlist’s privacy
70
+ # status. Must be one of: private, unscheduled, public.
71
+ # @example Update title and description of a playlist.
72
+ # playlist.update title: 'New title', description: 'New description'
73
+ # @example Update tags and status of a playlist.
74
+ # playlist.update tags: ['new', 'tags'], privacy_status: 'public'
75
+ def update(attributes = {})
76
+ super
77
+ end
78
+
79
+ # Adds a video to the playlist.
80
+ # Unlike {#add_video!}, does not raise an error if video can’t be added.
81
+ # @param [String] video_id the video to add to the playlist.
82
+ # @param [Hash] options the options on how to add the video.
83
+ # @option options [Integer] :position where to add video in the playlist.
84
+ # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
85
+ # authenticated Yt::Account with permissions to update the playlist.
86
+ # @return [Yt::PlaylistItem] the item added to the playlist.
87
+ def add_video(video_id, options = {})
88
+ playlist_item_params = playlist_item_params(video_id, options)
89
+ playlist_items.insert playlist_item_params, ignore_errors: true
90
+ end
91
+
92
+ # Adds a video to the playlist.
93
+ # Unlike {#add_video}, raises an error if video can’t be added.
94
+ # @param [String] video_id the video ID to add to the playlist.
95
+ # @param [Hash] options the options on how to add the video.
96
+ # @option options [Integer] :position where to add video in the playlist.
97
+ # @raise [Yt::Errors::RequestError] if video can’t be added.
98
+ # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
99
+ # authenticated Yt::Account with permissions to update the playlist.
100
+ # @return [Yt::PlaylistItem] the item added to the playlist.
101
+ def add_video!(video_id, options = {})
102
+ playlist_item_params = playlist_item_params(video_id, options)
103
+ playlist_items.insert playlist_item_params
104
+ end
105
+
106
+ # Adds multiple videos to the playlist.
107
+ # Unlike {#add_videos!}, does not raise an error if videos can’t be added.
108
+ # @param [Array<String>] video_ids the video IDs to add to the playlist.
109
+ # @param [Hash] options the options on how to add the videos.
110
+ # @option options [Integer] :position where to add videos in the playlist.
111
+ # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
112
+ # authenticated Yt::Account with permissions to update the playlist.
113
+ # @return [Array<Yt::PlaylistItem>] the items added to the playlist.
114
+ def add_videos(video_ids = [], options = {})
115
+ video_ids.map{|video_id| add_video video_id, options}
116
+ end
117
+
118
+ # Adds multiple videos to the playlist.
119
+ # Unlike {#add_videos}, raises an error if videos can’t be added.
120
+ # @param [Array<String>] video_ids the video IDs to add to the playlist.
121
+ # @param [Hash] options the options on how to add the videos.
122
+ # @option options [Integer] :position where to add videos in the playlist.
123
+ # @raise [Yt::Errors::Unauthorized] if {Resource#auth auth} is not an
124
+ # authenticated Yt::Account with permissions to update the playlist.
125
+ # @return [Array<Yt::PlaylistItem>] the items added to the playlist.
126
+ def add_videos!(video_ids = [], options = {})
127
+ video_ids.map{|video_id| add_video! video_id, options}
128
+ end
129
+
130
+ # Deletes the playlist’s items matching all the given attributes.
131
+ # @return [Array<Boolean>] whether each playlist item matching the given
132
+ # attributes was deleted.
133
+ # @raise [Yt::Errors::RequestError] if {Resource#auth auth} is not an
134
+ # authenticated Yt::Account with permissions to update the playlist.
135
+ # @param [Hash] attributes the attributes to match the items by.
136
+ # @option attributes [<String, Regexp>] :title The item’s title.
137
+ # Pass a String for perfect match or a Regexp for advanced match.
138
+ # @option attributes [<String, Regexp>] :description The item’s
139
+ # description. Pass a String (perfect match) or a Regexp (advanced).
140
+ # @option attributes [String] :privacy_status The item’s privacy status.
141
+ # @option attributes [String] :video_id The item’s video ID.
142
+ def delete_playlist_items(attributes = {})
143
+ playlist_items.delete_all attributes
144
+ end
145
+
146
+ ### ASSOCIATIONS ###
147
+
148
+ # @!attribute [r] playlist_items
149
+ # @return [Yt::Collections::PlaylistItems] the playlist’s items.
150
+ has_many :playlist_items
151
+
152
+ ### ANALYTICS ###
153
+
154
+ # @macro reports
155
+
156
+ # @macro report_by_playlist_dimensions
157
+ has_report :views, Integer
158
+
159
+ # @macro report_by_playlist_dimensions
160
+ has_report :estimated_minutes_watched, Integer
161
+
162
+ # @macro report_by_gender_and_age_group
163
+ has_report :viewer_percentage, Float
164
+
165
+ # @macro report_by_day_and_state
166
+ has_report :average_view_duration, Integer
167
+
168
+ # @macro report_by_day_and_state
169
+ has_report :playlist_starts, Integer
170
+
171
+ # @macro report_by_day_and_state
172
+ has_report :average_time_in_playlist, Float
173
+
174
+ # @macro report_by_day_and_state
175
+ has_report :views_per_playlist_start, Float
176
+
177
+ ### PRIVATE API ###
178
+
179
+ # @private
180
+ # Tells `has_reports` to retrieve the reports from YouTube Analytics API
181
+ # either as a Channel or as a Content Owner.
182
+ # @see https://developers.google.com/youtube/analytics/v1/reports
183
+ def reports_params
184
+ {}.tap do |params|
185
+ if auth.owner_name
186
+ params[:ids] = "contentOwner==#{auth.owner_name}"
187
+ else
188
+ params[:ids] = "channel==#{channel_id}"
189
+ end
190
+ params[:filters] = "playlist==#{id};isCurated==1"
191
+ end
192
+ end
193
+
194
+ # @private
195
+ def exists?
196
+ !@id.nil?
197
+ end
198
+
199
+ private
200
+
201
+ # @see https://developers.google.com/youtube/v3/docs/playlists/update
202
+ def update_parts
203
+ keys = [:title, :description, :tags]
204
+ snippet = {keys: keys, required: true, sanitize_brackets: true}
205
+ status = {keys: [:privacy_status]}
206
+ {snippet: snippet, status: status}
207
+ end
208
+
209
+ # @todo: extend camelize to also camelize the nested hashes, so we
210
+ # don’t have to write videoId
211
+ def playlist_item_params(video_id, params = {})
212
+ params.dup.tap do |params|
213
+ params[:resource_id] ||= {kind: 'youtube#video', videoId: video_id}
214
+ end
215
+ end
216
+ end
217
+ end
218
+ end