Class: RewriteRule

Inherits:
Object
  • Object
show all
Defined in:
lib/rewrite_rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, conditions = []) ⇒ RewriteRule

Returns a new instance of RewriteRule.

Raises:



9
10
11
12
13
14
15
16
17
# File 'lib/rewrite_rule.rb', line 9

def initialize(line, conditions = [])
  @line = line
  @conds = conditions

  self.parse line

  raise Untestable.new unless @conds.empty?
  raise InvalidRule.new unless valid?
end

Instance Attribute Details

#lineObject

Returns the value of attribute line.



7
8
9
# File 'lib/rewrite_rule.rb', line 7

def line
  @line
end

#possibilitiesObject

Returns the value of attribute possibilities.



7
8
9
# File 'lib/rewrite_rule.rb', line 7

def possibilities
  @possibilities
end

#substitutionObject

Returns the value of attribute substitution.



7
8
9
# File 'lib/rewrite_rule.rb', line 7

def substitution
  @substitution
end

Instance Method Details

#last?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/rewrite_rule.rb', line 55

def last?
  !@flags.nil? && @flags.include?('L')
end

#parse(line) ⇒ Object

Parse an apache RewriteRule and transform it into a Ruby object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rewrite_rule.rb', line 20

def parse(line)
  match_data = /RewriteRule[ ]+([^ ]+)[ ]+([^ ]+)([ ]+\[([^ ]+)\]\n?)?$/.match(line)

  return if match_data.nil?

  @regex = match_data[1]
  @possibilities = generate_possibilities @regex
  @substitution = match_data[2]
  @substitutions = @possibilities.map do |possibility|
    ::Redirects.substitute @substitution, possibility[:substituted_data]
  end
  @flags = match_data[4].nil? ? nil : match_data[4].split(',')

end

#redirection?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/rewrite_rule.rb', line 39

def redirection?
  !@flags.nil? && @flags.any?{ |flag| /^R/.match(flag) }
end

#redirectsObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rewrite_rule.rb', line 43

def redirects
  @substitutions.map.with_index do |substitution, i|
    request = ::Redirects.substitute(@possibilities[i][:request_uri])
    response = substitution.nil? ? request : substitution
    { 
      :possibility => request,
      :substitution => response,
      :code => redirection_code(@flags)
    }
  end
end

#valid?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rewrite_rule.rb', line 35

def valid?
  !@regex.nil? && !@substitution.nil?
end