Skip to content

Commit

Permalink
FIX making too many requests
Browse files Browse the repository at this point in the history
We may be trigerring too many requests to rubygems.
In which case it won't send us the information we need.
If such a thing happens, we should wait a while before making a new
request.

Details
* FIX making too many requests
* ADD specs
  • Loading branch information
MaximeD committed Feb 13, 2016
1 parent 42faf18 commit 625ed60
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/gem_updater/ruby_gems_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,17 @@ def source_uri
# @return [String|nil] uri of source code
def uri_from_rubygems
return unless source.remotes.map( &:host ).include?( 'rubygems.org' )
tries = 0

response = begin
JSON.parse( open( "https://rubygems.org/api/v1/gems/#{gem_name}.json" ).read )
rescue OpenURI::HTTPError
rescue OpenURI::HTTPError => e
# We may trigger too many requests, in which case give rubygems a break
if e.io.status.include?( '429' )
if ( tries += 1 ) < 2
sleep 1 and retry
end
end
end

if response
Expand Down
12 changes: 12 additions & 0 deletions spec/gem_updater/ruby_gems_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@

describe '#source_uri' do
context 'when gem exists on rubygems.org' do

describe 'making too many requests' do
before do
allow( subject ).to receive_message_chain( :open ) { raise OpenURI::HTTPError.new( '429', OpenStruct.new( status: [ '429' ] ) ) }
subject.source_uri
end

it 'tries again' do
expect( subject ).to have_received( :open ).twice
end
end

context "when 'source_code_uri' is present" do
before do
allow( subject ).to receive_message_chain( :open, :read ) { { source_code_uri: 'source_code_uri' }.to_json }
Expand Down

0 comments on commit 625ed60

Please sign in to comment.