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,136 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'yt/models/base'
4
+ require 'yt/models/url'
5
+
6
+ module Yt
7
+ module Models
8
+ class Resource < Base
9
+ # @private
10
+ attr_reader :auth
11
+
12
+ ### ID ###
13
+
14
+ # @!attribute [r] id
15
+ # @return [String] the ID that YouTube uses to identify each resource.
16
+ has_one :id
17
+
18
+ ### STATUS ###
19
+
20
+ has_one :status
21
+
22
+ # @!attribute [r] privacy_status
23
+ # @return [String] the privacy status of the resource. Possible values
24
+ # are: +'private'+, +'public'+, +'unlisted'+.
25
+ delegate :privacy_status, to: :status
26
+
27
+ # @return [Boolean] whether the resource is public.
28
+ def public?
29
+ privacy_status == 'public'
30
+ end
31
+
32
+ # @return [Boolean] whether the resource is private.
33
+ def private?
34
+ privacy_status == 'private'
35
+ end
36
+
37
+ # @return [Boolean] whether the resource is unlisted.
38
+ def unlisted?
39
+ privacy_status == 'unlisted'
40
+ end
41
+
42
+ has_one :snippet
43
+
44
+ # @private
45
+ def initialize(options = {})
46
+ @url = URL.new(options[:url]) if options[:url]
47
+ @id = options[:id] || (@url.id if @url)
48
+ @auth = options[:auth]
49
+ @snippet = Snippet.new(data: options[:snippet]) if options[:snippet]
50
+ @status = Status.new(data: options[:status]) if options[:status]
51
+ end
52
+
53
+ # @private
54
+ def kind
55
+ @url ? @url.kind.to_s : self.class.to_s.demodulize.underscore
56
+ end
57
+
58
+ # @private
59
+ def username
60
+ @url.username if @url
61
+ end
62
+
63
+ # @private
64
+ def update(attributes = {})
65
+ underscore_keys! attributes
66
+ body = build_update_body attributes
67
+ params = {part: body.keys.join(',')}
68
+ do_update params: params, body: body.merge(id: @id) do |data|
69
+ @id = data['id']
70
+ @snippet = Snippet.new data: data['snippet'] if data['snippet']
71
+ @status = Status.new data: data['status'] if data['status']
72
+ true
73
+ end
74
+ end
75
+
76
+ private
77
+
78
+ # Since YouTube API only returns tags on Videos#list, the memoized
79
+ # `@snippet` is erased if the video was instantiated through Video#search
80
+ # (e.g., by calling account.videos or channel.videos), so that the full
81
+ # snippet (with tags and category) is loaded, rather than the partial one.
82
+ def ensure_complete_snippet(attribute)
83
+ unless snippet.public_send(attribute).present? || snippet.complete?
84
+ @snippet = nil
85
+ end
86
+ snippet.public_send attribute
87
+ end
88
+
89
+ # TODO: instead of having Video, Playlist etc override this method,
90
+ # they should define *what* can be updated in their own *update*
91
+ # method.
92
+ # @return [Hash] the parameters to submit to YouTube to update a playlist.
93
+ # @see https://developers.google.com/youtube/v3/docs/playlists/update
94
+ # @see https://developers.google.com/youtube/v3/docs/videos/update
95
+ def update_params
96
+ super.tap do |params|
97
+ params[:request_format] = :json
98
+ params[:expected_response] = Net::HTTPOK
99
+ end
100
+ end
101
+
102
+ # @return [Hash] the parameters to submit to YouTube to delete a playlist.
103
+ # @see https://developers.google.com/youtube/v3/docs/playlists/delete
104
+ def delete_params
105
+ super.tap{|params| params[:params] = {id: @id}}
106
+ end
107
+
108
+ def build_update_body(attributes = {})
109
+ {}.tap do |body|
110
+ update_parts.each do |name, part|
111
+ if should_include_part_in_update? part, attributes
112
+ body[name] = build_update_body_part name, part, attributes
113
+ sanitize_brackets! body[name] if part[:sanitize_brackets]
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ def build_update_body_part(name, part, attributes = {})
120
+ {}.tap do |body_part|
121
+ part[:keys].map do |key|
122
+ body_part[camelize key] = attributes.fetch key, public_send(name).public_send(key)
123
+ end
124
+ end
125
+ end
126
+
127
+ def should_include_part_in_update?(part, attributes = {})
128
+ part[:required] || (part[:keys] & attributes.keys).any?
129
+ end
130
+
131
+ def camelize(value)
132
+ value.to_s.camelize(:lower).to_sym
133
+ end
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,52 @@
1
+ require 'cgi'
2
+ require 'yt/models/base'
3
+
4
+ module Yt
5
+ module Models
6
+ # @private
7
+ # Provides methods to upload videos with the resumable upload protocol.
8
+ # @see https://developers.google.com/youtube/v3/guides/using_resumable_upload_protocol
9
+ class ResumableSession < Base
10
+ # Sets up a resumable session using the URI returned by YouTube
11
+ def initialize(options = {})
12
+ @uri = URI.parse options[:url]
13
+ @auth = options[:auth]
14
+ @headers = options[:headers]
15
+ end
16
+
17
+ def update(params = {})
18
+ do_update(params) {|data| yield data}
19
+ end
20
+
21
+ # Uploads a thumbnail using the current resumable session
22
+ # @param [#read] file A binary object that contains the image content.
23
+ # Can either be a File, a StringIO (for instance using open-uri), etc.
24
+ # @return the new thumbnail resource for the given image.
25
+ # @see https://developers.google.com/youtube/v3/docs/thumbnails#resource
26
+ def upload_thumbnail(file)
27
+ do_update(body: file) {|data| data['items'].first}
28
+ end
29
+
30
+ private
31
+
32
+ def session_params
33
+ CGI::parse(@uri.query).tap{|hash| hash.each{|k,v| hash[k] = v.first}}
34
+ end
35
+
36
+ # @note: YouTube documentation states that a valid upload returns an HTTP
37
+ # code of 201 Created -- however it looks like the actual code is 200.
38
+ # To be sure to include both cases, HTTPSuccess is used
39
+ def update_params
40
+ super.tap do |params|
41
+ params[:request_format] = :file
42
+ params[:host] = @uri.host
43
+ params[:path] = @uri.path
44
+ params[:expected_response] = Net::HTTPSuccess
45
+ params[:headers] = @headers
46
+ params[:camelize_params] = false
47
+ params[:params] = session_params
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,58 @@
1
+ require 'yt/models/description'
2
+
3
+ module Yt
4
+ module Models
5
+ # Encapsulates information about the various types of owners of an asset.
6
+ # @see https://developers.google.com/youtube/partner/docs/v1/ownership#resource
7
+ class RightOwner < Base
8
+ def initialize(options = {})
9
+ @data = options[:data]
10
+ end
11
+
12
+ # @return [Float] the percentage of the asset that the owner controls or
13
+ # administers. For composition assets, the value can be any value
14
+ # between 0 and 100 inclusive. For all other assets, the only valid
15
+ # values are 100, which indicates that the owner completely owns the
16
+ # asset in the specified territories, and 0, which indicates that you
17
+ # are removing ownership of the asset in the specified territories.
18
+ has_attribute :ratio, type: Float
19
+
20
+ # @return [String] the name of the asset’s owner or rights administrator.
21
+ has_attribute :owner
22
+
23
+ # @return [String] if the asset is a composition asset and the asset
24
+ # owner is not known to have a formal relationship established with
25
+ # YouTube, the name of the asset’s publisher or rights administrator.
26
+ # @return [nil] otherwise.
27
+ has_attribute :publisher
28
+
29
+ # Return the list of territories where the owner owns the asset.
30
+ # Each territory is an ISO 3166 two-letter country code.
31
+ # @return [Array<String>] if the ownership lists 'included' territories,
32
+ # the territories where the owner owns the asset.
33
+ # @return [nil] if the ownership does not list 'included' territories,
34
+ def included_territories
35
+ territories if type == 'include'
36
+ end
37
+
38
+ # Return the list of territories where the owner does not own the asset.
39
+ # Each territory is an ISO 3166 two-letter country code.
40
+ # @return [Array<String>] if the ownership lists 'excluded' territories,
41
+ # the territories where the owner does not own the asset.
42
+ # @return [nil] if the ownership does not list 'excluded' territories,
43
+ def excluded_territories
44
+ territories if type == 'exclude'
45
+ end
46
+
47
+ # @return [Boolean] whether the ownership applies to the whole world.
48
+ def everywhere?
49
+ excluded_territories == []
50
+ end
51
+
52
+ private
53
+
54
+ has_attribute :type
55
+ has_attribute :territories, default: []
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,50 @@
1
+ require 'yt/models/description'
2
+
3
+ module Yt
4
+ module Models
5
+ # @private
6
+ # Provides methods to interact with the snippet of YouTube resources.
7
+ # @see https://developers.google.com/youtube/v3/docs/channels#resource
8
+ # @see https://developers.google.com/youtube/v3/docs/videos#resource
9
+ # @see https://developers.google.com/youtube/v3/docs/playlists#resource
10
+ # @see https://developers.google.com/youtube/v3/docs/playlistItems#resource
11
+ class Snippet < Base
12
+ attr_reader :data
13
+
14
+ def initialize(options = {})
15
+ @data = options[:data]
16
+ @auth = options[:auth]
17
+ end
18
+
19
+ has_attribute :title, default: ''
20
+ has_attribute(:description, default: '') {|text| Description.new text}
21
+ has_attribute :published_at, type: Time
22
+ has_attribute :channel_id
23
+ has_attribute :channel_title
24
+ has_attribute :tags, default: []
25
+ has_attribute :category_id
26
+ has_attribute :live_broadcast_content
27
+ has_attribute :playlist_id
28
+ has_attribute :position, type: Integer
29
+ has_attribute :resource_id, default: {}
30
+ has_attribute :thumbnails, default: {}
31
+
32
+ def thumbnail_url(size = :default)
33
+ thumbnails.fetch(size.to_s, {})['url']
34
+ end
35
+
36
+ # Returns whether YouTube API includes all attributes in this snippet.
37
+ # For instance, YouTube API only returns tags and categoryId on
38
+ # Videos#list, not on Videos#search. And returns position on
39
+ # PlaylistItems#list, not on PlaylistItems#insert.
40
+ # This method is used to guarantee that, when a video was instantiated
41
+ # by one of the methods above, an additional call to is made to retrieve
42
+ # the full snippet in case an attribute is missing.
43
+ # @see https://developers.google.com/youtube/v3/docs/videos
44
+ # @return [Boolean] whether YouTube API includes the complete snippet.
45
+ def complete?
46
+ @complete ||= data.fetch :complete, true
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,26 @@
1
+ require 'yt/models/base'
2
+
3
+ module Yt
4
+ module Models
5
+ # @private
6
+ # Encapsulates statistics about the resource, such as the number of times
7
+ # the resource has been viewed or liked.
8
+ # @see https://developers.google.com/youtube/v3/docs/videos#resource
9
+ class StatisticsSet < Base
10
+ attr_reader :data
11
+
12
+ def initialize(options = {})
13
+ @data = options[:data]
14
+ end
15
+
16
+ has_attribute :view_count, type: Integer
17
+ has_attribute :comment_count, type: Integer
18
+ has_attribute :like_count, type: Integer
19
+ has_attribute :dislike_count, type: Integer
20
+ has_attribute :favorite_count, type: Integer
21
+ has_attribute :video_count, type: Integer
22
+ has_attribute :subscriber_count, type: Integer
23
+ has_attribute :hidden_subscriber_count
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,32 @@
1
+ require 'yt/models/timestamp'
2
+
3
+ module Yt
4
+ module Models
5
+ # @private
6
+ # Contains information about the status of a resource. The details of the
7
+ # status are different for the different types of resources.
8
+ #
9
+ # Resources with a
10
+ # status are: channels, playlists, playlist items and videos.
11
+ # @see https://developers.google.com/youtube/v3/docs/channels#resource
12
+ # @see https://developers.google.com/youtube/v3/docs/videos#resource
13
+ # @see https://developers.google.com/youtube/v3/docs/playlists#resource
14
+ # @see https://developers.google.com/youtube/v3/docs/playlistItems#resource
15
+ class Status < Base
16
+ attr_reader :data
17
+
18
+ def initialize(options = {})
19
+ @data = options[:data]
20
+ end
21
+
22
+ has_attribute :privacy_status
23
+ has_attribute :upload_status
24
+ has_attribute :failure_reason
25
+ has_attribute :rejection_reason
26
+ has_attribute :license
27
+ has_attribute :embeddable
28
+ has_attribute :public_stats_viewable
29
+ has_attribute :publish_at, type: Time
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,38 @@
1
+ require 'yt/models/base'
2
+
3
+ module Yt
4
+ module Models
5
+ # @private
6
+ # Provides methods to interact with YouTube subscriptions.
7
+ # @see https://developers.google.com/youtube/v3/docs/subscriptions
8
+ class Subscription < Base
9
+ # @return [String] the ID that uniquely identify a YouTube subscription.
10
+ attr_reader :id
11
+
12
+ def initialize(options = {})
13
+ @id = options[:id]
14
+ @auth = options[:auth]
15
+ end
16
+
17
+ def delete(options = {}, &block)
18
+ do_delete {@id = nil}
19
+ !exists?
20
+ end
21
+
22
+ def exists?
23
+ !@id.nil?
24
+ end
25
+
26
+ private
27
+
28
+ # @return [Hash] the parameters to submit to YouTube to delete a subscription.
29
+ # @see https://developers.google.com/youtube/v3/docs/subscriptions/delete
30
+ def delete_params
31
+ super.tap do |params|
32
+ params[:path] = '/youtube/v3/subscriptions'
33
+ params[:params] = {id: @id}
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,13 @@
1
+ module Yt
2
+ module Models
3
+ # @private
4
+ # Extend Time to have .to_json return the format needed to submit
5
+ # timestamp to YouTube API.
6
+ # Only needed while we support ActiveSupport 3.
7
+ class Timestamp < Time
8
+ def as_json(options = nil)
9
+ utc.iso8601(3)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,90 @@
1
+ module Yt
2
+ module Models
3
+ # @private
4
+ class URL
5
+ attr_reader :kind
6
+
7
+ def initialize(url)
8
+ @url = url
9
+ @kind ||= parse url
10
+ @match_data ||= {}
11
+ end
12
+
13
+ def id
14
+ @match_data[:id]
15
+ rescue IndexError
16
+ end
17
+
18
+ def username
19
+ @match_data[:username]
20
+ rescue IndexError
21
+ end
22
+
23
+ private
24
+
25
+ def parse(url)
26
+ matching_pattern = patterns.find do |pattern|
27
+ @match_data = url.match pattern[:regex]
28
+ end
29
+ matching_pattern[:kind] if matching_pattern
30
+ end
31
+
32
+ def patterns
33
+ # @note: :channel *must* be the last since one of its regex eats the
34
+ # remaining patterns. In short, don't change the following order
35
+
36
+ @patterns ||= patterns_for :playlist, :subscription, :video, :channel
37
+ end
38
+
39
+ def patterns_for(*kinds)
40
+ prefix = '^(?:https?://)?(?:www\.)?'
41
+ suffix = '(?:|/)'
42
+ kinds.map do |kind|
43
+ patterns = send "#{kind}_patterns" # meta programming :/
44
+ patterns.map do |pattern|
45
+ {kind: kind, regex: %r{#{prefix}#{pattern}#{suffix}}}
46
+ end
47
+ end.flatten
48
+ end
49
+
50
+ def subscription_patterns
51
+ name = '(?:[a-zA-Z0-9&_=-]*)'
52
+
53
+ %W{
54
+ subscription_center\\?add_user=#{name}
55
+ subscribe_widget\\?p=#{name}
56
+ channel/#{name}\\?sub_confirmation=1
57
+ }.map{|path| "youtube\\.com/#{path}"}
58
+ end
59
+
60
+ def playlist_patterns
61
+ playlist_id = '(?<id>[a-zA-Z0-9_-]+)'
62
+
63
+ %W{
64
+ playlist\\?list=#{playlist_id}
65
+ }.map{|path| "youtube\\.com/#{path}"}
66
+ end
67
+
68
+ def video_patterns
69
+ video_id = '(?<id>[a-zA-Z0-9_-]+)'
70
+
71
+ %W{
72
+ youtube\\.com/watch\\?v=#{video_id}
73
+ youtu\\.be/#{video_id}
74
+ youtube\\.com/embed/#{video_id}
75
+ }
76
+ end
77
+
78
+ def channel_patterns
79
+ channel_id = '(?<id>[a-zA-Z0-9_-]+)'
80
+ username = '(?<username>[a-zA-Z0-9_-]+)'
81
+
82
+ %W{
83
+ channel/#{channel_id}
84
+ user/#{username}
85
+ #{username}
86
+ }.map{|path| "youtube\\.com/#{path}"}
87
+ end
88
+ end
89
+ end
90
+ end