-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminime.rb
147 lines (110 loc) · 2.75 KB
/
minime.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Mini.me URL's in Sinatra
#
require 'rubygems'
require 'sinatra'
require 'sequel'
require 'active_record'
require 'erb'
## Setup
## Your Mysql Database(mini_url) needs two tables (mini_me_urls and iterations)
# Table mini_me_urls(id, :string url, :string mini_key)
# Table iterations(id, :integer a, :integer b, :integer c, :integer d)
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "root",
:password => "",
:database => "mini_url"
)
# Models
class Iteration < ActiveRecord::Base
end
class MiniMeUrls < ActiveRecord::Base
end
get '/' do
# Form for entering a long URL
<<-end_form
<body style="background-color:black">
<center>
<div style="margin-top:200px">
<h1 style="color:white">Mini.me URLs!</h1>
<form method='post'>
<input type="text" name="url">
<input type="submit" value="Mini.me It!"><br><br>
<em style="color:white">include http:// in your url</em>
</form>
</div>
</center>
</body>
end_form
end
post '/' do
# Save the long URL
get_code = Iteration.find(:first)
@a_value = get_code[:a]
@b_value = get_code[:b]
@c_value = get_code[:c]
@d_value = get_code[:d]
#### Set the A Value #####
if @b_value == 61
if @a_value < 61
@a = @a_value +1
else
@a = 61
end
end
#### Set the B Value #####
if @c_value == 61
if @b_value < 61
@b = @b_value +1
else
@b = 61
end
end
#### Set the C Value #####
if @d_value == 61
if @c_value < 61
@c = @c_value +1
else
@c = 61
end
end
#### Set the D Value #####
if @d_value < 61
@d = @d_value + 1
else
@d = 61
end
chars = ["o", "8", "T", "I", "D", "J", "Z", "p", "9", "n", "s", "b", "C", "E", "N", "x", "v", "P", "z", "c", "3", "R", "g", "O", "1", "j", "Y", "W", "K", "w", "A", "M", "t", "i", "B", "S", "U", "Q", "6", "u", "0", "X", "L", "4", "G", "F", "V", "m", "H", "r", "e", "k", "l", "d", "q", "h", "2", "7", "a", "5", "y", "f"]
@a = @a.to_i
@b = @b.to_i
@c = @c.to_i
@d = @d.to_i
@a.upto(@a) do |c1|
@b.upto(@b) do |c2|
@c.upto(@c) do |c3|
@d.upto(@d) do |c4|
@mcreate = MiniMeUrls.create(:mini_key => [chars[c1], chars[c2], chars[c3], chars[c4]].join, :url => params[:url])
@icreate = Iteration.update(1,{:a => @a, :b => @b, :c =>@c, :d => @d})
end
end
end
end
mini_url = @mcreate.mini_key
url = request.url + mini_url.to_s
<<-end_form
<body style="background-color:black">
<center>
<div style="margin-top:200px">
<h1 style="color:white">Your shiny new URL!</h1>
<a style="color:white;font-size:20px" href='#{url}'>#{url}</a>
</div>
</center>
</body>
end_form
end
get '/:mini_id' do
# Redirect the Mini.me url
url_rec = MiniMeUrls.find_by_mini_key(params[:mini_id])
redirect "#{url_rec.url}", 301
end