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

Complete Release 3 #38

Open
wants to merge 9 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
2 changes: 2 additions & 0 deletions source/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
4 changes: 3 additions & 1 deletion source/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ gem 'spring', group: :development
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

gem 'httparty'

# Use unicorn as the app server
# gem 'unicorn'

Expand All @@ -38,4 +40,4 @@ gem 'spring', group: :development
# Use debugger
# gem 'debugger', group: [:development, :test]
gem 'rspec-rails', group: [:development, :test]

gem 'pry'
113 changes: 64 additions & 49 deletions source/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,43 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
builder (3.2.2)
builder (3.2.3)
coderay (1.1.2)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
coffee-script (2.3.0)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.8.0)
diff-lcs (1.2.5)
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
diff-lcs (1.3)
erubis (2.7.0)
execjs (2.2.1)
execjs (2.7.0)
hike (1.2.3)
i18n (0.6.11)
jbuilder (2.2.2)
activesupport (>= 3.0.0, < 5)
multi_json (~> 1.2)
jquery-rails (3.1.2)
httparty (0.15.6)
multi_xml (>= 0.5.2)
i18n (0.9.5)
concurrent-ruby (~> 1.0)
jbuilder (2.6.4)
activesupport (>= 3.0.0)
multi_json (>= 1.2)
jquery-rails (3.1.5)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.8.1)
mail (2.6.1)
mime-types (>= 1.16, < 3)
mime-types (2.4.1)
minitest (5.4.2)
multi_json (1.10.1)
rack (1.5.2)
rack-test (0.6.2)
json (1.8.6)
mail (2.7.0)
mini_mime (>= 0.1.1)
method_source (0.9.0)
mini_mime (1.0.0)
minitest (5.11.3)
multi_json (1.13.1)
multi_xml (0.6.0)
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rack (1.5.5)
rack-test (0.6.3)
rack (>= 1.0)
rails (4.1.6)
actionmailer (= 4.1.6)
Expand All @@ -71,63 +81,65 @@ GEM
activesupport (= 4.1.6)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.3.2)
rdoc (4.1.2)
json (~> 1.4)
rspec-core (3.1.6)
rspec-support (~> 3.1.0)
rspec-expectations (3.1.2)
rake (12.3.1)
rdoc (4.3.0)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.1.0)
rspec-mocks (3.1.3)
rspec-support (~> 3.1.0)
rspec-rails (3.1.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-rails (3.7.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.1.0)
rspec-expectations (~> 3.1.0)
rspec-mocks (~> 3.1.0)
rspec-support (~> 3.1.0)
rspec-support (3.1.2)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
sass (3.2.19)
sass-rails (4.0.3)
sass-rails (4.0.5)
railties (>= 4.0.0, < 5.0)
sass (~> 3.2.0)
sprockets (~> 2.8, <= 2.11.0)
sass (~> 3.2.2)
sprockets (~> 2.8, < 3.0)
sprockets-rails (~> 2.0)
sdoc (0.4.1)
sdoc (0.4.2)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
spring (1.1.3)
sprockets (2.11.0)
spring (1.7.2)
sprockets (2.12.4)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sprockets-rails (2.2.0)
sprockets-rails (2.3.3)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
sqlite3 (1.3.9)
thor (0.19.1)
thread_safe (0.3.4)
sqlite3 (1.3.13)
thor (0.20.0)
thread_safe (0.3.6)
tilt (1.4.1)
turbolinks (2.4.0)
coffee-rails
tzinfo (1.2.2)
turbolinks (5.1.1)
turbolinks-source (~> 5.1)
turbolinks-source (5.1.0)
tzinfo (1.2.5)
thread_safe (~> 0.1)
uglifier (2.5.3)
execjs (>= 0.3.0)
json (>= 1.8.0)
uglifier (4.1.10)
execjs (>= 0.3.0, < 3)

PLATFORMS
ruby

DEPENDENCIES
coffee-rails (~> 4.0.0)
httparty
jbuilder (~> 2.0)
jquery-rails
pry
rails (= 4.1.6)
rspec-rails
sass-rails (~> 4.0.3)
Expand All @@ -136,3 +148,6 @@ DEPENDENCIES
sqlite3
turbolinks
uglifier (>= 1.3.0)

BUNDLED WITH
1.16.1
28 changes: 28 additions & 0 deletions source/app/controllers/urls_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
require 'net/http'

class UrlsController < ApplicationController
def index
@urls = Url.all
end

def create
url = Url.new(url_params)

if !url.save
flash[:error] = 'Invalid URL'
end

redirect_to urls_path
end

def show
url = Url.find(params[:id])
url.click_count += 1
url.save
redirect_to url.long_url
end

private

def url_params
params.require(:url).permit(:long_url)
end
end
24 changes: 24 additions & 0 deletions source/app/models/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Url < ActiveRecord::Base
validates :long_url, presence: true
validate :long_url_is_uri_valid
validate :long_url_is_reachable

private
def long_url_is_uri_valid
unless long_url =~ /\A#{URI::regexp(['http', 'https'])}\z/
errors.add(:long_url, 'must be valid uri')
end
end

def long_url_is_reachable
begin
response = HTTParty.get(long_url, timeout: 1)

if response.code.to_i != 200
errors.add(:long_url, 'must return a 200 code')
end
rescue
errors.add(:long_url, 'must be accessible')
end
end
end
14 changes: 14 additions & 0 deletions source/app/views/urls/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p>Shorten URL:</p>

<%= flash[:error] if !nil? %>

<%= form_for :url do |f| %>
<%= f.label :long_url %>
<%= f.text_field :long_url %>
<% end %>

<ul>
<% @urls.each do |url| %>
<li><%= url_url(url) ====> #{url.long_url} (clicked #{url.click_count} times)" %></li>
<% end %>
</ul>
2 changes: 2 additions & 0 deletions source/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end

resources :urls, only: [:index, :create, :show]
end
9 changes: 9 additions & 0 deletions source/db/migrate/20180514142357_create_urls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateUrls < ActiveRecord::Migration
def change
create_table :urls do |t|
t.string :long_url

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions source/db/migrate/20180514175827_add_click_counter_to_urls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddClickCounterToUrls < ActiveRecord::Migration
def change
add_column :urls, :click_count, :integer
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDefaultValueToClickCountForUrls < ActiveRecord::Migration
def change
change_column_default :urls, :click_count, 0
end
end
32 changes: 32 additions & 0 deletions source/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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: 20180516183211) do

create_table "urls", force: true do |t|
t.string "long_url"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "click_count", default: 0
t.string "short_url"
end

create_table "users", force: true do |t|
t.string "username"
t.string "password"
t.boolean "admin"
t.datetime "created_at"
t.datetime "updated_at"
end

end
Loading