Rails - 在 rake 任务中访问 URL - 套接字错误
Rails - Visiting URLs in a rake task - Sockets Error
我正在尝试创建一个访问一系列 URL 的 rake 任务,记录状态代码,然后将它们打印到控制台。我的代码如下:
require 'csv'
require 'net/http'
require "open-uri"
namespace :routes do
desc "check status codes"
task check_301s: :environment do
# open_csv
# edit_urls
generate_routes
# visit_urls
# log_status_codes
visit_and_log_routes
# give_results
end
def generate_routes
csv = CSV.open('lib/better.csv', headers: true)
@urls = []
csv.each do |row|
@urls << row['url'].gsub('foo', 'localhost:3000')
end
end
def visit_and_log_routes
responses = []
@urls.each do |url|
http = Net::HTTP.new(url, 3000)
response = http.request_get('/')
responses << response
end
puts responses
end
end
当 运行 来自终端时,我收到以下错误代码:
SocketError: Failed to open TCP connection to localhost:3000/children-centre/london/greenwich/eltham-childrens-centre/join:3000 (getaddrinfo: nodename nor servname provided, or not known)
我不确定是否有更简单的方法来访问 URL 并记录其状态代码,如果有请告诉我如何操作,如果没有请告诉我如何更正此错误消息。我假设我只是遗漏了相关选项,但不确定如何/在何处添加它们。
对于任何犯过和我一样令人印象深刻的错误的人...路由需要在开始时使用 http:// 而不仅仅是 localhost:3000
我正在尝试创建一个访问一系列 URL 的 rake 任务,记录状态代码,然后将它们打印到控制台。我的代码如下:
require 'csv'
require 'net/http'
require "open-uri"
namespace :routes do
desc "check status codes"
task check_301s: :environment do
# open_csv
# edit_urls
generate_routes
# visit_urls
# log_status_codes
visit_and_log_routes
# give_results
end
def generate_routes
csv = CSV.open('lib/better.csv', headers: true)
@urls = []
csv.each do |row|
@urls << row['url'].gsub('foo', 'localhost:3000')
end
end
def visit_and_log_routes
responses = []
@urls.each do |url|
http = Net::HTTP.new(url, 3000)
response = http.request_get('/')
responses << response
end
puts responses
end
end
当 运行 来自终端时,我收到以下错误代码:
SocketError: Failed to open TCP connection to localhost:3000/children-centre/london/greenwich/eltham-childrens-centre/join:3000 (getaddrinfo: nodename nor servname provided, or not known)
我不确定是否有更简单的方法来访问 URL 并记录其状态代码,如果有请告诉我如何操作,如果没有请告诉我如何更正此错误消息。我假设我只是遗漏了相关选项,但不确定如何/在何处添加它们。
对于任何犯过和我一样令人印象深刻的错误的人...路由需要在开始时使用 http:// 而不仅仅是 localhost:3000