shawty-server 1.1.2 → 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gems +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +50 -0
- data/VERSION +1 -1
- data/config.ru +1 -1
- data/lib/shawty/helpers.rb +44 -0
- data/lib/shawty.rb +60 -0
- data/shawty-server.gemspec +36 -12
- data/test.rb +6 -3
- metadata +128 -27
- data/shawty.rb +0 -106
data/.gems
CHANGED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.2.6)
|
5
|
+
activesupport (= 3.2.6)
|
6
|
+
builder (~> 3.0.0)
|
7
|
+
activerecord (3.2.6)
|
8
|
+
activemodel (= 3.2.6)
|
9
|
+
activesupport (= 3.2.6)
|
10
|
+
arel (~> 3.0.2)
|
11
|
+
tzinfo (~> 0.3.29)
|
12
|
+
activesupport (3.2.6)
|
13
|
+
i18n (~> 0.6)
|
14
|
+
multi_json (~> 1.0)
|
15
|
+
alphadecimal (1.1.2)
|
16
|
+
arel (3.0.2)
|
17
|
+
builder (3.0.0)
|
18
|
+
git (1.2.5)
|
19
|
+
i18n (0.6.0)
|
20
|
+
jeweler (1.8.4)
|
21
|
+
bundler (~> 1.0)
|
22
|
+
git (>= 1.2.5)
|
23
|
+
rake
|
24
|
+
rdoc
|
25
|
+
json (1.7.3)
|
26
|
+
multi_json (1.3.6)
|
27
|
+
pg (0.13.2)
|
28
|
+
rack (1.4.1)
|
29
|
+
rack-test (0.6.1)
|
30
|
+
rack (>= 1.0)
|
31
|
+
rake (0.9.2.2)
|
32
|
+
rdoc (3.12)
|
33
|
+
json (~> 1.4)
|
34
|
+
shoulda (2.11.0)
|
35
|
+
sinatra (1.0)
|
36
|
+
rack (>= 1.0)
|
37
|
+
tzinfo (0.3.33)
|
38
|
+
|
39
|
+
PLATFORMS
|
40
|
+
ruby
|
41
|
+
|
42
|
+
DEPENDENCIES
|
43
|
+
activerecord
|
44
|
+
activesupport
|
45
|
+
alphadecimal
|
46
|
+
jeweler
|
47
|
+
pg
|
48
|
+
rack-test
|
49
|
+
shoulda (= 2.11.0)
|
50
|
+
sinatra (= 1.0.0)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.3
|
data/config.ru
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
class Shawty < Sinatra::Base
|
2
|
+
module Helpers
|
3
|
+
|
4
|
+
def select_column sql
|
5
|
+
connection.select_rows(sql).flatten.first
|
6
|
+
end
|
7
|
+
|
8
|
+
def execute sql
|
9
|
+
connection.execute sql
|
10
|
+
end
|
11
|
+
|
12
|
+
def quote string
|
13
|
+
connection.quote string
|
14
|
+
end
|
15
|
+
|
16
|
+
def table_name
|
17
|
+
connection.quote_table_name 'shawty'
|
18
|
+
end
|
19
|
+
|
20
|
+
def connection
|
21
|
+
ActiveRecord::Base.connection
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize_database
|
25
|
+
table = execute %Q{select * from pg_tables where tablename = #{quote 'shawty'}}
|
26
|
+
return if table.any?
|
27
|
+
execute %Q{
|
28
|
+
CREATE TABLE #{table_name} (
|
29
|
+
id SERIAL PRIMARY KEY,
|
30
|
+
url varchar(2048) UNIQUE
|
31
|
+
)
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
def database_config
|
36
|
+
YAML.load(File.read('config/database.yml'))
|
37
|
+
end
|
38
|
+
|
39
|
+
def init(environment)
|
40
|
+
ActiveRecord::Base.establish_connection database_config[environment]
|
41
|
+
initialize_database
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/shawty.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
## Resources
|
2
|
+
require 'sinatra/base'
|
3
|
+
require 'active_record'
|
4
|
+
require 'alphadecimal'
|
5
|
+
require File.expand_path('../shawty/helpers', __FILE__)
|
6
|
+
|
7
|
+
class Shawty < Sinatra::Base
|
8
|
+
include Helpers
|
9
|
+
extend Helpers
|
10
|
+
|
11
|
+
get '/' do
|
12
|
+
%Q{
|
13
|
+
<body style='line-height: 1.8em; font-family: Archer, Museo, Helvetica, Georgia; font-size 25px; text-align: center; padding-top: 20%;'>
|
14
|
+
Post to '/' to save a url and receive a plaintext short url in response. Example:
|
15
|
+
<pre style='font-family: Iconsolata, monospace;background-color:#EEE'>curl -X POST http://#{request.host}/http://some.url/at.someplace</pre>
|
16
|
+
<br />
|
17
|
+
<form action=/ method=POST onsubmit='if(\"\"==this.url.value)return false;else{this.action=\"/\"+this.url.value}'>
|
18
|
+
<input type=text name='url' />
|
19
|
+
<input type=submit value='Get Shawty' />
|
20
|
+
</form>
|
21
|
+
<small>Also, try <a href='http://github.com/JackDanger/shawty-client'>the official Ruby client</a></small>
|
22
|
+
</body
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
get '/:id' do
|
27
|
+
url = select_column %Q{
|
28
|
+
SELECT url FROM #{table_name}
|
29
|
+
WHERE id = #{quote params[:id].alphadecimal.to_i}
|
30
|
+
}
|
31
|
+
|
32
|
+
pass unless url
|
33
|
+
|
34
|
+
redirect url, 301
|
35
|
+
end
|
36
|
+
|
37
|
+
post '*' do
|
38
|
+
url = request.env['REQUEST_URI'] || Array(params[:splat]).first
|
39
|
+
|
40
|
+
url = url[1, url.size] if url =~ /^\//
|
41
|
+
|
42
|
+
pass if url.nil? || '' == url
|
43
|
+
|
44
|
+
quoted = quote url
|
45
|
+
|
46
|
+
id = select_column %Q{ SELECT id FROM #{table_name} WHERE url = #{quoted} }
|
47
|
+
|
48
|
+
id ||= select_column %Q{
|
49
|
+
INSERT INTO #{table_name} (url, id) VALUES (#{quoted}, nextval('shawty_id_seq'))
|
50
|
+
RETURNING id
|
51
|
+
}
|
52
|
+
|
53
|
+
"http://#{request.host}/#{id.to_i.alphadecimal}"
|
54
|
+
end
|
55
|
+
|
56
|
+
## Environments
|
57
|
+
configure :development do init 'development' end
|
58
|
+
configure :test do init 'test' end
|
59
|
+
configure :production do init 'production' end
|
60
|
+
end
|
data/shawty-server.gemspec
CHANGED
@@ -4,51 +4,75 @@
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "1.1.
|
7
|
+
s.name = "shawty-server"
|
8
|
+
s.version = "1.1.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jack Danger Canty"]
|
12
|
-
s.date =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
12
|
+
s.date = "2012-07-11"
|
13
|
+
s.description = "Run your own url shortener add your own web address for free on Heroku.com"
|
14
|
+
s.email = "gitcommit@6brand.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
17
|
"README.markdown"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".gems",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
21
23
|
"LICENSE",
|
22
24
|
"README.markdown",
|
23
25
|
"Rakefile",
|
24
26
|
"VERSION",
|
25
27
|
"config.ru",
|
26
28
|
"config/database.yml",
|
29
|
+
"lib/shawty.rb",
|
30
|
+
"lib/shawty/helpers.rb",
|
27
31
|
"shawty-server.gemspec",
|
28
|
-
"shawty.rb",
|
29
32
|
"test.rb"
|
30
33
|
]
|
31
|
-
s.homepage =
|
34
|
+
s.homepage = "http://github.com/JackDanger/shawty-server"
|
32
35
|
s.require_paths = ["lib"]
|
33
|
-
s.rubygems_version =
|
34
|
-
s.summary =
|
36
|
+
s.rubygems_version = "1.8.17"
|
37
|
+
s.summary = "Ultra-lightweight url shortening server for Heroku.com"
|
35
38
|
|
36
39
|
if s.respond_to? :specification_version then
|
37
40
|
s.specification_version = 3
|
38
41
|
|
39
42
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
40
|
-
s.add_runtime_dependency(%q<
|
43
|
+
s.add_runtime_dependency(%q<sinatra>, ["= 1.0.0"])
|
44
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 0"])
|
45
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
46
|
+
s.add_runtime_dependency(%q<alphadecimal>, [">= 0"])
|
47
|
+
s.add_runtime_dependency(%q<pg>, [">= 0"])
|
48
|
+
s.add_development_dependency(%q<rack-test>, [">= 0"])
|
49
|
+
s.add_development_dependency(%q<shoulda>, ["= 2.11.0"])
|
50
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
41
51
|
s.add_runtime_dependency(%q<alphadecimal>, [">= 1.0.1"])
|
42
52
|
s.add_development_dependency(%q<active_record>, [">= 0"])
|
43
53
|
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
44
54
|
else
|
45
|
-
s.add_dependency(%q<
|
55
|
+
s.add_dependency(%q<sinatra>, ["= 1.0.0"])
|
56
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
57
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
58
|
+
s.add_dependency(%q<alphadecimal>, [">= 0"])
|
59
|
+
s.add_dependency(%q<pg>, [">= 0"])
|
60
|
+
s.add_dependency(%q<rack-test>, [">= 0"])
|
61
|
+
s.add_dependency(%q<shoulda>, ["= 2.11.0"])
|
62
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
46
63
|
s.add_dependency(%q<alphadecimal>, [">= 1.0.1"])
|
47
64
|
s.add_dependency(%q<active_record>, [">= 0"])
|
48
65
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
49
66
|
end
|
50
67
|
else
|
51
|
-
s.add_dependency(%q<
|
68
|
+
s.add_dependency(%q<sinatra>, ["= 1.0.0"])
|
69
|
+
s.add_dependency(%q<activerecord>, [">= 0"])
|
70
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
71
|
+
s.add_dependency(%q<alphadecimal>, [">= 0"])
|
72
|
+
s.add_dependency(%q<pg>, [">= 0"])
|
73
|
+
s.add_dependency(%q<rack-test>, [">= 0"])
|
74
|
+
s.add_dependency(%q<shoulda>, ["= 2.11.0"])
|
75
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
52
76
|
s.add_dependency(%q<alphadecimal>, [">= 1.0.1"])
|
53
77
|
s.add_dependency(%q<active_record>, [">= 0"])
|
54
78
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
data/test.rb
CHANGED
@@ -5,17 +5,20 @@ require 'test/unit'
|
|
5
5
|
require 'active_support'
|
6
6
|
require 'shoulda'
|
7
7
|
require 'rack/test'
|
8
|
-
require '
|
8
|
+
require 'pp'
|
9
|
+
require File.expand_path('../lib/shawty', __FILE__)
|
9
10
|
|
10
11
|
class ShawtyTest < Test::Unit::TestCase
|
11
12
|
include Rack::Test::Methods
|
13
|
+
include Shawty::Helpers
|
14
|
+
extend Shawty::Helpers
|
12
15
|
|
13
16
|
def setup
|
14
17
|
execute "DELETE FROM #{table_name}"
|
15
18
|
end
|
16
19
|
|
17
20
|
def app
|
18
|
-
|
21
|
+
Shawty
|
19
22
|
end
|
20
23
|
|
21
24
|
context "on GET to /" do
|
@@ -88,4 +91,4 @@ class ShawtyTest < Test::Unit::TestCase
|
|
88
91
|
end
|
89
92
|
end
|
90
93
|
end
|
91
|
-
end
|
94
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shawty-server
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 1.1.
|
9
|
+
- 3
|
10
|
+
version: 1.1.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jack Danger Canty
|
@@ -15,28 +15,126 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
19
|
-
default_executable:
|
18
|
+
date: 2012-07-11 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
|
-
|
23
|
-
version_requirements: &id001 !ruby/object:Gem::Requirement
|
21
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
22
|
none: false
|
25
23
|
requirements:
|
26
24
|
- - "="
|
27
25
|
- !ruby/object:Gem::Version
|
28
|
-
hash:
|
26
|
+
hash: 23
|
29
27
|
segments:
|
30
28
|
- 1
|
31
|
-
-
|
32
|
-
-
|
33
|
-
version: 1.
|
34
|
-
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
version: 1.0.0
|
32
|
+
version_requirements: *id001
|
33
|
+
name: sinatra
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
version_requirements: *id002
|
47
|
+
name: activerecord
|
48
|
+
type: :runtime
|
35
49
|
prerelease: false
|
36
|
-
name: rack
|
37
50
|
- !ruby/object:Gem::Dependency
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
version_requirements: *id003
|
61
|
+
name: activesupport
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
- !ruby/object:Gem::Dependency
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
version_requirements: *id004
|
75
|
+
name: alphadecimal
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
hash: 3
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
version_requirements: *id005
|
89
|
+
name: pg
|
38
90
|
type: :runtime
|
39
|
-
|
91
|
+
prerelease: false
|
92
|
+
- !ruby/object:Gem::Dependency
|
93
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
hash: 3
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
version: "0"
|
102
|
+
version_requirements: *id006
|
103
|
+
name: rack-test
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
- !ruby/object:Gem::Dependency
|
107
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - "="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
hash: 35
|
113
|
+
segments:
|
114
|
+
- 2
|
115
|
+
- 11
|
116
|
+
- 0
|
117
|
+
version: 2.11.0
|
118
|
+
version_requirements: *id007
|
119
|
+
name: shoulda
|
120
|
+
type: :development
|
121
|
+
prerelease: false
|
122
|
+
- !ruby/object:Gem::Dependency
|
123
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
hash: 3
|
129
|
+
segments:
|
130
|
+
- 0
|
131
|
+
version: "0"
|
132
|
+
version_requirements: *id008
|
133
|
+
name: jeweler
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
- !ruby/object:Gem::Dependency
|
137
|
+
requirement: &id009 !ruby/object:Gem::Requirement
|
40
138
|
none: false
|
41
139
|
requirements:
|
42
140
|
- - ">="
|
@@ -47,12 +145,12 @@ dependencies:
|
|
47
145
|
- 0
|
48
146
|
- 1
|
49
147
|
version: 1.0.1
|
50
|
-
|
51
|
-
prerelease: false
|
148
|
+
version_requirements: *id009
|
52
149
|
name: alphadecimal
|
150
|
+
type: :runtime
|
151
|
+
prerelease: false
|
53
152
|
- !ruby/object:Gem::Dependency
|
54
|
-
|
55
|
-
version_requirements: &id003 !ruby/object:Gem::Requirement
|
153
|
+
requirement: &id010 !ruby/object:Gem::Requirement
|
56
154
|
none: false
|
57
155
|
requirements:
|
58
156
|
- - ">="
|
@@ -61,12 +159,12 @@ dependencies:
|
|
61
159
|
segments:
|
62
160
|
- 0
|
63
161
|
version: "0"
|
64
|
-
|
65
|
-
prerelease: false
|
162
|
+
version_requirements: *id010
|
66
163
|
name: active_record
|
67
|
-
- !ruby/object:Gem::Dependency
|
68
164
|
type: :development
|
69
|
-
|
165
|
+
prerelease: false
|
166
|
+
- !ruby/object:Gem::Dependency
|
167
|
+
requirement: &id011 !ruby/object:Gem::Requirement
|
70
168
|
none: false
|
71
169
|
requirements:
|
72
170
|
- - ">="
|
@@ -75,9 +173,10 @@ dependencies:
|
|
75
173
|
segments:
|
76
174
|
- 0
|
77
175
|
version: "0"
|
78
|
-
|
79
|
-
prerelease: false
|
176
|
+
version_requirements: *id011
|
80
177
|
name: shoulda
|
178
|
+
type: :development
|
179
|
+
prerelease: false
|
81
180
|
description: Run your own url shortener add your own web address for free on Heroku.com
|
82
181
|
email: gitcommit@6brand.com
|
83
182
|
executables: []
|
@@ -89,16 +188,18 @@ extra_rdoc_files:
|
|
89
188
|
- README.markdown
|
90
189
|
files:
|
91
190
|
- .gems
|
191
|
+
- Gemfile
|
192
|
+
- Gemfile.lock
|
92
193
|
- LICENSE
|
93
194
|
- README.markdown
|
94
195
|
- Rakefile
|
95
196
|
- VERSION
|
96
197
|
- config.ru
|
97
198
|
- config/database.yml
|
199
|
+
- lib/shawty.rb
|
200
|
+
- lib/shawty/helpers.rb
|
98
201
|
- shawty-server.gemspec
|
99
|
-
- shawty.rb
|
100
202
|
- test.rb
|
101
|
-
has_rdoc: true
|
102
203
|
homepage: http://github.com/JackDanger/shawty-server
|
103
204
|
licenses: []
|
104
205
|
|
@@ -128,7 +229,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
229
|
requirements: []
|
129
230
|
|
130
231
|
rubyforge_project:
|
131
|
-
rubygems_version: 1.
|
232
|
+
rubygems_version: 1.8.17
|
132
233
|
signing_key:
|
133
234
|
specification_version: 3
|
134
235
|
summary: Ultra-lightweight url shortening server for Heroku.com
|
data/shawty.rb
DELETED
@@ -1,106 +0,0 @@
|
|
1
|
-
## Resources
|
2
|
-
require 'rubygems' # sorry @defunkt, this is easier
|
3
|
-
gem 'sinatra', :version => '1.0'
|
4
|
-
require 'sinatra'
|
5
|
-
require 'active_record'
|
6
|
-
gem 'alphadecimal'
|
7
|
-
require 'alphadecimal'
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
## Application
|
12
|
-
|
13
|
-
get '/' do
|
14
|
-
%Q{
|
15
|
-
<body style='line-height: 1.8em; font-family: Archer, Museo, Helvetica, Georgia; font-size 25px; text-align: center; padding-top: 20%;'>
|
16
|
-
Post to '/' to save a url and receive a plaintext short url in response. Example:
|
17
|
-
<pre style='font-family: Iconsolata, monospace;background-color:#EEE'>curl -X POST http://#{request.host}/http://some.url/at.someplace</pre>
|
18
|
-
<br />
|
19
|
-
<form action=/ method=POST onsubmit='if(\"\"==this.url.value)return false;else{this.action=\"/\"+this.url.value}'>
|
20
|
-
<input type=text name='url' />
|
21
|
-
<input type=submit value='Get Shawty' />
|
22
|
-
</form>
|
23
|
-
<small>Also, try <a href='http://github.com/JackDanger/shawty-client'>the official Ruby client</a></small>
|
24
|
-
</body
|
25
|
-
}
|
26
|
-
end
|
27
|
-
|
28
|
-
get '/:id' do
|
29
|
-
url = select_column %Q{
|
30
|
-
SELECT url FROM #{table_name}
|
31
|
-
WHERE id = #{quote params[:id].alphadecimal.to_i}
|
32
|
-
}
|
33
|
-
|
34
|
-
pass unless url
|
35
|
-
|
36
|
-
redirect url, 301
|
37
|
-
end
|
38
|
-
|
39
|
-
post '*' do
|
40
|
-
url = request.env['REQUEST_URI'] || Array(params[:splat]).first
|
41
|
-
|
42
|
-
url = url[1, url.size] if url =~ /^\//
|
43
|
-
|
44
|
-
pass if url.nil? || '' == url
|
45
|
-
|
46
|
-
quoted = quote url
|
47
|
-
|
48
|
-
id = select_column %Q{ SELECT id FROM #{table_name} WHERE url = #{quoted} }
|
49
|
-
|
50
|
-
id ||= select_column %Q{
|
51
|
-
INSERT INTO #{table_name} (url, id) VALUES (#{quoted}, nextval('shawty_id_seq'))
|
52
|
-
RETURNING id
|
53
|
-
}
|
54
|
-
|
55
|
-
"http://#{request.host}/#{id.to_i.alphadecimal}"
|
56
|
-
end
|
57
|
-
|
58
|
-
|
59
|
-
## Helpers
|
60
|
-
|
61
|
-
def select_column sql
|
62
|
-
connection.select_rows(sql).flatten.first
|
63
|
-
end
|
64
|
-
|
65
|
-
def execute sql
|
66
|
-
connection.execute sql
|
67
|
-
end
|
68
|
-
|
69
|
-
def quote string
|
70
|
-
connection.quote string
|
71
|
-
end
|
72
|
-
|
73
|
-
def table_name
|
74
|
-
connection.quote_table_name 'shawty'
|
75
|
-
end
|
76
|
-
|
77
|
-
def connection
|
78
|
-
ActiveRecord::Base.connection
|
79
|
-
end
|
80
|
-
|
81
|
-
def initialize_database
|
82
|
-
table = execute %Q{select * from pg_tables where tablename = #{quote 'shawty'}}
|
83
|
-
return if table.any?
|
84
|
-
execute %Q{
|
85
|
-
CREATE TABLE #{table_name} (
|
86
|
-
id SERIAL PRIMARY KEY,
|
87
|
-
url varchar(255) UNIQUE
|
88
|
-
)
|
89
|
-
}
|
90
|
-
end
|
91
|
-
|
92
|
-
def database_config
|
93
|
-
YAML.load(File.read('config/database.yml'))
|
94
|
-
end
|
95
|
-
|
96
|
-
def init(environment)
|
97
|
-
ActiveRecord::Base.establish_connection database_config[environment]
|
98
|
-
initialize_database
|
99
|
-
end
|
100
|
-
|
101
|
-
|
102
|
-
## Environments
|
103
|
-
|
104
|
-
configure :development do init 'development' end
|
105
|
-
configure :test do init 'test' end
|
106
|
-
configure :production do init 'production' end
|