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,12 @@
|
|
1
|
+
require 'yt/models/resource'
|
2
|
+
|
3
|
+
module Yt
|
4
|
+
module Models
|
5
|
+
# @private
|
6
|
+
# Provides methods to interact with YouTube video categories.
|
7
|
+
# @see https://developers.google.com/youtube/v3/docs/videoCategories
|
8
|
+
class VideoCategory < Resource
|
9
|
+
delegate :data, :title, to: :snippet
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/yt/request.rb
ADDED
@@ -0,0 +1,278 @@
|
|
1
|
+
require 'net/http' # for Net::HTTP.start
|
2
|
+
require 'uri' # for URI.json
|
3
|
+
require 'json' # for JSON.parse
|
4
|
+
require 'active_support' # does not load anything by default but is required
|
5
|
+
require 'active_support/core_ext' # for Hash.from_xml, Hash.to_param
|
6
|
+
|
7
|
+
require 'yt/errors/forbidden'
|
8
|
+
require 'yt/errors/missing_auth'
|
9
|
+
require 'yt/errors/request_error'
|
10
|
+
require 'yt/errors/server_error'
|
11
|
+
require 'yt/errors/unauthorized'
|
12
|
+
|
13
|
+
module Yt
|
14
|
+
# @private
|
15
|
+
# A wrapper around Net::HTTP to send HTTP requests to any web API and
|
16
|
+
# return their result or raise an error if the result is unexpected.
|
17
|
+
# The basic way to use Request is by calling +run+ on an instance.
|
18
|
+
# @example List the most popular videos on YouTube.
|
19
|
+
# host = ''www.googleapis.com'
|
20
|
+
# path = '/youtube/v3/videos'
|
21
|
+
# params = {chart: 'mostPopular', key: ENV['API_KEY'], part: 'snippet'}
|
22
|
+
# response = Yt::Request.new(path: path, params: params).run
|
23
|
+
# response.body['items'].map{|video| video['snippet']['title']}
|
24
|
+
#
|
25
|
+
class Request
|
26
|
+
# Initializes a Request object.
|
27
|
+
# @param [Hash] options the options for the request.
|
28
|
+
# @option options [String, Symbol] :method (:get) The HTTP method to use.
|
29
|
+
# @option options [Class] :expected_response (Net::HTTPSuccess) The class
|
30
|
+
# of response that the request should obtain when run.
|
31
|
+
# @option options [String, Symbol] :response_format (:json) The expected
|
32
|
+
# format of the response body. If passed, the response body will be
|
33
|
+
# parsed according to the format before being returned.
|
34
|
+
# @option options [String] :host The host component of the request URI.
|
35
|
+
# @option options [String] :path The path component of the request URI.
|
36
|
+
# @option options [Hash] :params ({}) The params to use as the query
|
37
|
+
# component of the request URI, for instance the Hash +{a: 1, b: 2}+
|
38
|
+
# corresponds to the query parameters "a=1&b=2".
|
39
|
+
# @option options [Hash] :camelize_params (true) whether to transform
|
40
|
+
# each key of params into a camel-case symbol before sending the request.
|
41
|
+
# @option options [Hash] :request_format (:json) The format of the
|
42
|
+
# requesty body. If a request body is passed, it will be parsed
|
43
|
+
# according to this format before sending it in the request.
|
44
|
+
# @option options [#size] :body The body component of the request.
|
45
|
+
# @option options [Hash] :headers ({}) The headers component of the
|
46
|
+
# request.
|
47
|
+
# @option options [#access_token, #refreshed_access_token?] :auth The
|
48
|
+
# authentication object. If set, must respond to +access_token+ and
|
49
|
+
# return the OAuth token to make an authenticated request, and must
|
50
|
+
# respond to +refreshed_access_token?+ and return whether the access
|
51
|
+
# token can be refreshed if expired.
|
52
|
+
def initialize(options = {})
|
53
|
+
@method = options.fetch :method, :get
|
54
|
+
@expected_response = options.fetch :expected_response, Net::HTTPSuccess
|
55
|
+
@response_format = options.fetch :response_format, :json
|
56
|
+
@host = options[:host]
|
57
|
+
@path = options[:path]
|
58
|
+
@params = options.fetch :params, {}
|
59
|
+
# Note: This is to be invoked by auth-only YouTube APIs.
|
60
|
+
@params[:key] = options[:api_key] if options[:api_key]
|
61
|
+
# Note: This is to be invoked by all YouTube API except Annotations,
|
62
|
+
# Analyitics and Uploads
|
63
|
+
camelize_keys! @params if options.fetch(:camelize_params, true)
|
64
|
+
@request_format = options.fetch :request_format, :json
|
65
|
+
@body = options[:body]
|
66
|
+
@headers = options.fetch :headers, {}
|
67
|
+
@auth = options[:auth]
|
68
|
+
end
|
69
|
+
|
70
|
+
# Sends the request and returns the response.
|
71
|
+
# If the request fails once for a temporary server error or an expired
|
72
|
+
# token, tries the request again before eventually raising an error.
|
73
|
+
# @return [Net::HTTPResponse] if the request succeeds and matches the
|
74
|
+
# expectations, the response with the body appropriately parsed.
|
75
|
+
# @raise [Yt::RequestError] if the request fails or the response does
|
76
|
+
# not match the expectations.
|
77
|
+
def run
|
78
|
+
if matches_expectations?
|
79
|
+
response.tap{parse_response!}
|
80
|
+
elsif run_again?
|
81
|
+
run
|
82
|
+
else
|
83
|
+
raise response_error, error_message.to_json
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# Returns the +cURL+ version of the request, useful to re-run the request
|
88
|
+
# in a shell terminal.
|
89
|
+
# @return [String] the +cURL+ version of the request.
|
90
|
+
def as_curl
|
91
|
+
'curl'.tap do |curl|
|
92
|
+
curl << " -X #{http_request.method}"
|
93
|
+
http_request.each_header{|k, v| curl << %Q{ -H "#{k}: #{v}"}}
|
94
|
+
curl << %Q{ -d '#{http_request.body}'} if http_request.body
|
95
|
+
curl << %Q{ "#{uri.to_s}"}
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
# @return [URI::HTTPS] the (memoized) URI of the request.
|
102
|
+
def uri
|
103
|
+
attributes = {host: @host, path: @path, query: @params.to_param}
|
104
|
+
@uri ||= URI::HTTPS.build attributes
|
105
|
+
end
|
106
|
+
|
107
|
+
# @return [Net::HTTPRequest] the full HTTP request object,
|
108
|
+
# inclusive of headers of request body.
|
109
|
+
def http_request
|
110
|
+
net_http_class = "Net::HTTP::#{@method.capitalize}".constantize
|
111
|
+
@http_request ||= net_http_class.new(uri.request_uri).tap do |request|
|
112
|
+
set_request_body! request
|
113
|
+
set_request_headers! request
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
# Adds the request body to the request in the appropriate format.
|
118
|
+
# if the request body is a JSON Object, transform its keys into camel-case,
|
119
|
+
# since this is the common format for JSON APIs.
|
120
|
+
def set_request_body!(request)
|
121
|
+
case @request_format
|
122
|
+
when :json then request.body = (camelize_keys! @body).to_json
|
123
|
+
when :form then request.set_form_data @body
|
124
|
+
when :file then request.body_stream = @body
|
125
|
+
end if @body
|
126
|
+
end
|
127
|
+
|
128
|
+
# Destructively converts all the keys of hash to camel-case symbols.
|
129
|
+
# Note: This is to be invoked by all YouTube API except Accounts
|
130
|
+
def camelize_keys!(hash)
|
131
|
+
hash.keys.each do |key|
|
132
|
+
hash[key.to_s.camelize(:lower).to_sym] = hash.delete key
|
133
|
+
end if hash.is_a? Hash
|
134
|
+
hash
|
135
|
+
end
|
136
|
+
|
137
|
+
# Adds the request headers to the request in the appropriate format.
|
138
|
+
# The User-Agent header is also set to recognize the request, and to
|
139
|
+
# tell the server that gzip compression can be used, since Net::HTTP
|
140
|
+
# supports it and automatically sets the Accept-Encoding header.
|
141
|
+
def set_request_headers!(request)
|
142
|
+
case @request_format
|
143
|
+
when :json
|
144
|
+
request.initialize_http_header 'Content-Type' => 'application/json'
|
145
|
+
request.initialize_http_header 'Content-length' => '0' unless @body
|
146
|
+
when :file
|
147
|
+
request.initialize_http_header 'Content-Length' => @body.size.to_s
|
148
|
+
request.initialize_http_header 'Transfer-Encoding' => 'chunked'
|
149
|
+
end
|
150
|
+
@headers['User-Agent'] = 'Yt::Request (gzip)'
|
151
|
+
@headers['Authorization'] = "Bearer #{@auth.access_token}" if @auth
|
152
|
+
@headers.each{|name, value| request.add_field name, value}
|
153
|
+
end
|
154
|
+
|
155
|
+
# @return [Boolean] whether the class of response returned by running
|
156
|
+
# the request matches the expected class of response.
|
157
|
+
def matches_expectations?
|
158
|
+
response.is_a? @expected_response
|
159
|
+
end
|
160
|
+
|
161
|
+
# Run the request and memoize the response or the server error received.
|
162
|
+
def response
|
163
|
+
@response ||= send_http_request
|
164
|
+
rescue *server_errors => e
|
165
|
+
@response ||= e
|
166
|
+
end
|
167
|
+
|
168
|
+
# Send the request to the server, allowing ActiveSupport::Notifications
|
169
|
+
# client to subscribe to the request.
|
170
|
+
def send_http_request
|
171
|
+
net_http_options = [uri.host, uri.port, use_ssl: true]
|
172
|
+
ActiveSupport::Notifications.instrument 'request.yt' do |payload|
|
173
|
+
payload[:method] = @method
|
174
|
+
payload[:request_uri] = uri
|
175
|
+
payload[:response] = Net::HTTP.start(*net_http_options) do |http|
|
176
|
+
http.request http_request
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
# Replaces the body of the response with the parsed version of the body,
|
182
|
+
# according to the format specified in the Request.
|
183
|
+
def parse_response!
|
184
|
+
response.body = case @response_format
|
185
|
+
when :xml then Hash.from_xml response.body
|
186
|
+
when :json then JSON response.body
|
187
|
+
end if response.body
|
188
|
+
end
|
189
|
+
|
190
|
+
# Returns whether it is worth to run a failed request again.
|
191
|
+
# There are two cases in which retrying a request might be worth:
|
192
|
+
# - when the server specifies that the request token has expired and
|
193
|
+
# the user has to refresh the token in order to tryi again
|
194
|
+
# - when the server is unreachable, and waiting for a couple of seconds
|
195
|
+
# might solve the connection issues.
|
196
|
+
def run_again?
|
197
|
+
refresh_token_and_retry? || server_error? && sleep_and_retry?
|
198
|
+
end
|
199
|
+
|
200
|
+
# Returns the list of server errors worth retrying the request once.
|
201
|
+
def server_errors
|
202
|
+
[
|
203
|
+
OpenSSL::SSL::SSLError,
|
204
|
+
Errno::ETIMEDOUT,
|
205
|
+
Errno::EHOSTUNREACH,
|
206
|
+
Errno::ENETUNREACH,
|
207
|
+
Errno::ECONNRESET,
|
208
|
+
Net::HTTPServerError
|
209
|
+
] + extra_server_errors
|
210
|
+
end
|
211
|
+
|
212
|
+
# Returns the list of server errors that are only raised (and therefore
|
213
|
+
# can only be rescued) by specific versions of Ruby.
|
214
|
+
# @see: https://github.com/Fullscreen/yt/pull/110
|
215
|
+
def extra_server_errors
|
216
|
+
if defined? OpenSSL::SSL::SSLErrorWaitReadable
|
217
|
+
[OpenSSL::SSL::SSLErrorWaitReadable]
|
218
|
+
else
|
219
|
+
[]
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
# Sleeps for a while and returns true for the first +max_retries+ times,
|
224
|
+
# then returns false. Useful to try the same request again multiple
|
225
|
+
# times with a delay if a connection error occurs.
|
226
|
+
def sleep_and_retry?(max_retries = 1)
|
227
|
+
@retries_so_far ||= -1
|
228
|
+
@retries_so_far += 1
|
229
|
+
if (@retries_so_far < max_retries)
|
230
|
+
@response = @http_request = @uri = nil
|
231
|
+
sleep 3
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
# In case an authorized request responds with "Unauthorized", checks
|
236
|
+
# if the original access token can be refreshed. If that's the case,
|
237
|
+
# clears the memoized variables and returns true, so the request can
|
238
|
+
# be run again, otherwise raises an error.
|
239
|
+
def refresh_token_and_retry?
|
240
|
+
if unauthorized? && @auth && @auth.refreshed_access_token?
|
241
|
+
@response = @http_request = @uri = nil
|
242
|
+
true
|
243
|
+
end
|
244
|
+
rescue Errors::MissingAuth
|
245
|
+
false
|
246
|
+
end
|
247
|
+
|
248
|
+
# @return [Yt::RequestError] the error associated to the class of the
|
249
|
+
# response.
|
250
|
+
def response_error
|
251
|
+
case response
|
252
|
+
when *server_errors then Errors::ServerError
|
253
|
+
when Net::HTTPUnauthorized then Errors::Unauthorized
|
254
|
+
when Net::HTTPForbidden then Errors::Forbidden
|
255
|
+
else Errors::RequestError
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
# @return [Boolean] whether the response matches any server error.
|
260
|
+
def server_error?
|
261
|
+
response_error == Errors::ServerError
|
262
|
+
end
|
263
|
+
|
264
|
+
# @return [Boolean] whether the request lacks proper authorization.
|
265
|
+
def unauthorized?
|
266
|
+
response_error == Errors::Unauthorized
|
267
|
+
end
|
268
|
+
|
269
|
+
# Return the elements of the request/response that are worth displaying
|
270
|
+
# as an error message if the request fails.
|
271
|
+
# If the response format is JSON, showing the parsed body is sufficient,
|
272
|
+
# otherwise the whole (inspected) response is worth looking at.
|
273
|
+
def error_message
|
274
|
+
response_body = JSON(response.body) rescue response.inspect
|
275
|
+
{request_curl: as_curl, response_body: response_body}
|
276
|
+
end
|
277
|
+
end
|
278
|
+
end
|
data/lib/yt/version.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'yt/collections/claims'
|
3
|
+
require 'yt/models/content_owner'
|
4
|
+
|
5
|
+
describe Yt::Collections::Claims do
|
6
|
+
subject(:collection) { Yt::Collections::Claims.new parent: content_owner }
|
7
|
+
let(:content_owner) { Yt::ContentOwner.new owner_name: 'any-name' }
|
8
|
+
let(:page) { {items: [], token: 'any-token'} }
|
9
|
+
let(:query) { {q: 'search string'} }
|
10
|
+
|
11
|
+
describe '#count' do
|
12
|
+
context 'called once with .where(query) and once without' do
|
13
|
+
after do
|
14
|
+
collection.where(query).count
|
15
|
+
collection.count
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'only applies the query on the first call' do
|
19
|
+
expect(collection).to receive(:fetch_page) do |options|
|
20
|
+
expect(options[:params]).to include query
|
21
|
+
page
|
22
|
+
end
|
23
|
+
expect(collection).to receive(:fetch_page) do |options|
|
24
|
+
expect(options[:params]).not_to include query
|
25
|
+
page
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'yt/models/playlist'
|
3
|
+
require 'yt/models/playlist_item'
|
4
|
+
require 'yt/collections/playlist_items'
|
5
|
+
|
6
|
+
describe Yt::Collections::PlaylistItems do
|
7
|
+
subject(:collection) { Yt::Collections::PlaylistItems.new parent: playlist }
|
8
|
+
let(:playlist) { Yt::Playlist.new id: 'LLxO1tY8h1AhOz0T4ENwmpow' }
|
9
|
+
let(:attrs) { {id: 'MESycYJytkU', kind: :video} }
|
10
|
+
let(:msg) { {response_body: {error: {errors: [{reason: reason}]}}}.to_json }
|
11
|
+
before { expect(collection).to behave }
|
12
|
+
|
13
|
+
describe '#insert' do
|
14
|
+
let(:playlist_item) { Yt::PlaylistItem.new }
|
15
|
+
|
16
|
+
context 'given an existing video' do
|
17
|
+
let(:behave) { receive(:do_insert).and_return playlist_item }
|
18
|
+
|
19
|
+
it { expect(collection.insert attrs).to eq playlist_item }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'given an unknown video' do
|
23
|
+
let(:reason) { 'videoNotFound' }
|
24
|
+
let(:behave) { receive(:do_insert).and_raise Yt::Error, msg }
|
25
|
+
|
26
|
+
it { expect{collection.insert attrs}.to fail.with 'videoNotFound' }
|
27
|
+
it { expect{collection.insert attrs, ignore_errors: true}.not_to fail }
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'given a forbidden video' do
|
31
|
+
let(:reason) { 'forbidden' }
|
32
|
+
let(:behave) { receive(:do_insert).and_raise Yt::Error, msg }
|
33
|
+
|
34
|
+
it { expect{collection.insert attrs}.to fail.with 'forbidden' }
|
35
|
+
it { expect{collection.insert attrs, ignore_errors: true}.not_to fail }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#delete_all' do
|
40
|
+
let(:behave) { receive(:do_delete_all).and_return [true] }
|
41
|
+
|
42
|
+
it { expect(collection.delete_all).to eq [true] }
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'yt/collections/playlists'
|
3
|
+
|
4
|
+
describe Yt::Collections::Playlists do
|
5
|
+
subject(:collection) { Yt::Collections::Playlists.new }
|
6
|
+
before { expect(collection).to behave }
|
7
|
+
|
8
|
+
describe '#insert' do
|
9
|
+
let(:playlist) { Yt::Playlist.new }
|
10
|
+
# TODO: separate stubs to show options translate into do_insert params
|
11
|
+
let(:behave) { receive(:do_insert).and_return playlist }
|
12
|
+
|
13
|
+
it { expect(collection.insert).to eq playlist }
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#delete_all' do
|
17
|
+
let(:behave) { receive(:do_delete_all).and_return [true] }
|
18
|
+
|
19
|
+
it { expect(collection.delete_all).to eq [true] }
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#delete_all' do
|
23
|
+
let(:behave) { receive(:do_delete_all).and_return [true] }
|
24
|
+
|
25
|
+
it { expect(collection.delete_all).to eq [true] }
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'yt/collections/policies'
|
3
|
+
require 'yt/models/content_owner'
|
4
|
+
|
5
|
+
describe Yt::Collections::Policies do
|
6
|
+
subject(:collection) { Yt::Collections::Policies.new parent: content_owner }
|
7
|
+
let(:content_owner) { Yt::ContentOwner.new owner_name: 'any-name' }
|
8
|
+
let(:page) { {items: [], token: 'any-token'} }
|
9
|
+
let(:query) { {id: 'any-id'} }
|
10
|
+
|
11
|
+
describe '#count' do
|
12
|
+
context 'called once with .where(query) and once without' do
|
13
|
+
after do
|
14
|
+
collection.where(query).count
|
15
|
+
collection.count
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'only applies the query on the first call' do
|
19
|
+
expect(collection).to receive(:fetch_page) do |options|
|
20
|
+
expect(options[:params]).to include query
|
21
|
+
page
|
22
|
+
end
|
23
|
+
expect(collection).to receive(:fetch_page) do |options|
|
24
|
+
expect(options[:params]).not_to include query
|
25
|
+
page
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'yt/collections/references'
|
3
|
+
require 'yt/models/content_owner'
|
4
|
+
|
5
|
+
describe Yt::Collections::References do
|
6
|
+
subject(:collection) { Yt::Collections::References.new parent: content_owner }
|
7
|
+
let(:content_owner) { Yt::ContentOwner.new id: 'any-id' }
|
8
|
+
let(:page) { {items: [], token: 'any-token'} }
|
9
|
+
let(:query) { {id: 'reference-id'} }
|
10
|
+
|
11
|
+
describe '#count' do
|
12
|
+
context 'called once with .where(query) and once without' do
|
13
|
+
after do
|
14
|
+
collection.where(query).count
|
15
|
+
collection.count
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'only applies the query on the first call' do
|
19
|
+
expect(collection).to receive(:fetch_page) do |options|
|
20
|
+
expect(options[:params]).to include query
|
21
|
+
page
|
22
|
+
end
|
23
|
+
expect(collection).to receive(:fetch_page) do |options|
|
24
|
+
expect(options[:params]).not_to include query
|
25
|
+
page
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|