-
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
First releases complete, no auth or users yet #34
Open
davidhatten
wants to merge
3
commits into
paircolumbus:master
Choose a base branch
from
davidhatten:master
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,50 @@ | ||
class UrlsController < ApplicationController | ||
def index | ||
@urls = Url.all | ||
end | ||
|
||
def new | ||
@url = Url.new | ||
end | ||
|
||
def create | ||
@url = Url.new(url_params) | ||
|
||
if @url.save | ||
redirect_to @url | ||
else | ||
render 'new' | ||
end | ||
end | ||
|
||
def show | ||
@url = Url.find(params[:id]) | ||
end | ||
|
||
def edit | ||
@url = Url.find(params[:id]) | ||
end | ||
|
||
def destroy | ||
@url = Url.find(params[:id]) | ||
@url.destroy | ||
|
||
redirect_to urls_path | ||
end | ||
|
||
def jump | ||
url = Url.find_by_short_url(params[:key]) | ||
|
||
if url.nil? | ||
redirect_to root_path | ||
else | ||
url.increment!(:click_count) | ||
redirect_to url.long_url | ||
end | ||
end | ||
|
||
private | ||
def url_params | ||
params.require(:url).permit(:long_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,36 @@ | ||
require 'uri' | ||
require 'net/http' | ||
|
||
class Url < ActiveRecord::Base | ||
validate :long_url_must_be_valid_uri, :long_url_must_be_reachable | ||
validates :long_url, presence: true | ||
before_save :shorten_url | ||
|
||
private | ||
def shorten_url | ||
range = [*'0'..'9',*'A'..'Z',*'a'..'z'] | ||
self.short_url = Array.new(6){ range.sample }.join if self.short_url.nil? | ||
end | ||
|
||
def long_url_must_be_valid_uri | ||
return if long_url.empty? | ||
|
||
unless long_url =~ /\A#{URI.regexp(['http', 'https'])}\z/ | ||
errors.add(:long_url, "must be a valid url prefaced with http(s)://") | ||
end | ||
end | ||
|
||
def long_url_must_be_reachable | ||
return if long_url.empty? | ||
|
||
url = URI.parse(long_url) | ||
http = Net::HTTP.new(url.host, url.port) | ||
http.read_timeout = 1 | ||
http.start { |http| | ||
http.get(url) | ||
} | ||
|
||
rescue | ||
errors.add(:long_url, "must be a reachable 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,4 @@ | ||
<h1>URL Shortener Home</h1> | ||
|
||
<%= link_to 'New Url', new_url_path %><br/> | ||
<%= link_to 'All Urls', urls_path %> |
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,3 @@ | ||
<h1>Editing a Url</h1> | ||
|
||
<%= link_to 'Back', urls_path %> |
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 Urls:</h1> | ||
|
||
<table> | ||
<tr> | ||
<th>Short Url</th> | ||
<th>Times Visited</th> | ||
<th>Original Url</th> | ||
</tr> | ||
|
||
<% @urls.each do |url| %> | ||
<tr> | ||
<td><%= url.short_url %></td> | ||
<td><%= url.click_count %></td> | ||
<td><%= url.long_url %></td> | ||
<td><%= link_to 'Show', url_path(url) %></td> | ||
<td><%= link_to 'Edit', edit_url_path(url) %></td> | ||
<td><%= link_to 'Delete', url_path(url), method: :delete, data: {confirm: 'Are you sure?'} %></td> | ||
</tr> | ||
<% end %> | ||
</table> | ||
|
||
<%= link_to 'New Url', new_url_path %><br/> | ||
<%= link_to 'Home', root_path %> |
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,27 @@ | ||
<h1>Make a new url here</h1> | ||
|
||
<%= form_for :url, url: urls_path, local: true do |form| %> | ||
<% if @url.errors.any? %> | ||
<div id="error_explanation"> | ||
<h2> | ||
<%= pluralize(@url.errors.count, "error") %> prohibited this url from being saved: | ||
</h2> | ||
<ul> | ||
<%= @url.errors.full_messages.each do |msg| %> | ||
<li><%= msg %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<p> | ||
<%= form.label :long_url %> | ||
<%= form.text_field :long_url %> | ||
</p> | ||
|
||
<p> | ||
<%= form.submit %> | ||
</p> | ||
<% end %> | ||
|
||
<%= link_to 'Home', root_path %> |
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,18 @@ | ||
<h1>Showing a URL here</h1> | ||
<p> | ||
<strong>Original Url:</strong> | ||
<%= @url.long_url %> | ||
</p> | ||
|
||
<p> | ||
<strong>Visit Count:</strong> | ||
<%= @url.click_count %> | ||
</p> | ||
|
||
<p> | ||
<strong>Short Url:</strong> | ||
<%= @url.short_url %> | ||
</p> | ||
|
||
<%= link_to 'Back', urls_path %><br/> | ||
<%= link_to 'Home', root_path %> |
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,8 @@ | ||
class CreateUrls < ActiveRecord::Migration | ||
def change | ||
create_table :urls do |t| | ||
|
||
t.timestamps | ||
end | ||
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,5 @@ | ||
class AddLongUrlToUrl < ActiveRecord::Migration | ||
def change | ||
add_column :urls, :long_url, :text | ||
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,5 @@ | ||
class AddShortUrlToUrl < ActiveRecord::Migration | ||
def change | ||
add_column :urls, :short_url, :text | ||
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,5 @@ | ||
class AddCountToUrl < ActiveRecord::Migration | ||
def change | ||
add_column :urls, :click_count, :integer, default: 0 | ||
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,24 @@ | ||
# encoding: UTF-8 | ||
# This file is auto-generated from the current state of the database. Instead | ||
# of editing this file, please use the migrations feature of Active Record to | ||
# incrementally modify your database, and then regenerate this schema definition. | ||
# | ||
# Note that this schema.rb definition is the authoritative source for your | ||
# database schema. If you need to create the application database on another | ||
# system, you should be using db:schema:load, not running all the migrations | ||
# from scratch. The latter is a flawed and unsustainable approach (the more migrations | ||
# you'll amass, the slower it'll run and the greater likelihood for issues). | ||
# | ||
# It's strongly recommended that you check this file into your version control system. | ||
|
||
ActiveRecord::Schema.define(version: 20180124160940) do | ||
|
||
create_table "urls", force: true do |t| | ||
t.datetime "created_at" | ||
t.datetime "updated_at" | ||
t.text "long_url" | ||
t.text "short_url" | ||
t.integer "click_count", default: 0 | ||
end | ||
|
||
end |
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.
Perfect!