shawty-server 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/shawty-server.gemspec +1 -1
- data/shawty.rb +19 -21
- data/test.rb +11 -15
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
data/shawty-server.gemspec
CHANGED
data/shawty.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
## Resources
|
2
|
-
require 'rubygems'
|
2
|
+
require 'rubygems' # sorry @defunkt, this is easier
|
3
3
|
gem 'sinatra', :version => '1.0'
|
4
4
|
require 'sinatra'
|
5
5
|
require 'active_record'
|
6
6
|
gem 'alphadecimal'
|
7
7
|
require 'alphadecimal'
|
8
8
|
|
9
|
+
|
10
|
+
|
9
11
|
## Application
|
10
12
|
|
11
13
|
get '/' do
|
@@ -17,44 +19,40 @@ get '/' do
|
|
17
19
|
end
|
18
20
|
|
19
21
|
get '/:id' do
|
20
|
-
|
22
|
+
url = select_column %Q{
|
23
|
+
SELECT url FROM #{table_name}
|
24
|
+
WHERE id = #{quote params[:id].alphadecimal.to_i}
|
25
|
+
}
|
26
|
+
|
27
|
+
pass unless url
|
21
28
|
|
22
29
|
redirect url, 301
|
23
30
|
end
|
24
31
|
|
25
32
|
post '*' do
|
26
|
-
|
33
|
+
url = request.env['REQUEST_URI'] || Array(params[:splat]).first
|
27
34
|
|
28
|
-
url =
|
29
|
-
url = url[1, url.size] if url.chars.first == '/'
|
35
|
+
url = url[1, url.size] if url =~ /^\//
|
30
36
|
|
31
|
-
pass if url.
|
37
|
+
pass if url.nil? || '' == url
|
32
38
|
|
33
39
|
quoted = quote url
|
34
40
|
|
35
|
-
|
41
|
+
id = select_column %Q{ SELECT id FROM #{table_name} WHERE url = #{quoted} }
|
36
42
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
connection.transaction do
|
41
|
-
found = execute %Q{
|
42
|
-
INSERT INTO #{table_name} (url, id) VALUES (#{quoted}, nextval('shawty_id_seq'));
|
43
|
-
SELECT MAX(id) from #{table_name}
|
43
|
+
id ||= select_column %Q{
|
44
|
+
INSERT INTO #{table_name} (url, id) VALUES (#{quoted}, nextval('shawty_id_seq'))
|
45
|
+
RETURNING id
|
44
46
|
}
|
45
|
-
end
|
46
|
-
id = found.first['max']
|
47
|
-
end
|
48
47
|
|
49
|
-
"http://#{request.host}/#{id.alphadecimal}"
|
48
|
+
"http://#{request.host}/#{id.to_i.alphadecimal}"
|
50
49
|
end
|
51
50
|
|
52
51
|
|
53
52
|
## Helpers
|
54
53
|
|
55
|
-
def
|
56
|
-
|
57
|
-
return result.map.first['url'] if result.map.length > 0
|
54
|
+
def select_column sql
|
55
|
+
connection.select_rows(sql).flatten.first
|
58
56
|
end
|
59
57
|
|
60
58
|
def execute sql
|
data/test.rb
CHANGED
@@ -32,19 +32,16 @@ class ShawtyTest < Test::Unit::TestCase
|
|
32
32
|
|
33
33
|
context "on GET to / with url" do
|
34
34
|
setup {
|
35
|
-
|
35
|
+
id = select_column %Q{
|
36
36
|
INSERT INTO #{table_name} (url, id)
|
37
37
|
VALUES (
|
38
38
|
#{quote 'http://google.com/'},
|
39
39
|
nextval('shawty_id_seq')
|
40
40
|
)
|
41
|
+
RETURNING id
|
41
42
|
}
|
42
|
-
id = execute(%Q{
|
43
|
-
SELECT id FROM #{table_name}
|
44
|
-
WHERE url = #{quote 'http://google.com/'}
|
45
|
-
}).first['id'].to_i
|
46
43
|
|
47
|
-
get "/#{id.alphadecimal}"
|
44
|
+
get "/#{id.to_i.alphadecimal}"
|
48
45
|
}
|
49
46
|
should "return a 301 redirect" do
|
50
47
|
assert_equal 301, last_response.status
|
@@ -63,21 +60,20 @@ class ShawtyTest < Test::Unit::TestCase
|
|
63
60
|
assert last_response.ok?
|
64
61
|
end
|
65
62
|
should_change "record count", :by => 1 do
|
66
|
-
|
63
|
+
select_column(%Q{ SELECT COUNT(*) FROM #{table_name} }).to_i
|
67
64
|
end
|
68
65
|
should "save the url" do
|
69
|
-
|
70
|
-
SELECT
|
66
|
+
assert select_column %Q{
|
67
|
+
SELECT id FROM #{table_name}
|
71
68
|
WHERE url = #{quote 'http://some.url/path.ext'}
|
72
|
-
}
|
73
|
-
assert res.one?, res.map.inspect
|
69
|
+
}
|
74
70
|
end
|
75
71
|
should "display the shortened url" do
|
76
|
-
id =
|
72
|
+
id = select_column %Q{
|
77
73
|
SELECT id FROM #{table_name}
|
78
74
|
WHERE url = #{quote 'http://some.url/path.ext'}
|
79
|
-
}
|
80
|
-
assert_equal "http://example.org/#{id.alphadecimal}",
|
75
|
+
}
|
76
|
+
assert_equal "http://example.org/#{id.to_i.alphadecimal}",
|
81
77
|
last_response.body
|
82
78
|
end
|
83
79
|
context "with a url that's been saved previously" do
|
@@ -88,7 +84,7 @@ class ShawtyTest < Test::Unit::TestCase
|
|
88
84
|
assert last_response.ok?
|
89
85
|
end
|
90
86
|
should_not_change "record count" do
|
91
|
-
|
87
|
+
select_column(%Q{ SELECT COUNT(*) FROM #{table_name} }).to_i
|
92
88
|
end
|
93
89
|
end
|
94
90
|
end
|