From 625ed60a02bb20edf7ce8b0b185f58fb1a4f1b01 Mon Sep 17 00:00:00 2001 From: Maxime Demolin Date: Sat, 13 Feb 2016 17:14:41 +0100 Subject: [PATCH] FIX making too many requests 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 --- lib/gem_updater/ruby_gems_fetcher.rb | 9 ++++++++- spec/gem_updater/ruby_gems_fetcher_spec.rb | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/gem_updater/ruby_gems_fetcher.rb b/lib/gem_updater/ruby_gems_fetcher.rb index 3a99854..de128a9 100644 --- a/lib/gem_updater/ruby_gems_fetcher.rb +++ b/lib/gem_updater/ruby_gems_fetcher.rb @@ -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 diff --git a/spec/gem_updater/ruby_gems_fetcher_spec.rb b/spec/gem_updater/ruby_gems_fetcher_spec.rb index 94a3d76..af87fb5 100644 --- a/spec/gem_updater/ruby_gems_fetcher_spec.rb +++ b/spec/gem_updater/ruby_gems_fetcher_spec.rb @@ -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 }