deelay 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -21,5 +21,5 @@ Listening on 0.0.0.0:4567, CTRL+C to stop
21
21
  ## Usage
22
22
 
23
23
  ```html
24
- <img src="localhost:4567/1000?http://mysite.com/image.gif">
24
+ <img src="localhost:4567/1000/http://mysite.com/image.gif">
25
25
  ```
data/lib/deelay/app.rb CHANGED
@@ -6,20 +6,43 @@ module Deelay
6
6
  register Sinatra::Async
7
7
 
8
8
  aget '/:delay' do
9
- resource_url = parse(request.query_string)
9
+ url = parse_query_string(request.query_string)
10
+ redirect_to url
11
+ end
10
12
 
11
- EM.add_timer(params[:delay].to_i / 1000) do
12
- em_async_schedule { redirect resource_url }
13
- end
13
+ aget '/:delay/*' do
14
+ url = parse_splat(params[:splat].join("/"), request.query_string)
15
+ redirect_to url
14
16
  end
15
17
 
16
18
  private
17
19
 
18
- def parse(url)
19
- unescaped_url = ::URI.unescape(url)
20
+ def redirect_to(url, delay = 0)
21
+ EM.add_timer(delay / 1000) do
22
+ em_async_schedule { redirect url }
23
+ end
24
+ end
25
+
26
+ def parse_query_string(raw_url)
27
+ escaped_url = ::URI.unescape(raw_url)
28
+ raise ArgumentError, "Missing URL" if request.query_string.empty?
29
+
30
+ url = (escaped_url =~ /^http:\/\// ? "" : "http://") + escaped_url
31
+ return url
32
+ end
33
+
34
+ def parse_splat(raw_url, query_string)
35
+ raise ArgumentError, "Missing URL" if raw_url.empty?
36
+
37
+ escaped_url = ::URI.unescape(raw_url)
38
+ url = escaped_url.sub(/^http:\//, "http://")
39
+ url = "http://" + url if url !~ /^http:\/\//
40
+ url << "?" + request.query_string if !request.query_string.empty?
41
+ return url
42
+ end
20
43
 
21
- raise "Invalid URL" if (::URI::regexp =~ unescaped_url).nil?
22
- return unescaped_url
44
+ def validate(url)
45
+ raise "Invalid url" if url =~ /(http|https)/
23
46
  end
24
47
 
25
48
  # swaps env when testing
@@ -1,3 +1,3 @@
1
1
  module Deelay
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -13,37 +13,67 @@ class TestDeelayApp < MiniTest::Unit::TestCase
13
13
  Deelay::App.new
14
14
  end
15
15
 
16
- def test_redirect_on_valid_request
17
- get "/10?http://testurl.com"
16
+ def test_redirect_query_with_scheme
17
+ get "/10?http://testurl.com/path"
18
18
  em_async_continue
19
- assert_equal "http://testurl.com", last_response.location
19
+ assert_equal "http://testurl.com/path", last_response.location
20
20
  assert_equal 302, last_response.status
21
21
  end
22
22
 
23
- def test_redirect_on_valid_request_with_port
24
- get "/10?http://testurl.com:1234"
25
- em_async_continue
26
- assert_equal "http://testurl.com:1234", last_response.location
27
- assert_equal 302, last_response.status
23
+ def test_redirect_query_no_scheme
24
+ get "/10?testurl.com/path"
25
+ em_async_continue
26
+ assert_equal "http://testurl.com/path", last_response.location
27
+ assert_equal 302, last_response.status
28
+ end
29
+
30
+ def test_redirect_query_with_port
31
+ get "/10?testurl.com:1234/path"
32
+ em_async_continue
33
+ assert_equal "http://testurl.com:1234/path", last_response.location
34
+ assert_equal 302, last_response.status
35
+ end
36
+
37
+
38
+ def test_redirect_url_with_scheme
39
+ get "/10/http://testurl.com/path"
40
+ em_async_continue
41
+ assert_equal "http://testurl.com/path", last_response.location
42
+ assert_equal 302, last_response.status
43
+ end
44
+
45
+ def test_redirect_url_no_scheme
46
+ get "/10/testurl.com/path"
47
+ em_async_continue
48
+ assert_equal "http://testurl.com/path", last_response.location
49
+ assert_equal 302, last_response.status
28
50
  end
29
51
 
52
+ def test_redirect_url_with_port
53
+ get "/10/testurl.com:1234"
54
+ em_async_continue
55
+ assert_equal "http://testurl.com:1234", last_response.location
56
+ assert_equal 302, last_response.status
57
+ end
58
+
59
+ def test_redirect_url_with_query
60
+ get "/10/http://testurl.com/path?query=param"
61
+ em_async_continue
62
+ assert_equal "http://testurl.com/path?query=param", last_response.location
63
+ assert_equal 302, last_response.status
64
+ end
65
+
66
+
30
67
  def test_no_query
31
68
  get "/10"
32
- assert_raises(RuntimeError) do
69
+ assert_raises(ArgumentError) do
33
70
  em_async_continue(0.1)
34
71
  end
35
72
  end
36
73
 
37
74
  def test_no_link
38
75
  get "/10?"
39
- assert_raises(RuntimeError) do
40
- em_async_continue(0.1)
41
- end
42
- end
43
-
44
- def test_no_scheme_link
45
- get "/10?testurl.com"
46
- assert_raises(RuntimeError) do
76
+ assert_raises(ArgumentError) do
47
77
  em_async_continue(0.1)
48
78
  end
49
79
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deelay
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -46,7 +46,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
46
46
  version: '0'
47
47
  segments:
48
48
  - 0
49
- hash: 3411104809723433661
49
+ hash: -2051873546142903568
50
50
  required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements: