-
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
Completed #25
Open
jonahroth
wants to merge
6
commits into
paircolumbus:master
Choose a base branch
from
jonahroth: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
Completed #25
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
03dd0f9
Release 0
9a9b40e
Release 0 re-implemented to follow challenge instructions
8d0c33c
Added basic front end that works as long as URLs begin with HTTP... b…
aab974e
Release 1: click_count field that increments added to urls table
4d40871
Validates URLs but still can't show errors
cc0a932
errors display properly
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
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,3 +1,12 @@ | ||
// 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/ | ||
body { | ||
text-align: center; | ||
} | ||
|
||
.form-group { | ||
width: 50%; | ||
margin-left: 25%; | ||
margin-right: 25%; | ||
} |
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,33 @@ | ||
class UrlsController < ApplicationController | ||
#require 'socket' | ||
skip_before_action :verify_authenticity_token | ||
|
||
def new | ||
@url = Url.new | ||
end | ||
|
||
def create | ||
@url = Url.create(url_params) | ||
if @url.errors.any? | ||
@url.address = "" | ||
errors = "" | ||
@url.errors.full_messages.each { |msg| errors += msg + " " } | ||
p errors | ||
redirect_to root_path, alert: errors | ||
else | ||
redirect_to root_path, notice: "Your URL: http://#{request.host_with_port}/#{@url.shortcode}" | ||
end | ||
end | ||
|
||
def get | ||
@url = Url.find_by(:shortcode => params[:id]) | ||
@url.click_count += 1 | ||
@url.save | ||
redirect_to @url.address | ||
end | ||
|
||
private | ||
def url_params | ||
params.require(:url).permit(:address) | ||
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,37 @@ | ||
require 'digest/sha1' | ||
require 'uri' | ||
|
||
class Url < ActiveRecord::Base | ||
before_validation :smart_url_protocol | ||
before_create :set_shortcode | ||
validate :is_url, on: :create | ||
|
||
protected | ||
|
||
def set_shortcode | ||
full = Digest::SHA1.hexdigest(self.address) | ||
start = 0 | ||
code = full[start..(start+5)] | ||
loop do | ||
break if !Url.find_by(:shortcode => code) || (start + 5 >= full.length) | ||
start += 1 | ||
code = full[start..(start+5)] | ||
end | ||
self.shortcode = code | ||
end | ||
|
||
def is_url | ||
errors.add(:address, "is not a valid URL") unless !!URI.parse(address) | ||
rescue URI::InvalidURIError | ||
errors.add(:address, "is not a valid URL") | ||
end | ||
|
||
def smart_url_protocol | ||
unless self.address[/\Ahttp:\/\//] || self.address[/\Ahttps:\/\//] | ||
self.address = "http://#{self.address}" | ||
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,20 @@ | ||
%h1 Jonah's Fantastic URL Shortener | ||
|
||
- if flash[:notice] && [email protected]? | ||
.notice | ||
= flash[:notice] | ||
- if flash[:alert] | ||
.alert | ||
= flash[:alert] | ||
|
||
= form_for @url do |u| | ||
- if @url.errors.any? | ||
.alert.alert-error | ||
- @url.errors.full_messages.each do |msg| | ||
%strong | ||
= msg | ||
.form-group | ||
= u.label :address, "Enter the address to shorten:" | ||
= u.text_field :address, class: "form-control" | ||
.form-group | ||
= u.submit "Submit", class: "btn btn-submit" |
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,10 @@ | ||
class CreateUrls < ActiveRecord::Migration | ||
def change | ||
create_table :urls do |t| | ||
t.string :address | ||
t.string :shortcode | ||
|
||
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 AddClickCountToUrl < 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: 20160531200238) do | ||
|
||
create_table "urls", force: true do |t| | ||
t.string "address" | ||
t.string "shortcode" | ||
t.datetime "created_at" | ||
t.datetime "updated_at" | ||
t.integer "click_count", 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,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Url, :type => :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
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.
I don't think you need this bc you aren't allowing foreign open api calls.