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

Lo ryder1 #12

Open
wants to merge 2 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
28 changes: 28 additions & 0 deletions source/app/assets/stylesheets/urls.css.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
// Place all the styles related to the Urls controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

$red: #8C2318;
$headline: "Times New Roman", Times, serif;
$primary-font: Verdana, Geneva, sans-serif;
$my_grey: #F5F5F5;


.container {
margin: 5% auto;
width: 70%;
padding: 2%;
border: 3px solid black;
background-color: $my_grey;
font-family: $primary-font;
}

.wrap-center {
margin:5px;
display:block;
text-align: center;
}

.error {
background-color: pink;
padding:10px;
color:$red;
display:inline-block;
}
41 changes: 41 additions & 0 deletions source/app/controllers/urls_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,43 @@
class UrlsController < ApplicationController
def index
@urls = Url.all
end

def show
@url = Url.find(params[:id])
end

def new
@url = Url.new
end

def create

begin
response = Net::HTTP.get_response(uri)
rescue
flash[:error] = "not a valid url"
end

@url = Url.new(url_params)
@url.shorten_url
@url.counter = 0
if @url.save
redirect_to url_path(@url)
else
render 'new'
end
end

def link
@url = Url.where(short: params[:short])
@url[0].counter +=1
@url[0].save
redirect_to 'http://' + @url[0].orig
end

private
def url_params
params.require(:url).permit(:orig)
end
end
14 changes: 14 additions & 0 deletions source/app/models/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'securerandom'
require "net/http"
require "uri"


class Url < ActiveRecord::Base
validates :orig, presence: true, format: { with: /.+\.\w+/, message: "Not a valid web address." }
# before_save do
# self.short = SecureRandom.hex(3)
# end
def shorten_url
self.short = SecureRandom.hex(3)
end
end
4 changes: 3 additions & 1 deletion source/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
</head>
<body>

<%= yield %>
<div class="container">
<%= yield %>
</div>

</body>
</html>
10 changes: 10 additions & 0 deletions source/app/views/urls/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Welcome to LoRyder1's URL shortner</h1>

<%= link_to 'New URL', new_url_path %>

<ul>
<% @urls.each do |u| %>
<li><%= link_to "localhost:3000/url/#{u.short}", link_path(u.short), :method => :post %></li>
<li>Click Count:<%= u.counter %></li>
<% end %>
</ul>
28 changes: 28 additions & 0 deletions source/app/views/urls/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1>URL shortner</h1>

<% if @url.errors.any? %>
<%= pluralize(@url.errors.count, "error") %>
prohibited this url from being shortenend:
<% @url.errors.full_messages.each do |msg| %>
<ul>
<li><%= msg %></li>
</ul>
<% end %>

<% if flash[:error] %>
<div class='wrap-center'><p class='error'><%= flash[:error] %></p></div>
<% end %>

<% end %>


<%= form_for @url do |f| %>

<p>
<br><%= f.text_field :orig, placeholder: 'Enter your url' %>
</p>

<p>
<%= f.submit %>
</p>
<% end %>
8 changes: 8 additions & 0 deletions source/app/views/urls/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>Your URL shortened:</h1>


<p>Original: <%= @url.orig %></p>

<p>Short:
<%= link_to "localhost:3000/url/#{@url.short}", link_path(@url.short), :method => :post %>
</p>
6 changes: 6 additions & 0 deletions source/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
# Example of regular route:
# get 'products/:id' => 'catalog#view'

root 'urls#index'

resources :urls

post "url/:short" => "urls#link", as: "link"

# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

Expand Down
11 changes: 11 additions & 0 deletions source/db/migrate/20150531210037_create_urls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateUrls < ActiveRecord::Migration
def change
create_table :urls do |t|
t.string :orig
t.string :short
t.integer :counter

t.timestamps
end
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: 20150531210037) do

create_table "urls", force: true do |t|
t.string "orig"
t.string "short"
t.integer "counter"
t.datetime "created_at"
t.datetime "updated_at"
end

end
5 changes: 5 additions & 0 deletions source/spec/models/url_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Url, :type => :model do
pending "add some examples to (or delete) #{__FILE__}"
end