-
Notifications
You must be signed in to change notification settings - Fork 108
updates code school url when protocol is missing #468
base: master
Are you sure you want to change the base?
Conversation
tries with https first if possible then http
def check_scheme | ||
uri = URI.parse(url) | ||
update_url(uri) if uri.scheme.nil? | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently check_scheme
reads the URL attribute, then update_url
writes it.
To make the URL handling code reusable the API might look something like this:
self.url = ensure_url_protocol(url)
For now though, I'd probably keep it all in one method.
rescue StandardError | ||
candidate.sub!('s', '') | ||
end | ||
self.url = candidate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two bugs that I see:
- a valid URL to a website that does not support HTTPS would have an "s" characters removed
- a invalid URL that results in a request failure would be saved anyway (again with an "s" character removed)
The first issue can be resolved by using the Addressable gem:
def check_scheme
if url !~ /\Ahttps?:\/\//
uri = Addressable::URI.heuristic_parse(url, scheme: 'https')
begin
HTTParty.get(uri.to_s, timeout: 2)
rescue # (no need for StandardError. that's the default)
uri.scheme = 'http'
end
self.url = uri.to_s
end
end
It'd be great to use the standard library instead of a gem, but Addressable works around this unfortunate behavior of URI:
uri = URI('foo.com')
uri.scheme = 'https'
uri.to_s # https:foo.com
For the second issue, we can update the code to retry
the request after updating the protocol, to ensure it's really valid. Then if it still doesn't work we can add a validation error to prevent the record from saving:
def check_scheme
uri = Addressable::URI.heuristic_parse(url, scheme: 'https')
begin
HTTParty.get(uri.to_s, timeout: 2)
rescue
if uri.scheme == 'https'
uri.scheme = 'http'
retry
else
errors.add :url, 'unable to verify URL'
return
end
end
self.url = uri.to_s
end
But I'm new here, so I'm not sure if y'all want to show a validation error here or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that HTTParty only raises exceptions if it isn't able to connect to the server. If it receives a 404, though, you'd have to check the response object. But that might not be something y'all are trying to protect against.
I'm just making this first comment, just so you know that I've taken into account your suggestion. |
tries with https first if possible then http.