Class: CobwebLinks

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

Overview

CobwebLinks processes links to determine whether they are internal or external links

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CobwebLinks

Initalise’s internal and external patterns and sets up regular expressions



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cobweb_links.rb', line 6

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, @options)}")}
  @external_patterns = @options[:external_urls].map{|pattern| Regexp.new("^#{Cobweb.escape_pattern_for_regex(pattern, @options)}")}

end

Instance Method Details

#allowed?(link) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
# File 'lib/cobweb_links.rb', line 20

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

#external?(link) ⇒ Boolean

Returns true if the link is matched to an external_url or not matched to an internal_url

Returns:

  • (Boolean)


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

def external?(link)
  !@internal_patterns.any?{|pattern| link.match(pattern)} || @external_patterns.any?{|pattern| link.match(pattern)}
end

#internal?(link) ⇒ Boolean

Returns true if the link is matched to an internal_url and not matched to an external_url

Returns:

  • (Boolean)


30
31
32
# File 'lib/cobweb_links.rb', line 30

def internal?(link)
  @internal_patterns.any?{|pattern| link.match(pattern)} && !@external_patterns.any?{|pattern| link.match(pattern)}
end

#matches_external?(link) ⇒ Boolean

Returns:

  • (Boolean)


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

def matches_external?(link)
  @external_patterns.any?{|pattern| link.match(pattern)}
end