Class: Multigiri::LinkChecker

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

Instance Method Summary collapse

Constructor Details

#initialize(app, logger, patterns = nil, &block) ⇒ LinkChecker

Returns a new instance of LinkChecker.



13
14
15
16
17
# File 'lib/multigiri/link_checker.rb', line 13

def initialize(app, logger, patterns = nil, &block)
  @app, @logger, @patterns = app, logger, patterns
  @patterns ||= {"img" => "src", "link" => "href", "a" => "href"}
  block.call(self) unless block.nil?
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  @path_info = env["PATH_INFO"]
  status, headers, document = @app.call(env)
  @patterns.each do |tag, attribute|
    document.css("#{tag}[#{attribute}]").each do |element|
      link = element[attribute]
      if link.match(/^\w+:\/\//)
        check_remote_link(link)
      else
        check_local_link(link)
      end
    end
  end
  [status, headers, document]
end