Sha256: f1e97917ad5dfbba6c00948121846b3f8bb922f9ab0c7f5b46bfac7a743539b0

Contents?: true

Size: 1.92 KB

Versions: 22

Compression:

Stored size: 1.92 KB

Contents

# CobwebLinks processes links to determine whether they are internal or external links
class CobwebLinks
  
  # Initalise's internal and external patterns and sets up regular expressions
  def initialize(options={})
    @options = options
    
    raise InternalUrlsMissingError, ":internal_urls is required" unless @options.has_key? :internal_urls
    raise InvalidUrlsError, ":internal_urls must be an array" unless @options[:internal_urls].kind_of? Array
    raise InvalidUrlsError, ":external_urls must be an array" unless !@options.has_key?(:external_urls) || @options[:external_urls].kind_of?(Array)
    @options[:external_urls] = [] unless @options.has_key? :external_urls
    @options[:debug] = false unless @options.has_key? :debug
    
    @internal_patterns = @options[:internal_urls].map{|pattern| Regexp.new("^#{Cobweb.escape_pattern_for_regex(pattern)}")}
    @external_patterns = @options[:external_urls].map{|pattern| Regexp.new("^#{Cobweb.escape_pattern_for_regex(pattern)}")}
    
  end
  
  def allowed?(link)
    if @options[:obey_robots]
      robot = Robots.new(:url => link, :user_agent => @options[:user_agent])
      return robot.allowed?(link)
    else
      return true
    end
  end
  
  # Returns true if the link is matched to an internal_url and not matched to an external_url
  def internal?(link)
    !@internal_patterns.select{|pattern| link.match(pattern)}.empty? && @external_patterns.select{|pattern| link.match(pattern)}.empty?
  end
  
  # Returns true if the link is matched to an external_url or not matched to an internal_url
  def external?(link)
    @internal_patterns.select{|pattern| link.match(pattern)}.empty? || !@external_patterns.select{|pattern| link.match(pattern)}.empty?
  end
  
end

# Exception raised for :internal_urls missing from CobwebLinks
class InternalUrlsMissingError < Exception
end  
# Exception raised for :internal_urls being invalid from CobwebLinks
class InvalidUrlsError < Exception
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
cobweb-1.0.8 lib/cobweb_links.rb
cobweb-1.0.6 lib/cobweb_links.rb
cobweb-1.0.5 lib/cobweb_links.rb
cobweb-1.0.4 lib/cobweb_links.rb
cobweb-1.0.3 lib/cobweb_links.rb
cobweb-1.0.2 lib/cobweb_links.rb
cobweb-1.0.1 lib/cobweb_links.rb
cobweb-1.0.0 lib/cobweb_links.rb
cobweb-0.0.77 lib/cobweb_links.rb
cobweb-0.0.76 lib/cobweb_links.rb
cobweb-0.0.75 lib/cobweb_links.rb
cobweb-0.0.74 lib/cobweb_links.rb
cobweb-0.0.73 lib/cobweb_links.rb
cobweb-0.0.72 lib/cobweb_links.rb
cobweb-0.0.71 lib/cobweb_links.rb
cobweb-0.0.70 lib/cobweb_links.rb
cobweb-0.0.68 lib/cobweb_links.rb
cobweb-0.0.67 lib/cobweb_links.rb
cobweb-0.0.66 lib/cobweb_links.rb
cobweb-0.0.65 lib/cobweb_links.rb