Class: WebRobots::RobotsTxt::AccessControlLine

Inherits:
Line
  • Object
show all
Defined in:
lib/webrobots/robotstxt.rb

Direct Known Subclasses

AllowLine, DisallowLine

Instance Attribute Summary

Attributes inherited from Line

#token, #value

Instance Method Summary (collapse)

Methods inherited from Line

#initialize

Constructor Details

This class inherits a constructor from WebRobots::RobotsTxt::Line

Instance Method Details

- (Object) compile



674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/webrobots/robotstxt.rb', line 674

def compile
  @empty = @value.empty?
  re_src = '\A'
  s = StringScanner.new(@value)
  until s.eos?
    if t = s.scan(/[^%*$]+/)
      re_src << Regexp.quote(t)
    elsif t = s.scan(/%([0-9a-f]{2})/i)
      c = s[1].to_i(16)
      if c == 0x2f
        re_src << '%2[fF]'
      else
        re_src << Regexp.quote('%c' % c)
      end
    elsif t = s.scan(/\*/)
      re_src << '.*'
    elsif t = s.scan(/\$/)
      re_src << '\z'
      break
    else
      re_src << Regexp.quote(s.scan(/./))
    end
  end
  @pattern = Regexp.new(re_src, Regexp::MULTILINE)
  self
end

- (Boolean) match?(request_uri)

Returns:

  • (Boolean)


701
702
703
704
705
# File 'lib/webrobots/robotstxt.rb', line 701

def match?(request_uri)
  return false if @empty
  transformed = request_uri.gsub(/(%2[fF])|%([0-9a-f]{2})/i) { $1 || '%c' % $2.to_i(16) }
  !!@pattern.match(transformed)
end