Skip to content
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
1 change: 1 addition & 0 deletions .byebug_history
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ gem 'rake'

# Adding rspec for running unit testing
gem 'rspec'
gem 'byebug'

group :development, :test do
# Adding shotgun for local web hosting
Expand Down
93 changes: 47 additions & 46 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
GEM
remote: https://rubygems.org/
specs:
activemodel (4.2.5)
activesupport (= 4.2.5)
builder (~> 3.1)
activerecord (4.2.5)
activemodel (= 4.2.5)
activesupport (= 4.2.5)
arel (~> 6.0)
activesupport (4.2.5)
activemodel (5.0.1)
activesupport (= 5.0.1)
activerecord (5.0.1)
activemodel (= 5.0.1)
activesupport (= 5.0.1)
arel (~> 7.0)
activesupport (5.0.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.3)
backports (3.6.7)
builder (3.2.2)
daemons (1.2.3)
diff-lcs (1.2.5)
eventmachine (1.0.8)
i18n (0.7.0)
json (1.8.3)
minitest (5.8.3)
multi_json (1.11.2)
pg (0.18.4)
puma (2.15.3)
rack (1.6.4)
arel (7.1.4)
backports (3.6.8)
concurrent-ruby (1.0.4)
daemons (1.2.4)
diff-lcs (1.3)
eventmachine (1.2.2)
i18n (0.8.0)
minitest (5.10.1)
multi_json (1.12.1)
pg (0.19.0)
puma (3.7.0)
rack (1.6.5)
rack-protection (1.5.3)
rack
rack-test (0.6.3)
rack (>= 1.0)
rails_12factor (0.0.3)
rails_serve_static_assets
rails_stdout_logging
rails_serve_static_assets (0.0.4)
rails_stdout_logging (0.0.4)
rake (10.4.2)
rspec (3.4.0)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-core (3.4.1)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
rails_serve_static_assets (0.0.5)
rails_stdout_logging (0.0.5)
rake (12.0.0)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
shotgun (0.9.1)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
shotgun (0.9.2)
rack (>= 1.0)
sinatra (1.4.6)
rack (~> 1.4)
sinatra (1.4.8)
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (>= 1.3, < 3)
sinatra-activerecord (2.0.9)
sinatra-activerecord (2.0.11)
activerecord (>= 3.2)
sinatra (~> 1.0)
sinatra-contrib (1.4.6)
sinatra-contrib (1.4.7)
backports (>= 2.0)
multi_json
rack-protection
rack-test
sinatra (~> 1.4.0)
tilt (>= 1.3, < 3)
thin (1.6.4)
thin (1.7.0)
daemons (~> 1.0, >= 1.0.9)
eventmachine (~> 1.0, >= 1.0.4)
rack (~> 1.0)
rack (>= 1, < 3)
thread_safe (0.3.5)
tilt (2.0.1)
tilt (2.0.6)
tzinfo (1.2.2)
thread_safe (~> 0.1)
valid_url(0.0.4)
addressable
rails

PLATFORMS
ruby
Expand All @@ -91,6 +91,7 @@ DEPENDENCIES
sinatra-activerecord
sinatra-contrib
thin
valid_url

BUNDLED WITH
1.10.6
1.13.7
32 changes: 31 additions & 1 deletion app/controllers/static.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
get '/' do
@urls = Url.all.last(5)
@total_click = Url.total_click
erb :"static/index"
end
end
# let user create new short URL, display a list of shortened URLs


post '/urls' do
@urls=Url.all.last(5)
@url = Url.create(long_url: params[:long_url])
@error_message = @url.errors.messages[:long_url]
if @url.save
# redirect to '/'
# @url.to_json
else
# erb :"static/index"
@error = "Please enter a valid URL"
redirect to "/"
end
@url.to_json
end
# create a new Url


get '/:short_url' do
@url = Url.find_by(short_url: params[:short_url])
@url.click_count +=1
@url.save
redirect @url.long_url
end

# redirect to appropriate "long" URL
1 change: 1 addition & 0 deletions app/models/.byebug_history
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exit
38 changes: 38 additions & 0 deletions app/models/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'SecureRandom'

class Url < ActiveRecord::Base
# This is Sinatra! Remember to create a migration!

before_create :shorten

validates_presence_of :long_url, message: "can't be blank!"
validate :validate_url


# validates :long_url, :format => { :with => URI::regexp(%w(http https)), :message => "Valid URL required"}

def shorten
self.short_url = SecureRandom.hex(3)
end

def count
self.click_count += 1
self.save
end

def self.total_click
total_click = 0
click_array = Url.select(:click_count)
click_array.each do |click|
total_click += click.click_count
end
total_click
end

def validate_url
unless self.long_url =~ /(http:\/\/|https:\/\/)(www.)([a-z]*.)(com|edu|org|net|gov|mil|biz|info|co.uk)/
errors.add(:long_url, "Please enter a valid URL")
end
end

end
11 changes: 10 additions & 1 deletion app/views/layouts/application.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<html>
<head>
<title>Sinatra Framework</title>
<title>zub.me | the bitly clone</title>
<link rel="stylesheet" type="text/css" href="css/application.css"></link>
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"></link> -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="js/application.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>
<!-- <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> -->
<%= yield %>
</body>
</html>
50 changes: 48 additions & 2 deletions app/views/static/index.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,48 @@
<h1>Hello World</h1>
<p>See Me in views/static/index.html.erb</p>
<div class="container">
<nav></nav>
<!-- <img src="/img/zubme_logo.png" alt="logo" /> -->
<header><h1>zub*me.</h1></header>
<p class="lead">
"Measure you links with ZubMe, the world's leading link management platform."
</p>
</div>

<center>
<div class="form">
<form id="input" action="/urls" method="post" class="style-2">
<input id="paste" type="text" name="long_url" placeholder="Enter your URL" required>
<input id="btn" type="submit" name="Submit">
</form>
</center>
</div>

<br><br>

<div id="error">
<%=@error%>
</div>

<div class ="container">
<table class="table-url-links">
<tr>
<th class="th2">ID</th>
<th class="th2">Long URL</th>
<th class="th2">Short URL</th>
<th class="th2">Created At</th>
<th class="th2">Click No.</th>
</tr>
<% @urls.each do |url| %>
<tr>
<td> <%= url.id %> </td>
<td> <div class="overme"><%= url.long_url %></div> </td>
<td> <div class="overme"> <a href="/<%= url.short_url %>"><%="http://localhost:9393/"%><%= url.short_url %> </a> </div> </td>
<td> <%= url.created_at.strftime("%d %b. %Y at %H:%M") %> </td>
<td> <%= url.click_count %> </td>
</tr>
<% end %>
</table>
</div>




3 changes: 3 additions & 0 deletions config/environments/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# embedded ruby
require 'erb'
require 'uri'

# byebug
require 'byebug'
######################################################################


Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20170206142646_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 :url do |t|
t.string :original
t.string :shortened
t.timestamps
end
end
end
7 changes: 7 additions & 0 deletions db/migrate/20170207094708_rename_table_urls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class RenameTableUrls < ActiveRecord::Migration
def change
rename_table :url, :Urls
rename_column :Urls, :original, :long_url
rename_column :Urls, :shortened, :short_url
end
end
5 changes: 5 additions & 0 deletions db/migrate/20170207103806_rename_urls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameUrls < ActiveRecord::Migration
def change
rename_table :Urls, :urls
end
end
5 changes: 5 additions & 0 deletions db/migrate/20170207113001_add_column.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddColumn < ActiveRecord::Migration
def change
add_column :urls, :click_count, :integer, default: 0
end
end
Loading