Skip to content
This repository has been archived by the owner on Dec 3, 2019. It is now read-only.

updates code school url when protocol is missing #468

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

NeimadTL
Copy link

tries with https first if possible then http.

tries with https first if possible then http
def check_scheme
uri = URI.parse(url)
update_url(uri) if uri.scheme.nil?
end

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

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:

  1. a valid URL to a website that does not support HTTPS would have an "s" characters removed
  2. 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.

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.

@NeimadTL
Copy link
Author

I'm just making this first comment, just so you know that I've taken into account your suggestion.
When I'll get a chance (soon) to get into that more deeply, I'll get back to you.
Thank you.

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

Successfully merging this pull request may close these issues.

2 participants