rewritten 0.14.2 → 0.15.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 388f8074c4d815dbb2df6d3e5c50ddbe77821187
4
- data.tar.gz: 9dd7175a1d4d3d3c503a300c118a1f95841c2d41
3
+ metadata.gz: 452e88a0bbdf356f472e86034e0df828f4708899
4
+ data.tar.gz: bb3f1342257f6aa9414aada49a6ad2af7daf4af8
5
5
  SHA512:
6
- metadata.gz: b3afe4b2a95bb922788496eaacdb0bd86a299074fdb0beae96ad0db54b0a737612d110de02ed64f2dc99f49cfe72905e68e84f77952e42a5c8a61ea0adc5749b
7
- data.tar.gz: 60ece3b7d4b7e1ca470bc4996e4506c605fe9c17ba85f3ff80d9f796df751d1585e17252fbe8085cdee11f044ea0c4777483457335054917c431e659165be71a
6
+ metadata.gz: a5f725ac82a9353f1eaff6dfdb01655dde86ef5e1e3b17df701dad7215aa4d417d26ce5633195d5ed2cd0a35648655b0daba426fd84d7c89bea91f5ccd94fcbb
7
+ data.tar.gz: c4fe89affbe1216585006c25dc3cede93be62ffb4ab839f8bd053185e2c2f14a4200b77e873a0f26413257f6989ee3e928e2e379244bd6f24a885eb3024312f6
data/HISTORY.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.15.0
2
+
3
+ * Support external targets in redirects
4
+
1
5
  == 0.14.0
2
6
  * Extract canonical to own rack app
3
7
 
data/lib/rack/url.rb CHANGED
@@ -6,14 +6,25 @@ module Rack
6
6
 
7
7
  class Url
8
8
 
9
+ attr_accessor :base_url
10
+
9
11
  def initialize(app, &block)
10
12
  @app = app
11
13
  @translate_backwards = false
12
14
  @downcase_before_lookup = false
13
15
  @translate_partial = false
16
+ @base_url = ''
14
17
  instance_eval(&block) if block_given?
15
18
  end
16
19
 
20
+ def is_internal_target?(url)
21
+ url.nil? or url.start_with?('/') or url.start_with?(@base_url)
22
+ end
23
+
24
+ def is_external_target?(url)
25
+ !is_internal_target?(url)
26
+ end
27
+
17
28
  def call(env)
18
29
  req = Rack::Request.new(env)
19
30
 
@@ -22,7 +33,13 @@ module Rack
22
33
  path = "#{subdomain}#{req.path_info}"
23
34
  path.downcase! if downcase_before_lookup?
24
35
 
25
- if ::Rewritten.includes?(path.chomp("/")) or backwards=( translate_backwards? && ::Rewritten.exist_translation_for?(path) )
36
+ target_url = ::Rewritten.translate(path)
37
+
38
+ if is_external_target?(target_url)
39
+ r = Rack::Response.new
40
+ r.redirect(target_url, 301)
41
+ r.finish
42
+ elsif ::Rewritten.includes?(path.chomp("/")) or backwards=( translate_backwards? && ::Rewritten.exist_translation_for?(path) )
26
43
 
27
44
  to = ::Rewritten.includes?(path.chomp("/")) || path
28
45
 
@@ -1,4 +1,4 @@
1
1
  module Rewritten
2
- VERSION = "0.14.2"
2
+ VERSION = "0.15.0"
3
3
  end
4
4
 
@@ -61,6 +61,26 @@ describe Rack::Rewritten::Url do
61
61
  ret[0].must_equal 200
62
62
  end
63
63
 
64
+ describe 'external redirection' do
65
+
66
+ before {
67
+ @app = MiniTest::Mock.new
68
+ @rack = Rack::Rewritten::Url.new(@app) do |config|
69
+ config.base_url = 'http://www.example.org'
70
+ end
71
+
72
+ Rewritten.add_translation '/external/target', 'http://www.external.com'
73
+ }
74
+
75
+ it "must redirect to external target" do
76
+ ret = @rack.call request_url('/external/target')
77
+ @app.verify
78
+ ret[0].must_equal 301
79
+ ret[1]['Location'].must_equal "http://www.external.com"
80
+ end
81
+
82
+ end
83
+
64
84
  describe "partial translation" do
65
85
 
66
86
  before do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rewritten
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.2
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kai Rubarth
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-18 00:00:00.000000000 Z
11
+ date: 2014-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis-namespace