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
5 changes: 5 additions & 0 deletions .byebug_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
c
C
@url.to_json
@url

1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ source 'https://rubygems.org'
gem 'sinatra'
gem 'sinatra-contrib'

gem 'byebug'
# Adding thin gem as advised
gem 'thin'

Expand Down
6 changes: 4 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ GEM
arel (6.0.3)
backports (3.6.7)
builder (3.2.2)
byebug (8.2.4)
daemons (1.2.3)
diff-lcs (1.2.5)
eventmachine (1.0.8)
eventmachine (1.2.0.1)
i18n (0.7.0)
json (1.8.3)
minitest (5.8.3)
Expand Down Expand Up @@ -81,6 +82,7 @@ PLATFORMS
DEPENDENCIES
activerecord
activesupport
byebug
pg
puma
rails_12factor
Expand All @@ -93,4 +95,4 @@ DEPENDENCIES
thin

BUNDLED WITH
1.10.6
1.11.2
48 changes: 47 additions & 1 deletion app/controllers/static.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@

require 'byebug'

get '/' do
@uri = Url.all.order(created_at: :desc).limit(10)
erb :"static/index"
end
end

post '/urls' do
text = params[:long_url]
regex = (/\(?(?:(http|https):\/\/)/)

if text.match(regex)
params[:long_url]
else
params[:long_url] = "http://" + params[:long_url]
end

url1 = Url.shorten
@url = Url.new(long: params[:long_url], short: url1, counter: 0)

if @url.save
@url.to_json
else
status 400
end

end

get '/list' do
@uri = Url.all.order(created_at: :desc).limit(10)
erb :"static/index3"
end

get '/delete/:short_url' do
a = Url.find_by(short: params[:short_url])
a.destroy
redirect '/list'
end

get '/:short_url' do
a = Url.all
b = a.find_by(short: params[:short_url])
count = b.counter.to_i
count += 1
b.update(counter: count)
redirect b.long
end

24 changes: 24 additions & 0 deletions app/models/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@


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

validates :long, :format => {:with => /\(?(?:(http|https):\/\/)(?:((?:[^\W\s]|\.|-|[:]{1})+)@{1})?((?:www.)?(?:[^\W\s]|\.|-)+[\.][^\W\s]{2,4}|localhost(?=\/)|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::(\d*))?([\/]?[^\s\?]*[\/]{1})*(?:\/?([^\s\n\?\[\]\{\}\#]*(?:(?=\.)){1}|[^\s\n\?\[\]\{\}\.\#]*)?([\.]{1}[^\s\?\#]*)?)?(?:\?{1}([^\s\n\#\[\]]*))?([\#][^\s\n]*)?\)?/}
validates :short, :uniqueness => true


# before_create :shorten

def self.shorten

letters_array = (0..9).to_a + ('a'..'z').to_a + ('A'..'Z').to_a
random_selection = letters_array.sample(6)
new_url = random_selection.join('')
new_url

end




end
18 changes: 16 additions & 2 deletions app/views/layouts/application.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
<html>
<head>
<title>Sinatra Framework</title>
<title>DogeBit</title>

<link rel="icon" type="image/png" href="http://apk-dl.com/detail/image/com.jjapp.spacedogeadventure-w250.png" />

<link type="text/css" rel="stylesheet" href="css/application.css">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

<script type="text/javascript" src="/js/application.js"></script>

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

</head>
<body>

<body >
<%= yield %>
</body>

</html>
Binary file added app/views/static/doge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 45 additions & 2 deletions app/views/static/index.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
<h1>Hello World</h1>
<p>See Me in views/static/index.html.erb</p>
<header style="margin-top: -100px; margin-bottom: 60;">
<a href="http://localhost:9393/">
<img src="http://apk-dl.com/detail/image/com.jjapp.spacedogeadventure-w250.png" alt="Doge Icon" style="width:80px;height:80px; margin-left: 20">
</a>
</header>

<h1>Welcome to DogeBit</h1>
<h2> Your friendly Doge to make 'UR L'ife simpler </h2>


<form action="/urls" method="post" id='answer'>
<input type ="text" name ="long_url" placeholder="URL" id="changeinputbox">
<input type="submit" value="Shorten!" id="addfont">
</form>

<section>
<% if @url %>
<p>Your long URL: <%=@url.long %> </p>
<p>Your short URL:<a href =<%= @url.short %> target="_blank" > http://localhost:9393/<%= @url.short %></a> </p>
<% elsif @urll == true %>
<p>Invalid Link!</p>
<% end %>
</section>

<div id="info"></div>

<article>
<button>
List!
</button>
<table class='index_table' border="1" style="width:100%" id='full_table'>
<tr>
<th>Long URL</th>
<th>Shortened URL</th>
<th>Number of times Visited</th>
</tr>
<% @uri[0..2].each do |x| %>
<tr>
<td><a href="<%=x.long %>" target="_blank"><%=x.long%></a></td>
<td>http://localhost:9393/<%=x.short%></td>
<td><%=x.counter%></td>
</tr>
<% end %>
</table>
</article>
31 changes: 31 additions & 0 deletions app/views/static/index2.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

<header>

<a href="http://localhost:9393/">
<img src="http://apk-dl.com/detail/image/com.jjapp.spacedogeadventure-w250.png" alt="Doge Icon" style="width:80px;height:80px;">
</a>

<h1>Welcome to DogeBit</h1>

</header>

<h2> Your friendly Doge to make 'UR L'ife simpler </h2>

<nav>

<form action="/urls" method="post">
<input type ="text" name ="long_url" placeholder="Invalid Link" id="changeinputbox">
<input type="submit" value="Shorten!" id="addfont">
</form>

</nav>

<foot>


<form action="http://localhost:9393/list" >
<input type="submit" value="List!" id="changesizeforlist">
</form>


</foot>
36 changes: 36 additions & 0 deletions app/views/static/index3.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<header style="margin-top: -100px; margin-bottom: 60;">
<a href="http://localhost:9393/">
<img src="http://apk-dl.com/detail/image/com.jjapp.spacedogeadventure-w250.png" alt="Doge Icon" style="width:80px;height:80px; margin-left: 20">
</a>
</header>

<h1>DogeBit</h1>


<article>
<table border='1' style="width:100%;" id="magictable" >
<tr>
<th>Long URL</th>
<th>Shortened URL</th>
<th>Number of times Visited</th>
</tr>
<% if @uri %>
<% @uri.limit(10).each do |x| %>
<tr>
<td><a href="<%=x.long %>" target="_blank"><%=x.long%></a></td>
<td><a href="/<%=x.short %>" target="_blank">http://localhost:9393/<%=x.short%></a></td>
<td><%=x.counter%></td>
</tr>
<% end %>
<% end %>
</table>
</article>
<br>
<br>
<br>

<div id="footer">
<img src="http://fleeps.com/templates/__custom/images/fleeps/fleepsfans.png" alt="fitp" style="width:150px;">
</div>


1 change: 1 addition & 0 deletions config/environments/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'rubygems'
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
require 'pathname'
require 'byebug'

# database
require 'pg'
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20160418120448_create_urls.rb
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 :long
t.string :short
t.datetime :created_at
t.datetime :updated_at
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20160418173105_add_counter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCounter < ActiveRecord::Migration
def change
add_column :urls, :counter, :integer
end
end
40 changes: 40 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'csv'
require_relative '../config/environments/init'
require 'benchmark'

def url_pair
#reads stuff inside the urls file
url_list = CSV.read('urls.csv')
@final_array=[]

url_list.each_with_index do |x, index|

edit_string = x[0].chop
edit_string[0] = ''

#creates a shorten link
letters_array = (0..9).to_a + ('a'..'z').to_a + ('A'..'Z').to_a
random_selection = letters_array.sample(6)
new_url = random_selection.join('')
#puts both the long url from the urls file and the created short
#url together into a string
time = DateTime.now
temp = %Q[('#{edit_string}', '#{new_url}', '#{time}', '#{time}')]
#push each string into the array
@final_array << temp

puts "Loading...No #{index + 1} of #{url_list.size}"
end
#join each element in the array so it becomes a string with
#single quotation
@final_array.join(", ")

end
#here runs the sql
def seed_db
sql = 'INSERT INTO urls ("long", "short", "created_at", "updated_at") VALUES ' + url_pair

Url.connection.execute sql
end

puts Benchmark.measure {seed_db}
5 changes: 5 additions & 0 deletions db/url_test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(http://example.com/205f73f5524708814c220e24f0),
(http://example.com/67428fc65b66f7c27a3e9b55db),
(http://example.com/a8e3b671c50b732ce8f64f52a4),
(http://example.com/ea734b1bbb3c2d6ecdbe1e2223),
(http://example.com/0b1e436875cf1c6bb187019e39);
Loading