From 09b281444966afc6063de5c6f01d4a5835c63f9c Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Sat, 20 Apr 2024 16:46:08 +0200 Subject: [PATCH] Refactor check and raise failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Use `cover?` which is much faster than `include?` - Use a guard clause to improve readability - Add a spec ``` Comparison (IPS): cover?: 4483987.3 i/s include?: 2890227.4 i/s - 1.55x (± 0.00) slower ``` --- .rubocop_todo.yml | 18 +----------------- lib/sharepoint/client.rb | 6 +++--- spec/lib/sharepoint/client_methods_spec.rb | 14 +++++++++++++- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4efbae3..976261a 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -52,7 +52,7 @@ Metrics/AbcSize: # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 514 + Max: 513 # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. Metrics/MethodLength: @@ -69,11 +69,6 @@ Performance/CollectionLiteralInLoop: Exclude: - 'lib/sharepoint/client.rb' -# This cop supports unsafe autocorrection (--autocorrect-all). -Performance/RangeInclude: - Exclude: - - 'lib/sharepoint/client.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). Performance/UnfreezeString: Exclude: @@ -137,17 +132,6 @@ Style/FrozenStringLiteralComment: - 'lib/sharepoint/errors.rb' - 'lib/sharepoint/spec_helpers.rb' -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: MinBodyLength, AllowConsecutiveConditionals. -Style/GuardClause: - Exclude: - - 'lib/sharepoint/client.rb' - -# This cop supports safe autocorrection (--autocorrect). -Style/IfUnlessModifier: - Exclude: - - 'lib/sharepoint/client.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). Style/MapIntoArray: Exclude: diff --git a/lib/sharepoint/client.rb b/lib/sharepoint/client.rb index 6824dd0..eca933c 100644 --- a/lib/sharepoint/client.rb +++ b/lib/sharepoint/client.rb @@ -533,9 +533,9 @@ def last_location_header(ethon) end def check_and_raise_failure(ethon) - unless (200..299).include? ethon.response_code - raise "Request failed, received #{ethon.response_code}:\n#{ethon.url}\n#{ethon.response_body}" - end + return if (200..299).cover? ethon.response_code + + raise "Request failed, received #{ethon.response_code}:\n#{ethon.url}\n#{ethon.response_body}" end def prepare_metadata(metadata, type) diff --git a/spec/lib/sharepoint/client_methods_spec.rb b/spec/lib/sharepoint/client_methods_spec.rb index f01a102..1a4f8a9 100644 --- a/spec/lib/sharepoint/client_methods_spec.rb +++ b/spec/lib/sharepoint/client_methods_spec.rb @@ -253,7 +253,7 @@ end describe '#search' do - subject { client.search(options) } + subject(:search) { client.search(options) } before { mock_responses('search_modified_documents.json') } @@ -267,6 +267,18 @@ end it { is_expected.not_to be_empty } + + context 'when request fails' do + before do + allow_any_instance_of(Ethon::Easy) + .to receive(:response_code) + .and_return(401) + end + + it 'raises an error' do + expect { search }.to raise_error(/\ARequest failed, received 401.+/) + end + end end describe '#download' do