-
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
complete solution(Shabbir Saifee) #37
Open
shabbirsaifee92
wants to merge
1
commit into
paircolumbus:master
Choose a base branch
from
shabbirsaifee92:ShabbirSolution
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,57 @@ | ||
class UrlsController < ApplicationController | ||
|
||
#GET /urls | ||
def index | ||
@urls = Url.all | ||
end | ||
|
||
#GET /urls/new | ||
def new | ||
@url = Url.new | ||
end | ||
#GET /urls/:id | ||
def show | ||
@url = Url.find(params[:id]) | ||
end | ||
|
||
# POST /urls | ||
def create | ||
puts "inside create" | ||
@url = Url.new(url_params) | ||
if @url.save | ||
redirect_to url_path(@url) | ||
else | ||
render 'new' | ||
end | ||
end | ||
|
||
#DELETE /urls/:id | ||
def destroy | ||
@url =Url.find(params[:id]) | ||
if @url.destroy | ||
redirect_to urls_path | ||
else | ||
|
||
end | ||
end | ||
|
||
#GET /:short_url | ||
def hit_short_url | ||
puts "inside hit_url" | ||
url = Url.find_by short_url: params[:short_url] | ||
if url | ||
url.update('click_count' => url.click_count + 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Concurrent clicks will be undercounted. Not that it really matters for this exercise, but I think it's a fun exercise to think about doing atomic updates to the database records. |
||
redirect_to url.original_url | ||
else | ||
redirect_to root_path | ||
end | ||
|
||
end | ||
|
||
private | ||
def url_params | ||
params.require(:url).permit(:original_url) | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'uri' | ||
class Url < ActiveRecord:: Base | ||
|
||
CHARSET = (('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a) | ||
validates :original_url, presence: true | ||
validate :url_valid? | ||
#before_save :shorten_url, :on => :create | ||
before_create :shorten_url | ||
|
||
private def url_valid? | ||
begin | ||
error.add(:original_url, " is invalid!") unless | ||
URI.parse(original_url).kind_of?(URI::HTTP) || URI.parse(original_url).kind_of?(URI::HTTPS) | ||
rescue | ||
errors.add(:original_url, " is invalid! (must start with 'http' or 'https')") | ||
end | ||
end | ||
|
||
private def shorten_url | ||
puts "inside shotern method" | ||
self.short_url = create_short_url | ||
end | ||
|
||
private def create_short_url | ||
tiny_url ="" | ||
6.times do | ||
tiny_url << CHARSET.sample | ||
end | ||
tiny_url | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<h1> | ||
Listing all the urls | ||
</h1> | ||
|
||
<p> <%= link_to "Shorten Url", new_url_path %> | ||
</p> | ||
|
||
<table > | ||
<tr> | ||
<td> <strong> Original Url </strong> </td> | ||
<td> <strong> Shortened Url </strong> </td> | ||
<td> <strong>Total Clicks</strong> </td> | ||
</tr> | ||
<% @urls.each do |url| %> | ||
<tr> | ||
<td> <a href="<%=url.original_url %>" ><%=url.original_url %></a></td> | ||
<td> <a href="http://localhost:3000/<%=url.short_url%>"><%=url.short_url%> </a></td> | ||
<td><%= url.click_count %></td> | ||
<td><%= link_to 'Delete', url_path(url), method: :delete,data: {confirm: "Are you sure?"} %></td> | ||
</tr> | ||
|
||
<% end %> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<h1>Shorten a new url on this page</h1> | ||
|
||
<!-- --> | ||
|
||
<%if @url.errors.any?%> | ||
<h2> Correct the following errors</h2> | ||
<ul> | ||
<% @url.errors.full_messages.each do |msg| %> | ||
<li class="text-danger"> | ||
<%= msg %> | ||
</li> | ||
<%end %> | ||
</ul> | ||
|
||
<% end %> | ||
|
||
<%= form_for @url do |f| %> | ||
<p> | ||
<%= f.label "Enter Url" %></br> | ||
<%= f.text_field :original_url, class: "form-control" %> | ||
<%=f.submit class: "btn btn-success", value: "Shorten" %> | ||
</p> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<h1>showing selected url</h1> | ||
|
||
<p> | ||
Original URL: <%= @url.original_url%> | ||
</p> | ||
<p> | ||
Shortened Url: <%= @url.short_url%> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,6 @@ | ||
Rails.application.routes.draw do | ||
# The priority is based upon order of creation: first created -> highest priority. | ||
# See how all your routes lay out with "rake routes". | ||
|
||
# You can have the root of your site routed with "root" | ||
# root 'welcome#index' | ||
|
||
# Example of regular route: | ||
# get 'products/:id' => 'catalog#view' | ||
|
||
# Example of named route that can be invoked with purchase_url(id: product.id) | ||
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase | ||
|
||
# Example resource route (maps HTTP verbs to controller actions automatically): | ||
# resources :products | ||
|
||
# Example resource route with options: | ||
# resources :products do | ||
# member do | ||
# get 'short' | ||
# post 'toggle' | ||
# end | ||
# | ||
# collection do | ||
# get 'sold' | ||
# end | ||
# end | ||
|
||
# Example resource route with sub-resources: | ||
# resources :products do | ||
# resources :comments, :sales | ||
# resource :seller | ||
# end | ||
|
||
# Example resource route with more complex sub-resources: | ||
# resources :products do | ||
# resources :comments | ||
# resources :sales do | ||
# get 'recent', on: :collection | ||
# end | ||
# end | ||
|
||
# Example resource route with concerns: | ||
# concern :toggleable do | ||
# post 'toggle' | ||
# end | ||
# resources :posts, concerns: :toggleable | ||
# resources :photos, concerns: :toggleable | ||
|
||
# Example resource route within a namespace: | ||
# namespace :admin do | ||
# # Directs /admin/products/* to Admin::ProductsController | ||
# # (app/controllers/admin/products_controller.rb) | ||
# resources :products | ||
# end | ||
resources :urls | ||
root 'urls#index' | ||
get '/:short_url', to: 'urls#hit_short_url' | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
destroy
doesn't return true or false to tell you whether it succeeded, like some other ActiveRecord methods. So, just redirect unconditionally.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.
Oh Thanks. Is it better if implement the error handling here? because, if destroy fails due to some DB issue it will raise an exception right?
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.
Great question. The
destroy
call can raise an error for many various reasons. I would not handle those errors in app code as long as we don't expect them from regular app usage. Make sure the app is connected with some error aggregation service, then let "exceptional" exceptions go uncaught.If we start seeing errors we didn't account for, that's when we should do what we can to prevent them and also rescue them to return a more user-friendly response to our user.