lib/awesome_bot/statuses.rb in awesome_bot-1.8.1 vs lib/awesome_bot/statuses.rb in awesome_bot-1.8.2
- old
+ new
@@ -1,44 +1,47 @@
# Get link status
module AwesomeBot
- require 'net/http'
- require 'openssl'
- require 'uri'
-
- require 'parallel'
-
STATUS_ERROR = -1
class << self
- def net_status(url, head, timeout=30)
+ def net_status(url, timeout=30)
+ 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|
request = Net::HTTP::Get.new uri
response = http.request request
- return response
+
+ 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=false)
+ def statuses(links, threads, timeout)
+ require 'parallel'
+
statuses = []
Parallel.each(links, in_threads: threads) do |u|
begin
- response = net_status u, head, timeout
- status = response.code==nil ? 200 : response.code.to_i
- headers = {}
- response.each { |k, v| headers[k] = v }
+ status, headers = net_status u, timeout
error = nil # nil (success)
rescue => e
status = STATUS_ERROR
headers = {}
error = e
end
- yield status, u
+ yield status, u if block_given?
statuses.push('url' => u, 'status' => status, 'error' => error, 'headers' => headers)
end # Parallel
statuses
end