Skip to content

Commit

Permalink
Refactor check and raise failure
Browse files Browse the repository at this point in the history
- 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
```
  • Loading branch information
tagliala committed Apr 20, 2024
1 parent 9fe05d3 commit 09b2814
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
18 changes: 1 addition & 17 deletions .rubocop_todo.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/sharepoint/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion spec/lib/sharepoint/client_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
end

describe '#search' do
subject { client.search(options) }
subject(:search) { client.search(options) }

before { mock_responses('search_modified_documents.json') }

Expand All @@ -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
Expand Down

0 comments on commit 09b2814

Please sign in to comment.