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

Levi's solution #35

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

Levi's solution #35

wants to merge 1 commit into from

Conversation

levikl
Copy link

@levikl levikl commented Jan 25, 2018

@jaybobo
Implemented all features sans extra credit; will add user authentication if time allows.


# GET /:shortened_url
def redirect
@url = Url.where(shortened_url: params[:shortened_url]).take
Copy link
Member

Choose a reason for hiding this comment

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

take with no argument seems very out of place to me. I think most Rails devs would expect it's alias, first.

find_by simplifies this a bit more though.

# GET /:shortened_url
def redirect
@url = Url.where(shortened_url: params[:shortened_url]).take
if @url != nil
Copy link
Member

Choose a reason for hiding this comment

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

Ruby provides nil? on every object for just this sort of check.

But, since you're checking for "not nil" the Rails method present? would do the trick.

Even better (in my opinion) is to drop the nil or presence check and depend of the fact that nil is falsey. Just: if @url.

@url.update_attribute(:click_count, @url.click_count + 1)
redirect_to @url.original_url
else
redirect_to root_path, notice: 'We couldn\'t find a link for the bitly URL you clicked.'
Copy link
Member

Choose a reason for hiding this comment

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

Switching to double quotes is preferable over escaping the embedded single quote.

require 'net/http'

class Url < ActiveRecord::Base
ALPHANUMERIC_CHARACTERS = (('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a)
Copy link
Member

Choose a reason for hiding this comment

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

👍 extracting a constant for this

end

def original_url_must_return_a_response
return unless original_url.present? && original_url =~ URI::regexp(['http', 'https'])
Copy link
Member

Choose a reason for hiding this comment

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

Matching the url against URI's regexp helper gizmo is somewhat duplicative with your check in original_url_starts_with_http_or_https. I wonder how we can remove the duplicated concept. 🤔

errors.add(:original_url, "must respond to a HTTP request")
rescue Errno::ECONNREFUSED
errors.add(:original_url, "must respond to a HTTP request")
rescue SocketError
Copy link
Member

Choose a reason for hiding this comment

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

I suspect Net::HTTP is capable of raise many other errors. I'd switch to rescuing all subclasses of StandardError with bare rescue and make sure only Net::HTTP code is invoked in this method. (just the URI.parse and the get_response parts)

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

Successfully merging this pull request may close these issues.

2 participants