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
94 changes: 94 additions & 0 deletions .byebug_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
c
params
c
params
c
params
c
params
c
params
c
a = params[:url][:long].strip
params
c
exit
values[0][2]
values[0][1]
values[0]
values
values.push([row[0], row[1]])
values
c
exit
row[0]
row[0].gsub!(/^[(]/, '').gsub!(/[)]/, '')
row[0].gsub(/^[(]/, '').gsub(/[)]/, '')
row[0].gsub(/^[(]/, '')
row[0]
row[1]
c
{alert: "dasd", url_object: @new_url}.to_json
c
@new_url.to_json
@new_url
c
@new_url.id
url.id
if url.id != nil do
if @new_url.id != nil do
if @new_url.id != nil do p "haha"
if @new_url.id != nil do put "haha" else put "noo" end
if @new_url.id != nil do puts "haha" else puts "noo" end
@new_url
exit
@new_url
exit
row
c
row
exit
row
exit
e
c
row[0]
row
c
values[1][1]
values[1][0]
values[1][1]
values[1]
values[0]
c
params
c
ori_link.long.gsub(/(http)[s]*[:](\/\/)/, '')
ori_link.long.gsub("(http)[s]*[:](\/\/)", '')
c
ori_link.long
ori_link.long.gsub("(http)[s]*[:](\/\/)", '')
c
ori_link.long
c
ori_link.long
c
ori_link.long
ori_link
c
@alert_msg
exit
@alert_msg
@alert_msg.to_json
exit
@alert_msg.to_json
c
params[:url][:long]
params
c
n
e
n
params
@form
form
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ gem 'pg'
# Adding rake for management
gem 'rake'

gem 'byebug'

# Adding rspec for running unit testing
gem 'rspec'

Expand Down
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ GEM
arel (6.0.3)
backports (3.6.7)
builder (3.2.2)
byebug (9.0.6)
daemons (1.2.3)
diff-lcs (1.2.5)
eventmachine (1.0.8)
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.12.5
5 changes: 4 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'rake'
require 'rspec/core/rake_task'

require_relative 'db/seeds'

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

Expand Down Expand Up @@ -120,7 +122,8 @@ namespace :db do

desc "Populate the database with dummy data by running db/seeds.rb"
task :seed do
require APP_ROOT.join('db', 'seeds.rb')
# require APP_ROOT.join('db', 'seeds.rb')
UrlImporter.import('db/urls')
end

desc "Returns the current schema version number"
Expand Down
156 changes: 156 additions & 0 deletions app/controllers/static.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,159 @@
require 'pp'



get '/' do
puts "[LOG] Getting /"
puts "[LOG] Params: #{params.inspect}"
@urls = Url.all
@alert_msg = ""

erb :"static/index"

end

post '/urls' do
# create a new Url
# p params[:url]
# p long_url = params[:url][:long]

# @new_url = Url.create params[:url]
# @urls = Url.all

if Url.valid_url?(params[:url][:long])
@new_url = Url.find_or_initialize_by(params[:url])
if @new_url.save
@alert_msg = ""
else
p @new_url.errors.messages
@alert_msg = "There is a problem with your link"
end

else
@alert_msg = "'#{params[:url][:long]}'' is not a valid URL."
end

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

end



post '/ajax' do
# JQuery will receive the return value of this method
# in the response
# And Ruby will return the value to JQuery
# from the the last line of the method.

p "ajax"

# strip whitespace
params[:url][:long].strip!

if Url.valid_url?(params[:url][:long])
# @new_url = Url.new(params[:url])

# will not create a new object if we can find in the table
@new_url = Url.find_or_initialize_by(params[:url])

# don't update table if it is existed
if @new_url.id == nil
if @new_url.save
# @new_url.to_json
{existed: "0", url_object: @new_url}.to_json
else
# byebug
# p @new_url.errors.messages
# @alert_msg = '{"alert_msg": "There is a problem with your link"}'
{alert_msg: "There is a problem with your link"}.to_json

end
else
# byebug
{existed: "1", url_object: @new_url}.to_json
end
else
# @alert_msg = '{"alert_msg": "Not a valid URL"}'
{alert_msg: "Invalid URL"}.to_json

end

end


post '/ajax_key' do
# JQuery will receive the return value of this method
# in the response
# And Ruby will return the value to JQuery
# from the the last line of the method.

p "ajax KEY"
# byebug
params[:url][:long].strip!
if Url.valid_url?(params[:url][:long])
# @new_url = Url.new(params[:url])
# will not create a new object if we can find in the table
@new_url = Url.find_or_initialize_by(params[:url])

if @new_url.id == nil
if @new_url.save
# @alert_msg = ""
# @new_url.to_json
{existed: "0", url_object: @new_url}.to_json
else
# p @new_url.errors.messages
# p @new_url.errors
# @alert_msg = '{"alert_msg": "There is a problem with your link"}'
{alert_msg: "There is a problem with your link"}.to_json
end
else
# byebug
{existed: "1", url_object: @new_url}.to_json
end

else
@alert_msg = nil
end

end







get '/:short_url' do
# redirect to appropirate "long" URL
# this will fetch the short_url in the table and redirect it to the long link

p params[:short_url]

# ori_link = Url.where(short: params[:short_url])
# if ori_link.valid?
# p "horraaay"
# else
# p "OH nooo"
# end

if Url.where(short: params[:short_url]).count(:id) > 0
ori_link = Url.where(short: params[:short_url]).first

ori_link.add_click_count
ori_link.save

# byebug

# redirect to("https://" + ori_link.long)
redirect to("https://" + ori_link.long.gsub(/(http)[s]*[:](\/\/)/, ''))
else
@urls = Url.all
@alert_msg = "invalid short url"

#redirect to('/')

erb :"static/index"
end

end
51 changes: 51 additions & 0 deletions app/models/url.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
=begin
+------------+
| urls |
+------------+
| ori_link |
| short_link |
+------------+
=end
require 'securerandom'

class Url < ActiveRecord::Base
before_create :shorten
#http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

validates :long, presence: true

# This is Sinatra! Remember to create a migration!
def shorten
p "** #{self.id} | #{self.long}"
# self.short = Base64.urlsafe_encode64("rudy" + "[#{self.id}]")
# self.short = Base64.urlsafe_encode64("rudy" + Time.now.to_i.to_s)
self.short = Base64.urlsafe_encode64(Time.now.to_i.to_s + SecureRandom.base64(5))

# can use SecureRandom

p "**** #{self.short}"

end

def add_click_count
self.click_count += 1
end

def self.valid_url?(str)
# return true if not valid, otherwise false
if str =~ /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
puts "valid url = #{str}"
return true
else
return false
end
end

def self.seed_shorten
# p "seed shorten"
short = Base64.urlsafe_encode64(Time.now.to_i.to_s + SecureRandom.base64(5))
# p short
end

end

10 changes: 9 additions & 1 deletion 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>
<title>Pendekar</title>
<link rel="stylesheet" type="text/css" href="/css/application.css">
<link rel="stylesheet" type="text/css" href="/css/jquery-ui.min.css">

<script src="/js/jquery-3.1.1.min.js"></script>
<script src="/js/jquery-ui.min.js"></script>

<script src="/js/application.js"></script>

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