rewrite-tester 0.0.2 → 0.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99a5dcb85fa114f8f769441b8199500652a69389
4
- data.tar.gz: aca33b9d5182d96530dffccde13fabb05360c549
3
+ metadata.gz: 763e235adb195c5ca273470564ebd7b0d9c484b9
4
+ data.tar.gz: e94fe3e995481687c8a7d229d4857c8a2d18a2b3
5
5
  SHA512:
6
- metadata.gz: 0b529484ec15c7f537526c6a1d3aa761c4998c2862139c619027f02ced32f6a67c030f6c565725e69042b944b4147d6e2d394d2b4457294be4a0488894e56635
7
- data.tar.gz: a09c61cb7d4345e53d024d1139c3860b67ed9af212f5d78cdd865b885228625d19f76803cd17e42b993176b6e6202cc8f1def6c30c7ade943a9a43a89f0cb6d8
6
+ metadata.gz: 860246b0af05c765a004a38950c2795d5d62f72b0b8c2526469ec99cbfb759a2fefcf0daf443bbbf17047e5db9854b0552a34bef7a9e8e77f2e89f1aa75b75f4
7
+ data.tar.gz: d973871a71c7d4f1190fda33926b2eecce77d7c45a33d7d4d3241bd4101eb1938bb86801053258394e269d3d41e84c8bf437a95334280efdbddfc88e0752feff
data/README.md CHANGED
@@ -11,10 +11,10 @@ Usage
11
11
 
12
12
  - From the sources
13
13
 
14
- - Clone the repository and use the default rake task to run the tests. You will need to give the absolute path of a file containing a list of `RewriteRules` through a `RULES` env variable.
14
+ - Clone the repository and use the default rake task to run the tests. You will need to give the absolute path of a file containing a list of `RewriteRules` through a `RULES` env variable. Additionally you will need to provide a `HTTP_HOST` env variable, containing the host that you are testing against.
15
15
 
16
16
  `bash
17
- $ RULES=/path/to//my/rules rake
17
+ $ HTTP_HOST=www.example.com RULES=/path/to//my/rules rake
18
18
  `
19
19
 
20
20
  - From your project
@@ -28,10 +28,10 @@ Usage
28
28
  - Use the `test:redirects` rake task by giving a absolute file path to the `RULES` env variable.
29
29
 
30
30
  `bash
31
- $ RULES=/path/to//my/rules rake test:redirects
31
+ $ HTTP_HOST=www.example.com RULES=/path/to//my/rules rake test:redirects
32
32
  `
33
33
 
34
34
  LICENSE
35
35
  ===
36
36
 
37
- MIT
37
+ MIT
data/lib/redirects.rb CHANGED
@@ -2,12 +2,12 @@ require 'rewrite_rule'
2
2
 
3
3
  class Redirects
4
4
 
5
- def initialize io_instance
5
+ def initialize(io_instance, http_host = 'example.com')
6
6
  @rules = []
7
7
 
8
8
  unless io_instance.nil?
9
9
  io_instance.readlines.each do |line|
10
- @rules << RewriteRule.new(line)
10
+ @rules << RewriteRule.new(line, http_host)
11
11
  end
12
12
  end
13
13
  end
@@ -24,4 +24,4 @@ class Redirects
24
24
 
25
25
  attr_accessor :rules
26
26
 
27
- end
27
+ end
data/lib/rewrite_rule.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  class RewriteRule
2
2
 
3
- attr_accessor :possibilities, :substitution
3
+ attr_accessor :possibilities, :substitution, :line
4
4
 
5
- def initialize(line)
5
+ def initialize(line, http_host = 'example.com', http_scheme = 'http://')
6
+ @line = line
7
+ @http_host = http_host
8
+ @http_scheme = http_scheme
6
9
  self.parse line
7
10
  end
8
11
 
@@ -15,7 +18,7 @@ class RewriteRule
15
18
 
16
19
  @regex = match_data[1]
17
20
  @possibilities = generate_possibilities @regex
18
- @substitution = match_data[2].gsub(/\$[0-9]+/, '')
21
+ @substitution = substitute match_data[2]
19
22
  @flags = match_data[3].split(',')
20
23
 
21
24
  end
@@ -25,12 +28,12 @@ class RewriteRule
25
28
  end
26
29
 
27
30
  def redirection?
28
- !@flags.nil? && @flags.include?('R=301')
31
+ !@flags.nil? && @flags.any?{ |flag| /^R/.match(flag) }
29
32
  end
30
33
 
31
34
  def redirects
32
35
  @possibilities.map { |possibility|
33
- { possibility => @substitution}
36
+ { possibility => { :substitution => @substitution, :code => redirection_code(@flags) } }
34
37
  }
35
38
  end
36
39
 
@@ -50,7 +53,23 @@ class RewriteRule
50
53
 
51
54
  # Anything will be replaced by nothing
52
55
  some.map { |possibility|
53
- "http://www.clicrdv.com#{possibility.gsub('(.*)', '')}"
56
+ "#{@http_scheme}#{@http_host}#{possibility.gsub('(.*)', '')}"
54
57
  }
55
58
  end
56
- end
59
+
60
+ private
61
+
62
+ def redirection_code flags
63
+ redirect_regex = /^R=?([0-9]{3})?$/
64
+ redirect_regex.match(flags.detect { |flag| redirect_regex.match(flag) })[1] || '302'
65
+ end
66
+
67
+ def substitute substitute_rule
68
+ substitution = substitute_rule
69
+ substitution = "#{@http_scheme}#{@http_host}#{substitution}" if /^\//.match(substitution)
70
+ substitution = substitution.
71
+ gsub(/\$[0-9]+/, '').
72
+ gsub('%{HTTP_HOST}', @http_host).
73
+ gsub('%{HTTP_SCHEME}', @http_scheme)
74
+ end
75
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rewrite-tester
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Bonaud