Sha256: 72830845c86b4f5ca15cea53446e9e6300e4217e9257632b86ea3e3551b06841

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

require 'awesome_bot/links'
require 'awesome_bot/log'
require 'awesome_bot/result'
require 'awesome_bot/statuses'

# Check links
module AwesomeBot
  NUMBER_OF_THREADS = 10

  STATUS_OK = '✓'
  STATUS_OTHER = 'x'
  STATUS_REDIRECT = '→'

  class << self
    def log_status(s, log)
      if status_is_redirected? s
        log.addp STATUS_REDIRECT
      else
        log.addp(s == 200 ? STATUS_OK : STATUS_OTHER)
      end
    end

    def check(content, white_listed = nil, skip_dupe = false, log = Log.new)
      log.add '> Will allow duplicate links' if skip_dupe

      temp = links_filter(links_find(content))

      r = Result.new(temp, white_listed)
      r.skip_dupe = skip_dupe

      log.add "> White list: #{white_listed.join ', '}" if r.white_listing

      r.dupes = r.links.select { |e| r.links.count(e) > 1 }

      log.addp "Links found: #{r.links.count}"
      log.addp ", #{r.links_white_listed.count} white listed" if r.white_listing
      uniq = r.links.uniq.count
      log.addp ", #{uniq} unique" if uniq != r.links.count
      log.add ''
      r.links.uniq.each_with_index { |u, j| log.add "  #{j + 1}. #{u}" }

      log.addp 'Checking URLs: ' if r.links.count > 0
      r.status =
        statuses(r.links.uniq, NUMBER_OF_THREADS) do |s|
          log_status s, log
        end
      log.add ''

      return r if !r.white_listing || (r.links_white_listed.count == 0)

      log.addp 'Checking white listed URLs: '
      r.white_listed =
        statuses(r.links_white_listed.uniq, NUMBER_OF_THREADS, true) do |s|
          log_status s, log
        end
      log.add ''

      r
    end # check
  end # class
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
awesome_bot-1.5.1 lib/awesome_bot/check.rb