Skip to content

Commit

Permalink
Merge 946e731 into 38c6892
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-fs committed Jun 28, 2017
2 parents 38c6892 + 946e731 commit f6bc7e7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/yt/http_request.rb
Expand Up @@ -52,10 +52,24 @@ def run
else
raise Yt::HTTPError, error_message
end
rescue Yt::ConnectionError
retry_run
end

private

# retry the run method in case of a random 500 error from YouTube API
def retry_run
if @retried
raise Yt::ConnectionError, error_message
else
@retried = true
@response = nil
sleep 5
run
end
end

# @return [URI::HTTPS] the (memoized) URI of the request.
def uri
attributes = {host: @host, path: @path, query: URI.encode_www_form(query)}
Expand Down
15 changes: 15 additions & 0 deletions spec/http_request_spec.rb
Expand Up @@ -32,4 +32,19 @@
expect{request.run}.to raise_error Yt::ConnectionError
end
end

context 'given that YouTube API raises an error' do
context 'only once' do
path = '/discovery/v1/apis/youtube/v3/rest'
headers = {'User-Agent' => 'Yt::HTTPRequest'}
params = {verbose: 1}
let(:request) { Yt::HTTPRequest.new path: path, headers: headers, params: params }
let(:retry_response) { Net::HTTPSuccess.new '1.0', '200', 'OK' }
before { expect(Net::HTTP).to receive(:start).at_least(:once).and_return retry_response }
it 'returns the HTTP response with the JSON-parsed body' do
allow(retry_response).to receive(:body).and_return('{"fake": "body"}')
expect { request.run }.not_to raise_error
end
end
end
end

0 comments on commit f6bc7e7

Please sign in to comment.