Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change FormatParser.parse_http to follow a redirect #190

Merged
merged 5 commits into from
Feb 17, 2021

Conversation

fabioperrella
Copy link
Contributor

@fabioperrella fabioperrella commented Feb 10, 2021

closes #189

This PR adds support to follow a redirect when downloading a file with FormatParser.parse_http

I was inspired by this article which explains how to follow a redirect using Faraday

@fabioperrella fabioperrella self-assigned this Feb 10, 2021
@fabioperrella fabioperrella marked this pull request as ready for review February 10, 2021 15:54
@fabioperrella fabioperrella marked this pull request as draft February 11, 2021 13:36
@fabioperrella fabioperrella changed the base branch from master to using-gh-actions February 12, 2021 08:54
@fabioperrella fabioperrella marked this pull request as ready for review February 12, 2021 08:57
@fabioperrella fabioperrella changed the base branch from using-gh-actions to master February 12, 2021 12:57
Copy link
Contributor

@lorenzograndi4 lorenzograndi4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but I'd add a comment (with a link?) to explain why we need default_adapter in the configuration

Copy link
Contributor

@lorenzograndi4 lorenzograndi4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we set a limit to the number of redirects to follow?

@fabioperrella
Copy link
Contributor Author

Could we set a limit to the number of redirects to follow?

@lorenzograndi4 I saw here that the default value is 3.

It seems a reasonable value for me.

Do you think we should use another value?

@lorenzograndi4
Copy link
Contributor

3 sounds good !

@@ -7,7 +7,9 @@
rio = described_class.new('https://images.invalid/img.jpg')

fake_resp = double(headers: {'Content-Range' => '10-109/2577'}, status: 206, body: 'This is the response')
expect_any_instance_of(Faraday::Connection).to receive(:get).with('https://images.invalid/img.jpg', nil, range: 'bytes=10-109').and_return(fake_resp)
faraday_conn = instance_double(Faraday::Connection, get: fake_resp)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could extract the double in a let and the allow in a before, to DRY things up a bit 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried but I think I can't do it.

Faraday is loaded only after calling FormatParser::RemoteIO.new (here):

def initialize(uri)
  require 'faraday'

I think it was done this way to avoid loading the gem when it's not necessary, for example, if you parse only local files.

So, when I tried to reference the Faraday class in the before block, I got the error uninitialized constant Faraday.

I think that it's worth to require the gem before just to change the test.

Is it ok for you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right...

Looking at it better, the subject could also be DRYed out, and that would also require the gem. It is more work than this PR covers, so I'm also fine with moving on with how it is at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Follow redirects when requesting a range in RemoteIO
4 participants