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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions app/models/code_school.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,23 @@ class CodeSchool < ApplicationRecord
validates :name, :url, :logo, presence: true
validates_inclusion_of :full_time, :hardware_included, :has_online, :online_only, :in => [true, false]
has_many :locations, -> { order('state ASC, city ASC') }, dependent: :destroy

before_create :check_scheme

private

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.


def update_url(uri)
candidate = 'https://' << uri.to_s
begin
HTTParty.get(candidate, timeout: 2)
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.

end
end
22 changes: 22 additions & 0 deletions test/models/code_schools_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,26 @@ def setup
test 'rep_email should be present' do
assert @code_school.rep_email.present?
end

test 'url should be present' do
assert @code_school.url.present?
end

test 'url does not change when url is correct' do
previous_url = @code_school.url
@code_school.run_callbacks :create
assert @code_school.url.eql?(previous_url)
end

test 'url gets updated with HTTPS scheme when url has no scheme' do
@code_school.url = 'stackoverflow.com'
@code_school.run_callbacks :create
assert @code_school.url.eql?('https://stackoverflow.com')
end

test 'url gets updated with HTTP scheme when url has no scheme' do
@code_school.url = 'neverssl.com'
@code_school.run_callbacks :create
assert @code_school.url.eql?('http://neverssl.com')
end
end