-
Notifications
You must be signed in to change notification settings - Fork 40
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
base: master
Are you sure you want to change the base?
Levi's solution #35
Conversation
|
||
# GET /:shortened_url | ||
def redirect | ||
@url = Url.where(shortened_url: params[:shortened_url]).take |
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.
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 |
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.
@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.' |
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.
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) |
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.
👍 extracting a constant for this
end | ||
|
||
def original_url_must_return_a_response | ||
return unless original_url.present? && original_url =~ URI::regexp(['http', 'https']) |
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.
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 |
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 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)
@jaybobo
Implemented all features sans extra credit; will add user authentication if time allows.