Class: Redirects
- Inherits:
-
Object
- Object
- Redirects
- Defined in:
- lib/redirects.rb
Constant Summary collapse
- HTTP_HOST =
ENV['HTTP_HOST'] || 'example.com'
- HTTP_SCHEME =
ENV['HTTP_SCHEME'] || 'http://'
Instance Attribute Summary collapse
-
#http_host ⇒ Object
Returns the value of attribute http_host.
-
#http_scheme ⇒ Object
Returns the value of attribute http_scheme.
-
#rules ⇒ Object
Returns the value of attribute rules.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(io_instance) ⇒ Redirects
constructor
A new instance of Redirects.
Constructor Details
#initialize(io_instance) ⇒ Redirects
Returns a new instance of Redirects.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/redirects.rb', line 9 def initialize(io_instance) @rules = [] @untestable_rules = [] unless io_instance.nil? conds = [] io_instance.readlines.each.with_index do |line,i| begin match_data = /^[^#]*RewriteCond/.match(line) unless match_data.nil? conds << RewriteCond.new(line) next end match_data = /^[^#]*RewriteRule/.match(line) unless match_data.nil? rule = RewriteRule.new(line, conds) @rules << rule conds = [] next end rescue InvalidRule => e raise "\e[41mSyntax error on Line #{i+1}: #{line}\e[49m" rescue Untestable => e @untestable_rules << "(Line #{i+1}) #{/.*(Rewrite.*)/.match(line)[1]}" end end unless @untestable_rules.empty? text = <<EOS \e[91m Rewriting rule with conditions are not supported (for now..), please check the following rules by hand: #{@untestable_rules.join("\n")} \e[49m EOS puts text end end end |
Instance Attribute Details
#http_host ⇒ Object
Returns the value of attribute http_host.
54 55 56 |
# File 'lib/redirects.rb', line 54 def http_host @http_host end |
#http_scheme ⇒ Object
Returns the value of attribute http_scheme.
54 55 56 |
# File 'lib/redirects.rb', line 54 def http_scheme @http_scheme end |
#rules ⇒ Object
Returns the value of attribute rules.
54 55 56 |
# File 'lib/redirects.rb', line 54 def rules @rules end |
Class Method Details
.substitute(substitute_rule, substituted_data = []) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/redirects.rb', line 56 def self.substitute substitute_rule, substituted_data = [] return nil if substitute_rule == '-' substitution = substitute_rule substitution = "/#{substitution}" if /^(\/|https?:\/\/)/.match(substitution).nil? substitution = "#{HTTP_SCHEME}#{HTTP_HOST}#{substitution}" unless /^https?\:\/\//.match(substitution) substitution = substitution. gsub(/\$[0-9]+?/){ |m| !m.nil? && substituted_data[m[1..-1].to_i-1] || '' }. gsub('%{HTTP_HOST}', HTTP_HOST). gsub('%{HTTP_SCHEME}', HTTP_SCHEME) end |