require 'typhoeus'

module Metrics

  class LinkChecker < Metric
    attr_reader :score

    def initialize(metadata, worker=nil)
      @worker = worker
      @total = metadata.length
      @resource_availability = Hash.new
      metadata.each_with_index do |dataset, i|
        dataset[:resources].each do |resource|
          @resources += 1
          url = resource[:url]
          enqueue_request(url)
        end
        @worker.at(i + i, @total)
      end
    end

  end

end

require 'typhoeus'

module Metrics

  class LinkChecker < Metric
    attr_reader :score

    def initialize(metadata, worker=nil)
      @worker = worker
      @processed = 0
      @requests = 0
      @total = metadata.length

      @resource_availability = Hash.new { |h, k| h[k] = Hash.new }
      metadata.each_with_index do |dataset, i|
        dataset[:resources].each do |resource|
          @resources += 1
          url = resource[:url]
          enqueue_request(url)
        end
        @worker.at(i + i, @total)
      end
    end

    def enqueue_request(id, url)

      config = {:method => :head,
                :timeout => 20,
                :connecttimeout => 10,
                :nosignal => true}

      @resource_availability[id][url] = false
      request = Typhoeus::Request.new(url, config)
      request.on_complete do |response|
        if response.success?
          @resource_availability[id] = true
        end
        @worker.at(@processed + 1, @requests)
        @processed += 1
      end
      @dispatcher.queue(request)
      @requests += 1
    end

  end

end

Add Typhoeus request code for the Link Checker
Add Typhoeus request code for the Link Checker

Just added the code to determine whether an URL is working or not. This
can be done by relying on the response.success? method, as in every
other case either another response code has been delivered or the
request timed out.

Signed-off-by: Konrad Reiche <4ff0213f56e2082fcd8c882d04c546881c579ce4@gmail.com>
