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

First releases complete, no auth or users yet #34

Open
wants to merge 3 commits 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
3 changes: 3 additions & 0 deletions source/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

def index
end
end
48 changes: 48 additions & 0 deletions source/app/controllers/urls_controller.rb
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
36 changes: 36 additions & 0 deletions source/app/models/url.rb
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/
Copy link
Member

Choose a reason for hiding this comment

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

Perfect!

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
4 changes: 4 additions & 0 deletions source/app/views/application/index.html.erb
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 %>
3 changes: 3 additions & 0 deletions source/app/views/urls/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Editing a Url</h1>

<%= link_to 'Back', urls_path %>
23 changes: 23 additions & 0 deletions source/app/views/urls/index.html.erb
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 %>
27 changes: 27 additions & 0 deletions source/app/views/urls/new.html.erb
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 %>
18 changes: 18 additions & 0 deletions source/app/views/urls/show.html.erb
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 %>
6 changes: 6 additions & 0 deletions source/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,10 @@
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
#

get 'application/index'
resources :urls
get '/:key' => 'urls#jump'
root 'application#index'
end
8 changes: 8 additions & 0 deletions source/db/migrate/20180123203210_create_urls.rb
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
5 changes: 5 additions & 0 deletions source/db/migrate/20180123215003_add_long_url_to_url.rb
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
5 changes: 5 additions & 0 deletions source/db/migrate/20180124143738_add_short_url_to_url.rb
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
5 changes: 5 additions & 0 deletions source/db/migrate/20180124160940_add_count_to_url.rb
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
24 changes: 24 additions & 0 deletions source/db/schema.rb
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