Sha256: 205aa3e0b11787a3108caa82a90cdbab1e6b0a269fe46edb1c822625e8aee484

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

# Get link status
module AwesomeBot
  STATUS_ERROR = -1

  class << self
    def net_status(url, timeout=30, head=true)
      require 'net/http'
      require 'openssl'
      require 'uri'

      uri = URI.parse url
      Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', :open_timeout => timeout) do |http|
        if head
          request = Net::HTTP::Head.new uri
        else
          request = Net::HTTP::Get.new uri
        end
        response = http.request request

        code = response.code==nil ? 200 : response.code.to_i

        headers = {}
        response.each { |k, v| headers[k] = v }

        return [code, headers]
      end
    end

    def status_is_redirected?(status)
      (status > 299) && (status < 400)
    end

    def statuses(links, threads, timeout, head=true)
      require 'parallel'

      statuses = []
      Parallel.each(links, in_threads: threads) do |u|
        begin
          status, headers = net_status u, timeout, head
          error = nil # nil (success)
        rescue => e
          status = STATUS_ERROR
          headers = {}
          error = e
        end

        yield status, u, headers if block_given?
        statuses.push('url' => u, 'status' => status, 'error' => error, 'headers' => headers)
      end # Parallel

      statuses
    end
  end # class
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
awesome_bot-1.8.4 lib/awesome_bot/net.rb
awesome_bot-1.8.3 lib/awesome_bot/net.rb