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.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/shawty-server.gemspec +1 -1
  3. data/shawty.rb +19 -21
  4. data/test.rb +11 -15
  5. metadata +1 -1
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{shawty-server}
8
- s.version = "1.0.1"
8
+ s.version = "1.1.0"
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"]
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
- pass unless url = find_url_by_id(params[:id].alphadecimal)
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
- pass if params[:splat].empty?
33
+ url = request.env['REQUEST_URI'] || Array(params[:splat]).first
27
34
 
28
- url = params[:splat].first
29
- url = url[1, url.size] if url.chars.first == '/'
35
+ url = url[1, url.size] if url =~ /^\//
30
36
 
31
- pass if url.empty?
37
+ pass if url.nil? || '' == url
32
38
 
33
39
  quoted = quote url
34
40
 
35
- found = execute %Q{ SELECT id FROM #{table_name} WHERE url = #{quoted} }
41
+ id = select_column %Q{ SELECT id FROM #{table_name} WHERE url = #{quoted} }
36
42
 
37
- if found.any?
38
- id = found.first['id']
39
- else
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 find_url_by_id(id)
56
- result = execute %Q{ SELECT url FROM #{table_name} WHERE id = #{quote id.to_i} }
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
- execute %Q{
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
- execute(%Q{ SELECT COUNT(*) FROM #{table_name} }).first['count'].to_i
63
+ select_column(%Q{ SELECT COUNT(*) FROM #{table_name} }).to_i
67
64
  end
68
65
  should "save the url" do
69
- res = execute(%Q{
70
- SELECT * FROM #{table_name}
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 = execute(%Q{
72
+ id = select_column %Q{
77
73
  SELECT id FROM #{table_name}
78
74
  WHERE url = #{quote 'http://some.url/path.ext'}
79
- }).first['id']
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
- execute(%Q{ SELECT COUNT(*) FROM #{table_name} }).first['count'].to_i
87
+ select_column(%Q{ SELECT COUNT(*) FROM #{table_name} }).to_i
92
88
  end
93
89
  end
94
90
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shawty-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Danger Canty