Skip to content

Commit

Permalink
Merge pull request #52 from MaximeD/fix_api_too_many_requests
Browse files Browse the repository at this point in the history
FIX making too many requests
  • Loading branch information
MaximeD committed Feb 13, 2016
2 parents 42faf18 + 625ed60 commit 6aa7908
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 6aa7908

Please sign in to comment.