Skip to content

Commit

Permalink
Merge 6a89775 into 38c6892
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-fs committed Jun 17, 2017
2 parents 38c6892 + 6a89775 commit 8497a8d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/yt/http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,18 @@ def run
parse_response!
end
else
retry_run
end
end

def retry_run
if @retry
raise Yt::HTTPError, error_message
else
@retry = true
@response = nil
sleep 10
run
end
end

Expand Down
15 changes: 15 additions & 0 deletions spec/http_request_spec.rb
Original file line number Diff line number Diff line change
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 8497a8d

Please sign in to comment.