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
104 changes: 104 additions & 0 deletions .byebug_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
exit
good_urls
random_short
exit
Url.longUrl
longUrl
Url[:longUrl]
Url[:longUr]l
Url.longUrl
params[:longUrl]
Url.last
url.last
good_urls
Url.new(longUrl: "https://www.facebook.com")
url.last
good_urls
good
good_urls.map {|url| url.pop()}
good_urls.pop()
good_urls
good_urls.map {|url| url.pop()}
good_urls.map {|url| url <<"héhé"}
url.pop()
url.pop
good_urls.map {|url| url <<"héhé"}
good_urls.map {|url| url << url.shortUrl}
url.shortUrl = url.shorten
url = Url.new()
url.shortUrl = url.shorten
good_urls.map {|url| url << url.shorten}
good_urls.map {|url| url << shortUrl.shorten}
good_urls.map {|url| url << "hqhqh"}
good_urls
ood_urls
urls
c
exit
urls.map {|url| url.compact}
urls.compact
urls
c
Url.import columns, urls
urls
urls`
exit
@error[0]
@error
n
exit
url.errors.messages[:longUrl]
@error
exit
url.save
exit
url.save
exit
@error[0]
@error[1]
@error
n.error
@error.each do |n|
if !@error.nil?
@error[0]
error[0]
@error[0]
url.errors.messages[:longUrl]
url.errors.message[:longUrl]
exit
@error
puts @error
p @error
print @error
url.errors
Url.errors
error
errors
@error
url.errors.messages[:longUrl]
exit
@error
url.errors.messages[:longUrl]
url.errors.messages[:long_url]
messages[:long_url]
message[:long_url]
@error
message[:longUrl]
messages[:longUrl]
exit
url.errors.messages[:longUrl]
url.errors.message[:longUrl]
:message
params[:message]
url.messages[:longUrl]
url.message[:longUrl]
message[:longUrl]
exit
url.clickCount
n
exit
Url.find_by(shortUrl: params[:shortUrl])
params[:shortUrl]
params[:short_url]
params[:shortUrl]
params[shortUrl]
7 changes: 6 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ source 'https://rubygems.org'
# Adding Sinatra Drivers
gem 'sinatra'
gem 'sinatra-contrib'

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

Expand All @@ -24,6 +24,11 @@ gem 'rake'
# Adding rspec for running unit testing
gem 'rspec'

gem 'activerecord-import'




group :development, :test do
# Adding shotgun for local web hosting
gem 'shotgun'
Expand Down
94 changes: 48 additions & 46 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
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)
activerecord-import (0.17.1)
activerecord (>= 3.2)
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)
byebug (9.0.6)
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)

Expand All @@ -80,7 +80,9 @@ PLATFORMS

DEPENDENCIES
activerecord
activerecord-import
activesupport
byebug
pg
puma
rails_12factor
Expand All @@ -93,4 +95,4 @@ DEPENDENCIES
thin

BUNDLED WITH
1.10.6
1.13.7
18 changes: 18 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
require 'rake'
require 'rspec/core/rake_task'
require 'securerandom'

# Include environment settings
require ::File.expand_path('../config/environments/init', __FILE__)

# Require all ActiveSupport's class and extensions
require 'active_support/core_ext'





namespace :generate do
desc "Create empty model spec in spec, e.g., rake generate:spec NAME=test_name"
task :spec do
Expand Down Expand Up @@ -127,6 +132,19 @@ namespace :db do
task :version do
puts "Current version: #{ActiveRecord::Migrator.current_version}"
end

task :import_urls do
require 'csv'
random_short = SecureRandom.hex(3)
Url.transaction do
urls = CSV.read("test.csv")
good_urls = urls.map {|url| url.compact}
columns = [:longUrl, :shortUrl]
good_urls.map {|url| url << random_short }
byebug
Url.import columns, good_urls, validate: false
end
end
end


Expand Down
42 changes: 42 additions & 0 deletions app/controllers/static.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
require 'byebug'

get '/' do
# let user create new short URL, display a list of shortened URLs


@result = Url.all
erb :"static/index"

end

post '/urls' do

# create a new Url

url = Url.new()
url.longUrl = params[:long_url]
url.shortUrl = url.shorten

if url.save

url.to_json
else

@error = url.errors.messages[:longUrl]
# @result = Url.all

# erb :"static/index"
#redirect "/"
end



end

# i.e. /q6bda
get '/:shortUrl' do
# redirect to appropriate "long" URL

url = Url.find_by(shortUrl: params[:shortUrl])
to_page = url.longUrl
url.countClick += 1
url.save
redirect to_page
end
34 changes: 34 additions & 0 deletions app/models/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'URI'
require 'securerandom'


class Url < ActiveRecord::Base
before_create :shorten
# validates :longUrl, uniqueness: true
# validates :longUrl, presence:{ message:"nothing inside sorry, i need an url!"}
# validates :longUrl, :format => { :with => /"@^(https?|ftp)://[^\s/$.?#].[^\s]*$@iS"/,
# :message => "Enter a valid url, please!"}
#validates :longUrl, :check_validation
validates_format_of :longUrl, :with => URI::regexp(%w(http https)), :message => "Enter a valid url, please!"


def check_validation
# p = URI::Parser.new(:ESCAPED => "(?:%[a-fA-F0-9]{2}|%u[a-fA-F0-9]{4})")
# u = p.parse(params[:longUrl])
# URI.parse(u.to_s)
end

# This is Sinatra! Remember to create a migration!



def shorten
# range_alpha = [*"A".."Z",*"a".."z"]
# range_int = [*"0".."9"]
# short_url = (0...3).map{ range_alpha.sample }.join('')
# short_url2 = (0...4).map{range_int.sample}.join('')

# return short_url + short_url2
return SecureRandom.hex(3)
end
end
8 changes: 8 additions & 0 deletions app/views/layouts/application.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<html>
<head>
<title>Sinatra Framework</title>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="/css/application.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="/js/application.js" ></script>


</head>
<body>
<%= yield %>
Expand Down
Loading